Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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")
class SomethingsController < ActionController::Base
def create
Time.zone = current_user.time_zone
@another_user = current_user.friends.find(params[:friend_id])
FriendMailer.message(@another_user).deliver
# Wouldn't Time.zone be reset by the mailer before rendering the page here?
end
end
@avit
avit / rack_inputs.html
Created October 9, 2012 03:58
Bad inputs
<!-- this combination of inputs will throw Rack -->
<input name="event[dates][]" type="date" />
<input id="event_dates__destroy" name="event[dates][_destroy]" type="hidden" />
Oct 16 19:56:48 localhost mysqld_safe[27557]: ERROR: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALTER TABLE user ADD column Show_view_priv enum('N','Y') CHARACTER SET utf8 NOT ' at line 1
Oct 16 19:56:48 localhost mysqld_safe[27557]: 121016 19:56:48 [ERROR] Aborting
class Foo < ActiveRecord::Base
serialize :check_in, TimeOfDayAttribute # dump/load for valid, empty, nil values tested independently
# Setter will handle both String & TimeOfDay input
# I want to test the getter before defining (and depending on) this method
def check_in=(param)
# ...
end
end