Skip to content

Instantly share code, notes, and snippets.

@iannono
iannono / notes_of_rails4_patterns.md
Last active December 21, 2015 12:19
notes of rails 4 patterns

models

  • controllers should be responsible for orchestrating the models. means that you should always tell objects what to do, and not ask them question about there state
# controller
if @item.publish
  flash[:notice] = "xxxx"
end
# modle
class Item < ActiveRecord::Base
require 'feedzirra'
# fetching a single feed
feed = Feedzirra::Feed.fetch_and_parse("http://feeds.feedburner.com/PaulDixExplainsNothing")
# feed and entries accessors
feed.title # => "Paul Dix Explains Nothing"
feed.url # => "http://www.pauldix.net"
feed.feed_url # => "http://feeds.feedburner.com/PaulDixExplainsNothing"
feed.etag # => "GunxqnEP4NeYhrqq9TyVKTuDnh0"
### test
capture_io
flunk msg = nil
pass msg = nil
skip msg = nil, bt = caller
assert test, msg = nil
assert_block msg = nil
assert_empty obj, msg = nil
#Model
@user.should have(2).children # check assosiation
@user.should have(1).error_on(:username) # Checks whether there is an error in username
@user.errors[:username].should include("can't be blank") # check for the error message
expect { @user.save }.to change { User.count }.by(1) # or from(1).to(2)
expect { @user.save! }.to raise_error(ActivieRecord::RecordInvalid)
#More matchers
@person.should respond_to(:child?)
@iannono
iannono / notes_of_rails_best_practice.md
Last active December 22, 2015 15:19
Notes of rails best practice of codeschool
  • use scope
scope :trending, -> { |num = nil| where('started_trending > ?', 1.days.ago).
                                  order('mentions desc').
                                  limit(num) }
                                  
@tweets = current_user.tweets.unscoped.order(:status).limit(10)

Capybara

save_and_open_page

Matchers

have_button(locator)
# username = "my_username"
# pwd = "my_password"
# target_path = "my_target_path"
# saving auth cookie
system %Q{wget --save-cookies /tmp/cookie.txt --keep-session-cookies --post-data "username=#{username}&password=#{pwd}" -O - \
https://rubytapas.dpdcart.com/subscriber/login?__dpd_cart=d08391e6-5fe2-4400-8b27-2dc17b413027}
(25..600).each do |i|
@iannono
iannono / common_gem
Last active August 29, 2015 14:08
env:== rails
** devise **
https://github.com/plataformatec/devise
rails generate devise:install
rails generate devise MODEL
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
before_action :authenticate_user!
rails generate devise:views users
rails generate devise:controllers [scope]
@iannono
iannono / encrypt_and_decrype
Created November 1, 2014 08:58
type:== rails
before_save :encrypt_note
def encrypt_note
salt = SecureRandom.random_bytes(64)
key = encrypt_key || ActiveSupport::KeyGenerator.new(ENV["SECRET_KEY"]).generate_key(salt)
crypt = ActiveSupport::MessageEncryptor.new(key)
%w(client_note food medicines hydration md_appointments activity mood positive_thinking positive_action listener_note).each_with_index do |att|
val = if !new_record? and send("#{att}_changed?")
read_attribute(att.to_sym)

tmux cheatsheet

As configured in my dotfiles.

start new:

tmux

start new with session name: