aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorNick Howard <ndh@baroquebobcat.com>2011-05-01 16:22:46 -0600
committerNick Howard <ndh@baroquebobcat.com>2011-05-01 16:22:56 -0600
commit86ea94e4d0c228c79b3709f0c667ba90b02e41cd (patch)
tree1d2c41554c5a8b8f8cd1120d68b380fe5beb703c /activerecord/test
parentb8ccd0552473fbe0f346334e37b7d84481dd3533 (diff)
downloadrails-86ea94e4d0c228c79b3709f0c667ba90b02e41cd.tar.gz
rails-86ea94e4d0c228c79b3709f0c667ba90b02e41cd.tar.bz2
rails-86ea94e4d0c228c79b3709f0c667ba90b02e41cd.zip
Fix for lighthouse #6741
- adds tests for find_or_create_by and find_or_initialize_by on has_many associations - changes the behavior of ActiveRecord::Associations::CollectionProxy#method_missing to differ to ActiveRecord::FinderMethods#find_or_instantiator_by_attributes for arg processing and saving so find_or_create_by's api on associations will be consistent w/ the api for model classes.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 007f11b535..247decc67b 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -605,6 +605,30 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
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
force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)