Skip to content

Instantly share code, notes, and snippets.

View liangzan's full-sized avatar

Wong Liang Zan liangzan

View GitHub Profile
# lib/formtastic/i18n.rb
SCOPES = [
'soccer_team.player.update.name',
'soccer_team.update.name',
'soccer_team.player.name',
'soccer_team.name',
'player.name',
'name'
]
# lib/formtastic/i18n.rb
def translate(*args)
key = args.shift.to_sym
options = args.extract_options!
options.reverse_merge!(:default => DEFAULT_VALUES[key])
options[:scope] = [DEFAULT_SCOPE, options[:scope]].flatten.compact
::I18n.translate(key, *(args << options))
end
# actionpack/lib/action_view/base.rb
module ActionView
class Base
...
attr_accessor :output_buffer
...
end
end
# actionpack/lib/action_view/helpers/text_helper.rb
def concat(string, unused_binding = nil)
if unused_binding
ActiveSupport::Deprecation.warn("The binding argument of #concat is no longer needed. Please remove it from your views and helpers.", caller)
end
output_buffer.safe_concat(string)
end
# spec/commit_button_spec.rb
describe 'SemanticFormBuilder#commit_button' do
...
before do
@output_buffer = ''
mock_everything
end
...
end
# spec/commit_button_spec.rb
it 'should pass options given in :button_html to the button' do
@new_post.stub!(:new_record?).and_return(false)
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button('text', :button_html => {:class => 'my_class', :id => 'my_id'}))
end
output_buffer.should have_tag('li.commit input#my_id')
output_buffer.should have_tag('li.commit input.my_class')
end
class Event < ActiveRecord::Base
validates_length_of :title, :minimum => 5
end
>> reflection = Event.reflect_on_validations_for(:title)
>> reflection[0].macro
=> :validates_length_of
>> reflection[0].options
=> { :minimum => 5 }
# lib/validation_reflection.rb
ActiveRecord::Base.class_eval do
include ::ActiveRecordExtensions::ValidationReflection
::ActiveRecordExtensions::ValidationReflection.load_config
::ActiveRecordExtensions::ValidationReflection.install(self)
end
# lib/validation_reflection.rb
def install(base)
@@reflected_validations.each do |validation_type|
next if base.respond_to?(:"#{validation_type}_with_reflection")
ignore_subvalidations = false
if validation_type.kind_of?(::Hash)
ignore_subvalidations = validation_type[:ignore_subvalidations]
validation_type = validation_type[:method]
end