Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

do is_a? tests on assignment so runtime is faster

  • Loading branch information...
commit f25251b5734a2a0934d60dd99c93dfd3d42f2c6c 1 parent afa510e
@tenderlove tenderlove authored
View
12 activerecord/lib/active_record/associations/builder/collection_association.rb
@@ -56,7 +56,17 @@ def define_callback(callback_name)
# TODO : why do i need method_defined? I think its because of the inheritance chain
model.class_attribute full_callback_name unless model.method_defined?(full_callback_name)
- model.send("#{full_callback_name}=", Array(options[callback_name.to_sym]))
+ callbacks = Array(options[callback_name.to_sym]).map do |callback|
+ case callback
+ when Symbol
+ ->(method, owner, record) { owner.send(callback, record) }
+ when Proc
+ ->(method, owner, record) { callback.call(owner, record) }
+ else
+ ->(method,owner,record) { callback.send(method, owner, record) }
+ end
+ end
+ model.send "#{full_callback_name}=", callbacks
end
# Defines the setter and getter methods for the collection_singular_ids.
View
9 activerecord/lib/active_record/associations/collection_association.rb
@@ -510,14 +510,7 @@ def concat_records(records)
def callback(method, record)
callbacks_for(method).each do |callback|
- case callback
- when Symbol
- owner.send(callback, record)
- when Proc
- callback.call(owner, record)
- else
- callback.send(method, owner, record)
- end
+ callback.call(method, owner, record)
end
end
View
4 activerecord/test/cases/associations_test.rb
@@ -278,7 +278,7 @@ class DifferentPeopleList < PeopleList
def test_habtm_association_redefinition_callbacks_should_differ_and_not_inherited
# redeclared association on AR descendant should not inherit callbacks from superclass
callbacks = PeopleList.before_add_for_has_and_belongs_to_many
- assert_equal([:enlist], callbacks)
+ assert_equal(1, callbacks.length)
callbacks = DifferentPeopleList.before_add_for_has_and_belongs_to_many
assert_equal([], callbacks)
end
@@ -286,7 +286,7 @@ def test_habtm_association_redefinition_callbacks_should_differ_and_not_inherite
def test_has_many_association_redefinition_callbacks_should_differ_and_not_inherited
# redeclared association on AR descendant should not inherit callbacks from superclass
callbacks = PeopleList.before_add_for_has_many
- assert_equal([:enlist], callbacks)
+ assert_equal(1, callbacks.length)
callbacks = DifferentPeopleList.before_add_for_has_many
assert_equal([], callbacks)
end
Please sign in to comment.
Something went wrong with that request. Please try again.