Skip to content

Instantly share code, notes, and snippets.

@avit
avit / calculations_a.rb
Created September 8, 2012 23:43
ActiveRecord::Calculations API discussion
# 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")
@avit
avit / mailer.rb
Created August 25, 2012 13:35
inheritable_copy error
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
@avit
avit / robot_army_relation.rb
Created August 13, 2012 04:04
Testing associations
class Army < AR:B
has_many :robots
has_many :weapons
end
class Robot < AR:B
belongs_to: :army
has_one :weapon
end
@avit
avit / rate.rb
Created July 24, 2012 21:37
model arithmetic
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
@avit
avit / count_with_group_and_select.rb
Created July 21, 2012 09:22
ActiveRecord::Relation::Calculations#count fails on grouping with select
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
@avit
avit / nil_object.rb
Created July 19, 2012 19:47
NilObject, much borrowed from avdi.org
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
@avit
avit / regexp.rb
Created June 22, 2012 21:47
Ruby Regexp match named capture
# 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
@avit
avit / articles_controller.rb
Created June 18, 2012 23:17
Showing errors for a form partial
class ArticlesController < ActionController::Base
def show
@article = Article.find(params[:id])
# ... bunch of stuff to set up template display
end
end
@avit
avit / posts_controller.rb
Created June 12, 2012 19:46
Testing page caching
class PostsController < ApplicationController
caches_page :index
def index
render text: "html!"
end
end
@avit
avit / Cheffile
Created June 7, 2012 01:00
librarian-chef
#!/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'