This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Goal: split responsibilities of count and grouped_count so they are responsible for returning | |
# count => Fixnum and grouped_count => Hash in all cases | |
# | |
# This option removes original calculation options for count(:group => x) | |
# in favour of grouped_count(:group => x) | |
# | |
# (I'm showing count and average as examples of optional & required column_name params, | |
# but this would apply to all calculation methods.) | |
relation = Post.group("author_id").order("created_at") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyMailer < ActionMailer::Base | |
def notice(user) | |
# ... | |
mail(to: @to, from: @from, subject: @subject) do |format| | |
format.html { render text: @html_content } | |
format.text { render text: @text_content } | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Army < AR:B | |
has_many :robots | |
has_many :weapons | |
end | |
class Robot < AR:B | |
belongs_to: :army | |
has_one :weapon | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe Rate do | |
it "can do arithmetic" do | |
total = Rate.new(amount: 100.00) + Rate.new(amount: 200.00) | |
total.should == 300.00 | |
end | |
end | |
class Rate < ActiveRecord::Base | |
attr_accessible :amount |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'activerecord', '4.0.0.beta' | |
require 'active_record' | |
require 'logger' | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Base.logger.level = Logger::INFO | |
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', | |
:database => ':memory:' ) | |
ActiveRecord::Schema.define do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NilObject | |
def true?; false end | |
def false?; true end | |
def nil?; true end | |
def blank?; true end | |
def present?; false end | |
def empty?; true end | |
def to_s; "" end | |
def !; true end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Regex with two named captures both named "word" | |
r = %r{ | |
(?<word>[a-z]+)\s+ | |
(?<word>[a-z]+) | |
}xi | |
m = r.match("one two") | |
# => #<MatchData "one two" word:"one" word:"two"> | |
m.captures |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArticlesController < ActionController::Base | |
def show | |
@article = Article.find(params[:id]) | |
# ... bunch of stuff to set up template display | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
caches_page :index | |
def index | |
render text: "html!" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
#^syntax detection | |
site 'http://community.opscode.com/api/v1' | |
cookbook 'application_nginx' | |
cookbook 'application_ruby' | |
cookbook 'apt' | |
cookbook 'git' | |
cookbook 'imagemagick' |