Merge pull request #358 from baroquebobcat/fix_find_or_create_master
Fix for lighthouse #6741
This commit is contained in:
commit
f098c80947
@ -64,9 +64,12 @@ def respond_to?(*args)
|
|||||||
|
|
||||||
def method_missing(method, *args, &block)
|
def method_missing(method, *args, &block)
|
||||||
match = DynamicFinderMatch.match(method)
|
match = DynamicFinderMatch.match(method)
|
||||||
if match && match.creator?
|
if match && match.instantiator?
|
||||||
attributes = match.attribute_names
|
record = send(:find_or_instantiator_by_attributes, match, match.attribute_names, *args) do |r|
|
||||||
return send(:"find_by_#{attributes.join('_and_')}", *args) || create(Hash[attributes.zip(args)])
|
@association.send :set_owner_attributes, r
|
||||||
|
@association.send :add_to_target, r
|
||||||
|
yield(r) if block_given?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if target.respond_to?(method) || (!@association.klass.respond_to?(method) && Class.respond_to?(method))
|
if target.respond_to?(method) || (!@association.klass.respond_to?(method) && Class.respond_to?(method))
|
||||||
|
@ -605,6 +605,30 @@ def test_find_or_create_updates_size
|
|||||||
assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
|
assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_find_or_initialize_updates_collection_size
|
||||||
|
number_of_clients = companies(:first_firm).clients_of_firm.size
|
||||||
|
companies(:first_firm).clients_of_firm.find_or_initialize_by_name("name" => "Another Client")
|
||||||
|
assert_equal number_of_clients + 1, companies(:first_firm).clients_of_firm.size
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_find_or_create_with_hash
|
||||||
|
post = authors(:david).posts.find_or_create_by_title(:title => 'Yet another post', :body => 'somebody')
|
||||||
|
assert_equal post, authors(:david).posts.find_or_create_by_title(:title => 'Yet another post', :body => 'somebody')
|
||||||
|
assert post.persisted?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_find_or_create_with_one_attribute_followed_by_hash
|
||||||
|
post = authors(:david).posts.find_or_create_by_title('Yet another post', :body => 'somebody')
|
||||||
|
assert_equal post, authors(:david).posts.find_or_create_by_title('Yet another post', :body => 'somebody')
|
||||||
|
assert post.persisted?
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_find_or_create_should_work_with_block
|
||||||
|
post = authors(:david).posts.find_or_create_by_title('Yet another post') {|p| p.body = 'somebody'}
|
||||||
|
assert_equal post, authors(:david).posts.find_or_create_by_title('Yet another post') {|p| p.body = 'somebody'}
|
||||||
|
assert post.persisted?
|
||||||
|
end
|
||||||
|
|
||||||
def test_deleting
|
def test_deleting
|
||||||
force_signal37_to_load_all_clients_of_firm
|
force_signal37_to_load_all_clients_of_firm
|
||||||
companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
|
companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
|
||||||
|
Loading…
Reference in New Issue
Block a user