From a8ede3664055f33c102b3f229cf280b0bf69c540 Mon Sep 17 00:00:00 2001 From: Dan Erikson Date: Mon, 8 Apr 2013 00:41:16 -0600 Subject: Changed ActiveRecord::Associations::CollectionProxy#select to take multiple arguments. This makes the arguments the same as ActiveRecord::QueryMethods::select. --- activerecord/test/cases/associations/has_many_associations_test.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 781b87741d..7c50c18763 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -523,7 +523,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end def test_select_query_method - assert_equal ['id'], posts(:welcome).comments.select(:id).first.attributes.keys + assert_equal ['id', 'body'], posts(:welcome).comments.select(:id, :body).first.attributes.keys + end + + def test_select_with_block + assert_equal [1], posts(:welcome).comments.select { |c| c.id == 1 }.map(&:id) end def test_adding -- cgit v1.2.3 From ef99c1147592e91bb256952986470592ea0e5f6c Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 21 May 2013 17:59:37 +0200 Subject: Fix the `:primary_key` option for `has_many` associations. When removing records from a `has_many` association it used the `primary_key` defined on the association. Our test suite didn't fail because on all occurences of `:primary_key`, the specified column was available in both tables. This prevented the code from raising an exception but it still behaved badly. I added a test-case to prevent regressions that failed with: ``` 1) Error: HasManyAssociationsTest#test_has_many_assignment_with_custom_primary_key: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: essays.first_name: UPDATE "essays" SET "writer_id" = NULL WHERE "essays"."writer_id" = ? AND "essays"."first_name" IS NULL ``` --- .../test/cases/associations/has_many_associations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index d85570236f..058c812dca 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1472,6 +1472,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal david.essays, Essay.where(writer_id: "David") end + def test_has_many_assignment_with_custom_primary_key + david = people(:david) + + assert_equal ["A Modest Proposal"], david.essays.map(&:name) + david.essays = [Essay.create!(name: "Remote Work" )] + assert_equal ["Remote Work"], david.essays.map(&:name) + end + def test_blank_custom_primary_key_on_new_record_should_not_run_queries author = Author.new assert !author.essays.loaded? -- cgit v1.2.3 From aff928bacfb26f164c3f0ccbee7bcfa8228941cb Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Mon, 27 May 2013 16:52:42 +0200 Subject: `implicit_readonly` is being removed in favor of calling `readonly` explicitly --- .../test/cases/associations/inner_join_association_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inner_join_association_test.rb b/activerecord/test/cases/associations/inner_join_association_test.rb index 918783e8f1..9baf94399a 100644 --- a/activerecord/test/cases/associations/inner_join_association_test.rb +++ b/activerecord/test/cases/associations/inner_join_association_test.rb @@ -46,10 +46,10 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase assert_equal 2, authors.count end - def test_find_with_implicit_inner_joins_honors_readonly_without_select - authors = Author.joins(:posts).to_a - assert !authors.empty?, "expected authors to be non-empty" - assert authors.all? {|a| a.readonly? }, "expected all authors to be readonly" + def test_find_with_implicit_inner_joins_without_select_does_not_imply_readonly + authors = Author.joins(:posts) + assert_not authors.empty?, "expected authors to be non-empty" + assert authors.none? {|a| a.readonly? }, "expected no authors to be readonly" end def test_find_with_implicit_inner_joins_honors_readonly_with_select -- cgit v1.2.3 From 5d75579eec90e4fe0f3d33adbf5e7065a7cdcfa3 Mon Sep 17 00:00:00 2001 From: kennyj Date: Sat, 1 Jun 2013 21:36:50 +0900 Subject: Remove #sum with a block was deprecated. --- activerecord/test/cases/associations/has_many_associations_test.rb | 6 ------ 1 file changed, 6 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 058c812dca..9f64ecd845 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1747,12 +1747,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_deprecated { klass.has_many :foo, :counter_sql => 'lol' } end - test "sum calculation with block for array compatibility is deprecated" do - assert_deprecated do - posts(:welcome).comments.sum { |c| c.id } - end - end - test "has many associations on new records use null relations" do post = Post.new -- cgit v1.2.3 From 743ee7b2186711b44cae89cfbb43cdbde0ca2895 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Sun, 2 Jun 2013 23:52:33 +0530 Subject: wonderfull => wonderful --- activerecord/test/cases/associations/belongs_to_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 87af24cbe6..95896971a8 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -338,7 +338,7 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase topic.replies.create!(:title => "re: 37s", :content => "rails") assert_equal 1, Topic.find(topic.id)[:replies_count] - topic.update_columns(content: "rails is wonderfull") + topic.update_columns(content: "rails is wonderful") assert_equal 1, Topic.find(topic.id)[:replies_count] end -- cgit v1.2.3 From e9177816ff9138aa9260f9fe216e1b424177dc0d Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Thu, 9 May 2013 17:34:58 +0530 Subject: Make test name descriptive and add reference to original regression commit --- activerecord/test/cases/associations/eager_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 4aa6567d85..1cfaf552af 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -250,7 +250,8 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_nil Post.all.merge!(:includes => :author).find(posts(:authorless).id).author end - def test_nested_loading_with_no_associations + # Regression test for 21c75e5 + def test_nested_loading_does_not_raise_exception_when_association_does_not_exist assert_nothing_raised do Post.all.merge!(:includes => {:author => :author_addresss}).find(posts(:authorless).id) end -- cgit v1.2.3 From 394cc6047df090233927fd1eaf21197e84d7aa37 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 18 Jun 2013 08:37:00 +0200 Subject: `CollectionProxy#include?` returns `true` and `false` as documented. --- .../associations/has_many_associations_test.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 9f64ecd845..168ce13097 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -418,7 +418,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase client_ary = firm.clients_using_finder_sql.find("2", "3") assert_kind_of Array, client_ary assert_equal 2, client_ary.size - assert client_ary.include?(client) + assert_equal true, client_ary.include?(client) end def test_find_all @@ -1220,14 +1220,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end def test_included_in_collection - assert companies(:first_firm).clients.include?(Client.find(2)) + assert_equal true, companies(:first_firm).clients.include?(Client.find(2)) end def test_included_in_collection_for_new_records client = Client.create(:name => 'Persisted') assert_nil client.client_of - assert !Firm.new.clients_of_firm.include?(client), - 'includes a client that does not belong to any firm' + assert_equal false, Firm.new.clients_of_firm.include?(client), + 'includes a client that does not belong to any firm' end def test_adding_array_and_collection @@ -1254,7 +1254,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase firm.save firm.reload assert_equal 2, firm.clients.length - assert !firm.clients.include?(:first_client) + assert_equal false, firm.clients.include?(:first_client) end def test_replace_failure @@ -1332,7 +1332,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase firm.save! assert_equal 2, firm.clients(true).size - assert firm.clients.include?(companies(:second_client)) + assert_equal true, firm.clients.include?(companies(:second_client)) end def test_get_ids_for_through @@ -1366,7 +1366,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_no_queries do assert firm.clients.loaded? - assert firm.clients.include?(client) + assert_equal true, firm.clients.include?(client) end end @@ -1377,7 +1377,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase firm.reload assert ! firm.clients.loaded? assert_queries(1) do - assert firm.clients.include?(client) + assert_equal true, firm.clients.include?(client) end assert ! firm.clients.loaded? end @@ -1388,7 +1388,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase firm.reload assert ! firm.clients_using_sql.loaded? - assert firm.clients_using_sql.include?(client) + assert_equal true, firm.clients_using_sql.include?(client) assert firm.clients_using_sql.loaded? end @@ -1398,7 +1398,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase client = Client.create!(:name => 'Not Associated') assert ! firm.clients.loaded? - assert ! firm.clients.include?(client) + assert_equal false, firm.clients.include?(client) end def test_calling_first_or_last_on_association_should_not_load_association @@ -1613,7 +1613,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_include_method_in_has_many_association_should_return_true_for_instance_added_with_build post = Post.new comment = post.comments.build - assert post.comments.include?(comment) + assert_equal true, post.comments.include?(comment) end def test_load_target_respects_protected_attributes -- cgit v1.2.3 From 2b73f780ffa52baba09511b2db753f0fde574c14 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sat, 11 May 2013 00:34:25 -0400 Subject: do not load all child records for inverse case currently `post.comments.find(Comment.first.id)` would load all comments for the given post to set the inverse association. This has a huge performance penalty. Because if post has 100k records and all these 100k records would be loaded in memory even though the comment id was supplied. Fix is to use in-memory records only if loaded? is true. Otherwise load the records using full sql. Fixes #10509 --- activerecord/test/cases/associations/inverse_associations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index b1f0be3204..993e7294cf 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -401,6 +401,14 @@ class InverseHasManyTests < ActiveRecord::TestCase assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match after the child name is changed" end + def test_find_on_child_instance_with_id_should_not_load_all_child_records + man = Man.create! + interest = Interest.create!(man: man) + + man.interests.find(interest.id) + refute man.interests.loaded? + end + def test_raise_record_not_found_error_when_invalid_ids_are_passed man = Man.create! -- cgit v1.2.3 From e47b6dee858e62dceba867dd160b968d679c82e8 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Wed, 19 Jun 2013 14:22:02 +0100 Subject: Revert "Merge pull request #10566 from neerajdotname/10509d" This reverts commit 2b817a5e89ac0e7aeb894a40ae7151a0cf3cef16, reversing changes made to 353a398bee68c5ea99d76ac7601de0a5fef6f4a5. Conflicts: activerecord/CHANGELOG.md Reason: the build broke --- activerecord/test/cases/associations/inverse_associations_test.rb | 8 -------- 1 file changed, 8 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index 993e7294cf..b1f0be3204 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -401,14 +401,6 @@ class InverseHasManyTests < ActiveRecord::TestCase assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match after the child name is changed" end - def test_find_on_child_instance_with_id_should_not_load_all_child_records - man = Man.create! - interest = Interest.create!(man: man) - - man.interests.find(interest.id) - refute man.interests.loaded? - end - def test_raise_record_not_found_error_when_invalid_ids_are_passed man = Man.create! -- cgit v1.2.3 From 6741a0a18761ca9cbbdaeefe70f4d53d68fdf1b5 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Fri, 21 Jun 2013 20:59:04 +0530 Subject: fix bad test by making number that fits for integer PR https://github.com/rails/rails/pull/10566 had to be reverted because after applying the fix test "test_raise_record_not_found_error_when_invalid_ids_are_passed" started failing. In this test invalid_id is being assigned a really large number which was causing following failure when PR #10566 was applied. ``` RangeError: bignum too big to convert into `long long' SELECT `interests`.* FROM `interests` WHERE `interests`.`man_id` = ? AND `interests`.`id` = ? LIMIT 1 [["man_id", 970345987], ["id", 2394823094892348920348523452345]] ``` This test is not failing in master because when test code `man.interests.find(invalid_id)` is executed then interests are fully loaded in memory and no database query is executed. After PR #10566 was merged then test code `man.interests.find(invalid_id)` started executing sql query and hence the error. In case someone is wondering why the second part of query is not failing, then that's because the actual query does not require any variable substituation where the number is large. In that case the sql generate is following. ``` SELECT `interests`.* FROM `interests` WHERE `interests`.`man_id` = ? AND `interests`.`id` IN (8432342, 2390102913, 2453245234523452) [["man_id", 970345987]] ``` --- activerecord/test/cases/associations/inverse_associations_test.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index b1f0be3204..d961ceca1d 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -402,9 +402,13 @@ class InverseHasManyTests < ActiveRecord::TestCase end def test_raise_record_not_found_error_when_invalid_ids_are_passed + # delete all interest records to ensure that hard coded invalid_id(s) + # are indeed invalid. + Interest.delete_all + man = Man.create! - invalid_id = 2394823094892348920348523452345 + invalid_id = 245324523 assert_raise(ActiveRecord::RecordNotFound) { man.interests.find(invalid_id) } invalid_ids = [8432342, 2390102913, 2453245234523452] -- cgit v1.2.3 From 82882d4162c534e9aeef629cbbd7b5f84f45ee12 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sat, 11 May 2013 00:34:25 -0400 Subject: do not load all child records for inverse case currently `post.comments.find(Comment.first.id)` would load all comments for the given post to set the inverse association. This has a huge performance penalty. Because if post has 100k records and all these 100k records would be loaded in memory even though the comment id was supplied. Fix is to use in-memory records only if loaded? is true. Otherwise load the records using full sql. Fixes #10509 --- activerecord/test/cases/associations/inverse_associations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index d961ceca1d..71cf1237e8 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -401,6 +401,14 @@ class InverseHasManyTests < ActiveRecord::TestCase assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match after the child name is changed" end + def test_find_on_child_instance_with_id_should_not_load_all_child_records + man = Man.create! + interest = Interest.create!(man: man) + + man.interests.find(interest.id) + refute man.interests.loaded? + end + def test_raise_record_not_found_error_when_invalid_ids_are_passed # delete all interest records to ensure that hard coded invalid_id(s) # are indeed invalid. -- cgit v1.2.3 From 951bde4557cde3e1da8a5545031498c97a4e03f0 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sat, 22 Jun 2013 14:11:12 +0200 Subject: test-case to prevent regressions on `Association#build` with an Array. Closes #11026 --- .../test/cases/associations/has_many_associations_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 168ce13097..d3704474a3 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -176,6 +176,16 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_raise(ActiveRecord::SubclassNotFound) { firm.companies.build(:type => "Account") } end + test "building the association with an array" do + speedometer = Speedometer.new(speedometer_id: "a") + data = [{name: "first"}, {name: "second"}] + speedometer.minivans.build(data) + + assert_equal 2, speedometer.minivans.size + assert speedometer.save + assert_equal ["first", "second"], speedometer.reload.minivans.map(&:name) + end + def test_association_keys_bypass_attribute_protection car = Car.create(:name => 'honda') -- cgit v1.2.3 From f6cdc2282ff4a34e4d0230b80d945f9c898d6973 Mon Sep 17 00:00:00 2001 From: Jared Armstrong Date: Mon, 24 Jun 2013 08:52:08 +0200 Subject: test-case to prevent regressions described in #10901. --- .../associations/has_many_associations_test.rb | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index d3704474a3..acb92bdbd0 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1336,6 +1336,33 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [companies(:second_client).id, companies(:first_client).id], companies(:first_firm).clients_ordered_by_name_ids end + def test_get_ids_for_association_on_new_record_does_not_try_to_find_records + Company.columns # Load schema information so we don't query below + Contract.columns # if running just this test. + + company = Company.new + assert_queries(0) do + company.contract_ids + end + + assert_equal [], company.contract_ids + end + + def test_set_ids_for_association_on_new_record_applies_association_correctly + contract_a = Contract.create! + contract_b = Contract.create! + another_contract = Contract.create! + company = Company.new(:name => "Some Company") + + company.contract_ids = [contract_a.id, contract_b.id] + assert_equal [contract_a.id, contract_b.id], company.contract_ids + assert_equal [contract_a, contract_b], company.contracts + + company.save! + assert_equal company, contract_a.reload.company + assert_equal company, contract_b.reload.company + end + def test_assign_ids_ignoring_blanks firm = Firm.create!(:name => 'Apple') firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, ''] -- cgit v1.2.3 From 9d474d4885a72a0bbebbb1a0515a7388223d9ad9 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Mon, 24 Jun 2013 20:28:15 +0530 Subject: Fix `another_contract` not being used warning --- activerecord/test/cases/associations/has_many_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index acb92bdbd0..8f5e18b863 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1351,7 +1351,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_set_ids_for_association_on_new_record_applies_association_correctly contract_a = Contract.create! contract_b = Contract.create! - another_contract = Contract.create! + Contract.create! # another contract company = Company.new(:name => "Some Company") company.contract_ids = [contract_a.id, contract_b.id] -- cgit v1.2.3 From 3cc7223f3d57f31affdbabccc86cbc8b6589e2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Strza=C5=82kowski?= Date: Thu, 27 Jun 2013 19:56:30 +0200 Subject: Remove depreacted finders They were deprecated in 4.0, planned to remove in 4.1 --- activerecord/test/cases/associations/eager_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 1cfaf552af..846ef05c7d 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -554,9 +554,9 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal 2, posts.size count = ActiveSupport::Deprecation.silence do - Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ]) + Post.includes(:author, :comments).limit(2).references(:author).where("authors.name = ?", 'David').count end - assert_equal count, posts.size + assert_equal posts.size, count end def test_eager_with_has_many_and_limit_and_high_offset -- cgit v1.2.3 From 55193e449a377c448e43d8fec42899ea1ff93b27 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 19 Apr 2013 12:52:44 +0100 Subject: Apply default scope when joining associations. For example: class Post < ActiveRecord::Base default_scope -> { where published: true } end class Comment belongs_to :post end When calling `Comment.join(:post)`, we expect to receive only comments on published posts, since that is the default scope for posts. Before this change, the default scope from `Post` was not applied, so we'd get comments on unpublished posts. --- .../test/cases/associations/inner_join_association_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inner_join_association_test.rb b/activerecord/test/cases/associations/inner_join_association_test.rb index 9baf94399a..de47a576c6 100644 --- a/activerecord/test/cases/associations/inner_join_association_test.rb +++ b/activerecord/test/cases/associations/inner_join_association_test.rb @@ -104,4 +104,12 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase assert !posts(:welcome).tags.empty? assert Post.joins(:misc_tags).where(:id => posts(:welcome).id).empty? end + + test "the default scope of the target is applied when joining associations" do + author = Author.create! name: "Jon" + author.categorizations.create! + author.categorizations.create! special: true + + assert_equal [author], Author.where(id: author).joins(:special_categorizations) + end end -- cgit v1.2.3 From 22b3481ba2aa55fad1f9a5db94072312b345fb55 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 27 Jun 2013 21:46:24 +0200 Subject: remove deprecated implicit join references. --- activerecord/test/cases/associations/eager_test.rb | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 846ef05c7d..28bf48f4fd 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -346,9 +346,7 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name assert_nothing_raised do - ActiveSupport::Deprecation.silence do - Comment.all.merge!(:includes => :post, :where => ['posts.id = ?',4]).to_a - end + Comment.includes(:post).references(:posts).where('posts.id = ?', 4) end end @@ -367,9 +365,7 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id') assert_nothing_raised do - ActiveSupport::Deprecation.silence do - Comment.all.merge!(:includes => :post, :where => ["#{quoted_posts_id} = ?",4]).to_a - end + Comment.includes(:post).references(:posts).where("#{quoted_posts_id} = ?", 4) end end @@ -382,9 +378,7 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id') assert_nothing_raised do - ActiveSupport::Deprecation.silence do - Comment.all.merge!(:includes => :post, :order => quoted_posts_id).to_a - end + Comment.includes(:post).references(:posts).order(quoted_posts_id) end end @@ -548,14 +542,10 @@ class EagerAssociationTest < ActiveRecord::TestCase end def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers - posts = ActiveSupport::Deprecation.silence do - Post.all.merge!(:includes => [ :author, :comments ], :limit => 2, :where => [ "authors.name = ?", 'David' ]).to_a - end + posts = Post.includes(:author, :comments).limit(2).references(:author).where("authors.name = ?", 'David') assert_equal 2, posts.size - count = ActiveSupport::Deprecation.silence do - Post.includes(:author, :comments).limit(2).references(:author).where("authors.name = ?", 'David').count - end + count = Post.includes(:author, :comments).limit(2).references(:author).where("authors.name = ?", 'David').count assert_equal posts.size, count end -- cgit v1.2.3 From f319e4a9421d8815717fd6ca6191268fed9e536d Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Mon, 13 May 2013 17:10:23 -0400 Subject: Do not invoke callbacks when delete_all is called Method `delete_all` should not be invoking callbacks and this feature was deprecated in Rails 4.0. This is being removed. `delete_all` will continue to honor the `:dependent` option. However if `:dependent` value is `:destroy` then the default deletion strategy for that collection will be applied. User can also force a deletion strategy by passing parameter to `delete_all`. For example you can do `@post.comments.delete_all(:nullify)` --- .../associations/has_many_associations_test.rb | 27 +++++++++++++++-- .../has_many_through_associations_test.rb | 34 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 8f5e18b863..fc40f95e26 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -148,6 +148,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 'defaulty', bulb.name end + def test_do_not_call_callbacks_for_delete_all + bulb_count = Bulb.count + car = Car.create(:name => 'honda') + car.funky_bulbs.create! + assert_nothing_raised { car.reload.funky_bulbs.delete_all } + assert_equal bulb_count + 1, Bulb.count, "bulbs should have been deleted using :nullify strategey" + end + def test_building_the_associated_object_with_implicit_sti_base_class firm = DependentFirm.new company = firm.companies.build @@ -930,18 +938,33 @@ class HasManyAssociationsTest < ActiveRecord::TestCase firm = companies(:first_firm) client_id = firm.dependent_clients_of_firm.first.id assert_equal 1, firm.dependent_clients_of_firm.size + assert_equal 1, Client.find_by_id(client_id).client_of - # :dependent means destroy is called on each client + # :nullify is called on each client firm.dependent_clients_of_firm.clear assert_equal 0, firm.dependent_clients_of_firm.size assert_equal 0, firm.dependent_clients_of_firm(true).size - assert_equal [client_id], Client.destroyed_client_ids[firm.id] + assert_equal [], Client.destroyed_client_ids[firm.id] # Should be destroyed since the association is dependent. + assert_nil Client.find_by_id(client_id).client_of + end + + def test_delete_all_with_option_delete_all + firm = companies(:first_firm) + client_id = firm.dependent_clients_of_firm.first.id + firm.dependent_clients_of_firm.delete_all(:delete_all) assert_nil Client.find_by_id(client_id) end + def test_delete_all_accepts_limited_parameters + firm = companies(:first_firm) + assert_raise(ArgumentError) do + firm.dependent_clients_of_firm.delete_all(:destroy) + end + end + def test_clearing_an_exclusively_dependent_association_collection firm = companies(:first_firm) client_id = firm.exclusively_dependent_clients_of_firm.first.id diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 70c6b489aa..ee88fd490a 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -57,6 +57,40 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert post.reload.people(true).include?(person) end + def test_delete_all_for_with_dependent_option_destroy + person = people(:david) + assert_equal 1, person.jobs_with_dependent_destroy.count + + assert_no_difference 'Job.count' do + assert_difference 'Reference.count', -1 do + person.reload.jobs_with_dependent_destroy.delete_all + end + end + end + + def test_delete_all_for_with_dependent_option_nullify + person = people(:david) + assert_equal 1, person.jobs_with_dependent_nullify.count + + assert_no_difference 'Job.count' do + assert_no_difference 'Reference.count' do + person.reload.jobs_with_dependent_nullify.delete_all + end + end + end + + def test_delete_all_for_with_dependent_option_delete_all + person = people(:david) + assert_equal 1, person.jobs_with_dependent_delete_all.count + + assert_no_difference 'Job.count' do + assert_difference 'Reference.count', -1 do + person.reload.jobs_with_dependent_delete_all.delete_all + end + end + end + + def test_associate_existing_record_twice_should_add_to_target_twice post = posts(:thinking) person = people(:david) -- cgit v1.2.3 From a8c888cca93021b71c887f5d4a01936a9e28389f Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 2 Jul 2013 01:07:02 +0530 Subject: Removed deprecated options for assocations Deprecated options `delete_sql`, `insert_sql`, `finder_sql` and `counter_sql` have been deleted. --- .../has_and_belongs_to_many_associations_test.rb | 12 ------------ .../test/cases/associations/has_many_associations_test.rb | 10 ---------- 2 files changed, 22 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 84bdca3a97..e23780174b 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -845,18 +845,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert project.developers.include?(developer) end - test ":insert_sql is deprecated" do - klass = Class.new(ActiveRecord::Base) - def klass.name; 'Foo'; end - assert_deprecated { klass.has_and_belongs_to_many :posts, :insert_sql => 'lol' } - end - - test ":delete_sql is deprecated" do - klass = Class.new(ActiveRecord::Base) - def klass.name; 'Foo'; end - assert_deprecated { klass.has_and_belongs_to_many :posts, :delete_sql => 'lol' } - end - test "has and belongs to many associations on new records use null relations" do projects = Developer.new.projects assert_no_queries do diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 8f5e18b863..8843ef6d2e 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1774,16 +1774,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end - test ":finder_sql is deprecated" do - klass = Class.new(ActiveRecord::Base) - assert_deprecated { klass.has_many :foo, :finder_sql => 'lol' } - end - - test ":counter_sql is deprecated" do - klass = Class.new(ActiveRecord::Base) - assert_deprecated { klass.has_many :foo, :counter_sql => 'lol' } - end - test "has many associations on new records use null relations" do post = Post.new -- cgit v1.2.3 From 4bf1ecd6d8ce38e63a0be81287b07f402ce8b5c8 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 2 Jul 2013 12:24:12 +0530 Subject: Removed support for deprecated `counter_sql` --- .../has_and_belongs_to_many_associations_test.rb | 21 ------------------- .../associations/has_many_associations_test.rb | 24 ---------------------- 2 files changed, 45 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index e23780174b..7862fe3bf4 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -65,19 +65,6 @@ class DeveloperWithSymbolsForKeys < ActiveRecord::Base :foreign_key => "developer_id" end -class DeveloperWithCounterSQL < ActiveRecord::Base - self.table_name = 'developers' - - ActiveSupport::Deprecation.silence do - has_and_belongs_to_many :projects, - :class_name => "DeveloperWithCounterSQL", - :join_table => "developers_projects", - :association_foreign_key => "project_id", - :foreign_key => "developer_id", - :counter_sql => proc { "SELECT COUNT(*) AS count_all FROM projects INNER JOIN developers_projects ON projects.id = developers_projects.project_id WHERE developers_projects.developer_id =#{id}" } - end -end - class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects, :parrots, :pirates, :parrots_pirates, :treasures, :price_estimates, :tags, :taggings @@ -791,14 +778,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, david.projects.count end - def test_count_with_counter_sql - developer = DeveloperWithCounterSQL.create(:name => 'tekin') - developer.project_ids = [projects(:active_record).id] - developer.save - developer.reload - assert_equal 1, developer.projects.count - end - unless current_adapter?(:PostgreSQLAdapter) def test_count_with_finder_sql assert_equal 3, projects(:active_record).developers_with_finder_sql.count diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 582cde26ed..42e140a831 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -36,19 +36,6 @@ class HasManyAssociationsTestForCountWithFinderSql < ActiveRecord::TestCase end end -class HasManyAssociationsTestForCountWithCountSql < ActiveRecord::TestCase - class Invoice < ActiveRecord::Base - ActiveSupport::Deprecation.silence do - has_many :custom_line_items, :class_name => 'LineItem', :counter_sql => "SELECT COUNT(*) line_items.* from line_items" - end - end - def test_should_fail - assert_raise(ArgumentError) do - Invoice.create.custom_line_items.count(:conditions => {:amount => 0}) - end - end -end - class HasManyAssociationsTestForCountWithVariousFinderSqls < ActiveRecord::TestCase class Invoice < ActiveRecord::Base ActiveSupport::Deprecation.silence do @@ -381,17 +368,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal client, firm.clients_using_sql.find(client.id, client.id.to_s) end - def test_counting_using_sql - assert_equal 1, Firm.order("id").first.clients_using_counter_sql.size - assert Firm.order("id").first.clients_using_counter_sql.any? - assert_equal 0, Firm.order("id").first.clients_using_zero_counter_sql.size - assert !Firm.order("id").first.clients_using_zero_counter_sql.any? - end - - def test_counting_non_existant_items_using_sql - assert_equal 0, Firm.order("id").first.no_clients_using_counter_sql.size - end - def test_counting_using_finder_sql assert_equal 2, Firm.find(4).clients_using_sql.count end -- cgit v1.2.3 From d80334488ff5c83e63cca3fb70250e9002a2f0c3 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 2 Jul 2013 21:56:51 +0530 Subject: Removed support for deprecated `finder_sql` in associations. --- .../has_and_belongs_to_many_associations_test.rb | 26 ----- .../associations/has_many_associations_test.rb | 114 --------------------- 2 files changed, 140 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 7862fe3bf4..3ff1aeb7fe 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -524,25 +524,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert ! project.developers.include?(developer) end - def test_find_in_association_with_custom_finder_sql - assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id), "SQL find" - - active_record = projects(:active_record) - active_record.developers_with_finder_sql.reload - assert_equal developers(:david), active_record.developers_with_finder_sql.find(developers(:david).id), "Ruby find" - end - - def test_find_in_association_with_custom_finder_sql_and_multiple_interpolations - # interpolate once: - assert_equal [developers(:david), developers(:jamis), developers(:poor_jamis)], projects(:active_record).developers_with_finder_sql, "first interpolation" - # interpolate again, for a different project id - assert_equal [developers(:david)], projects(:action_controller).developers_with_finder_sql, "second interpolation" - end - - def test_find_in_association_with_custom_finder_sql_and_string_id - assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id.to_s), "SQL find" - end - def test_find_with_merged_options assert_equal 1, projects(:active_record).limited_developers.size assert_equal 1, projects(:active_record).limited_developers.to_a.size @@ -778,13 +759,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, david.projects.count end - unless current_adapter?(:PostgreSQLAdapter) - def test_count_with_finder_sql - assert_equal 3, projects(:active_record).developers_with_finder_sql.count - assert_equal 3, projects(:active_record).developers_with_multiline_finder_sql.count - end - end - def test_association_proxy_transaction_method_starts_transaction_in_association_class Post.expects(:transaction) Category.first.posts.transaction do diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 42e140a831..3187156db6 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -23,66 +23,6 @@ require 'models/categorization' require 'models/minivan' require 'models/speedometer' -class HasManyAssociationsTestForCountWithFinderSql < ActiveRecord::TestCase - class Invoice < ActiveRecord::Base - ActiveSupport::Deprecation.silence do - has_many :custom_line_items, :class_name => 'LineItem', :finder_sql => "SELECT line_items.* from line_items" - end - end - def test_should_fail - assert_raise(ArgumentError) do - Invoice.create.custom_line_items.count(:conditions => {:amount => 0}) - end - end -end - -class HasManyAssociationsTestForCountWithVariousFinderSqls < ActiveRecord::TestCase - class Invoice < ActiveRecord::Base - ActiveSupport::Deprecation.silence do - has_many :custom_line_items, :class_name => 'LineItem', :finder_sql => "SELECT DISTINCT line_items.amount from line_items" - has_many :custom_full_line_items, :class_name => 'LineItem', :finder_sql => "SELECT line_items.invoice_id, line_items.amount from line_items" - has_many :custom_star_line_items, :class_name => 'LineItem', :finder_sql => "SELECT * from line_items" - has_many :custom_qualified_star_line_items, :class_name => 'LineItem', :finder_sql => "SELECT line_items.* from line_items" - end - end - - def test_should_count_distinct_results - invoice = Invoice.new - invoice.custom_line_items << LineItem.new(:amount => 0) - invoice.custom_line_items << LineItem.new(:amount => 0) - invoice.save! - - assert_equal 1, invoice.custom_line_items.count - end - - def test_should_count_results_with_multiple_fields - invoice = Invoice.new - invoice.custom_full_line_items << LineItem.new(:amount => 0) - invoice.custom_full_line_items << LineItem.new(:amount => 0) - invoice.save! - - assert_equal 2, invoice.custom_full_line_items.count - end - - def test_should_count_results_with_star - invoice = Invoice.new - invoice.custom_star_line_items << LineItem.new(:amount => 0) - invoice.custom_star_line_items << LineItem.new(:amount => 0) - invoice.save! - - assert_equal 2, invoice.custom_star_line_items.count - end - - def test_should_count_results_with_qualified_star - invoice = Invoice.new - invoice.custom_qualified_star_line_items << LineItem.new(:amount => 0) - invoice.custom_qualified_star_line_items << LineItem.new(:amount => 0) - invoice.save! - - assert_equal 2, invoice.custom_qualified_star_line_items.count - end -end - class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase fixtures :authors, :posts, :comments @@ -352,26 +292,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal "Summit", Firm.all.merge!(:order => "id").first.clients_using_primary_key.first.name end - def test_finding_using_sql - firm = Firm.order("id").first - first_client = firm.clients_using_sql.first - assert_not_nil first_client - assert_equal "Microsoft", first_client.name - assert_equal 1, firm.clients_using_sql.size - assert_equal 1, Firm.order("id").first.clients_using_sql.size - end - - def test_finding_using_sql_take_into_account_only_uniq_ids - firm = Firm.order("id").first - client = firm.clients_using_sql.first - assert_equal client, firm.clients_using_sql.find(client.id, client.id) - assert_equal client, firm.clients_using_sql.find(client.id, client.id.to_s) - end - - def test_counting_using_finder_sql - assert_equal 2, Firm.find(4).clients_using_sql.count - end - def test_belongs_to_sanity c = Client.new assert_nil c.firm @@ -399,22 +319,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) } end - def test_find_string_ids_when_using_finder_sql - firm = Firm.order("id").first - - client = firm.clients_using_finder_sql.find("2") - assert_kind_of Client, client - - client_ary = firm.clients_using_finder_sql.find(["2"]) - assert_kind_of Array, client_ary - assert_equal client, client_ary.first - - client_ary = firm.clients_using_finder_sql.find("2", "3") - assert_kind_of Array, client_ary - assert_equal 2, client_ary.size - assert_equal true, client_ary.include?(client) - end - def test_find_all firm = Firm.all.merge!(:order => "id").first assert_equal 2, firm.clients.where("#{QUOTED_TYPE} = 'Client'").to_a.length @@ -1324,13 +1228,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [readers(:michael_welcome).id], posts(:welcome).readers_with_person_ids end - def test_get_ids_for_unloaded_finder_sql_associations_loads_them - company = companies(:first_firm) - assert !company.clients_using_sql.loaded? - assert_equal [companies(:second_client).id], company.clients_using_sql_ids - assert company.clients_using_sql.loaded? - end - def test_get_ids_for_ordered_association assert_equal [companies(:second_client).id, companies(:first_client).id], companies(:first_firm).clients_ordered_by_name_ids end @@ -1418,17 +1315,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert ! firm.clients.loaded? end - def test_include_loads_collection_if_target_uses_finder_sql - firm = companies(:first_firm) - client = firm.clients_using_sql.first - - firm.reload - assert ! firm.clients_using_sql.loaded? - assert_equal true, firm.clients_using_sql.include?(client) - assert firm.clients_using_sql.loaded? - end - - def test_include_returns_false_for_non_matching_record_to_verify_scoping firm = companies(:first_firm) client = Client.create!(:name => 'Not Associated') -- cgit v1.2.3 From 8ef2463807402f3e81bc5bbfe7206e2314cc79dc Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 3 Jul 2013 00:08:48 +0530 Subject: Removed support for deprecated `delete_sql` in associations. --- .../has_and_belongs_to_many_associations_test.rb | 25 ---------------------- 1 file changed, 25 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 3ff1aeb7fe..0f9af8a0c3 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -351,31 +351,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, david.projects(true).size end - def test_deleting_with_sql - david = Developer.find(1) - active_record = Project.find(1) - active_record.developers.reload - assert_equal 3, active_record.developers_by_sql.size - - active_record.developers_by_sql.delete(david) - assert_equal 2, active_record.developers_by_sql(true).size - end - - def test_deleting_array_with_sql - active_record = Project.find(1) - active_record.developers.reload - assert_equal 3, active_record.developers_by_sql.size - - active_record.developers_by_sql.delete(Developer.all) - assert_equal 0, active_record.developers_by_sql(true).size - end - - def test_deleting_all_with_sql - project = Project.find(1) - project.developers_by_sql.delete_all - assert_equal 0, project.developers_by_sql.size - end - def test_deleting_all david = Developer.find(1) david.projects.reload -- cgit v1.2.3 From 0e3700364c98fe8f766fea8d711795dfe15e7ff0 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 3 Jul 2013 00:46:39 +0530 Subject: Dropped deprecated option `:restrict` for `:dependent` in associations --- .../cases/associations/has_many_associations_test.rb | 15 --------------- .../test/cases/associations/has_one_associations_test.rb | 16 ---------------- 2 files changed, 31 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 3187156db6..9c484a8bbe 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1092,21 +1092,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal num_accounts, Account.count end - def test_restrict - firm = RestrictedFirm.create!(:name => 'restrict') - firm.companies.create(:name => 'child') - - assert !firm.companies.empty? - assert_raise(ActiveRecord::DeleteRestrictionError) { firm.destroy } - assert RestrictedFirm.exists?(:name => 'restrict') - assert firm.companies.exists?(:name => 'child') - end - - def test_restrict_is_deprecated - klass = Class.new(ActiveRecord::Base) - assert_deprecated { klass.has_many :posts, dependent: :restrict } - end - def test_restrict_with_exception firm = RestrictedWithExceptionFirm.create!(:name => 'restrict') firm.companies.create(:name => 'child') diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 0e48fbca9c..4fdf9a9643 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -158,22 +158,6 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_nothing_raised { firm.destroy } end - def test_restrict - firm = RestrictedFirm.create!(:name => 'restrict') - firm.create_account(:credit_limit => 10) - - assert_not_nil firm.account - - assert_raise(ActiveRecord::DeleteRestrictionError) { firm.destroy } - assert RestrictedFirm.exists?(:name => 'restrict') - assert firm.account.present? - end - - def test_restrict_is_deprecated - klass = Class.new(ActiveRecord::Base) - assert_deprecated { klass.has_one :post, dependent: :restrict } - end - def test_restrict_with_exception firm = RestrictedWithExceptionFirm.create!(:name => 'restrict') firm.create_account(:credit_limit => 10) -- cgit v1.2.3 From 1894682cf799500f3a5f966145924fa0660b71c8 Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Fri, 5 Jul 2013 02:06:35 +0300 Subject: Cleanup belongs to tests simplified logic to calculate number of queries by using assert_queries --- activerecord/test/cases/associations/belongs_to_associations_test.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 95896971a8..cc72ab7b2a 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -391,8 +391,7 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase def test_dont_find_target_when_foreign_key_is_null tagging = taggings(:thinking_general) - queries = assert_sql { tagging.super_tag } - assert_equal 0, queries.length + assert_queries(0) { tagging.super_tag } end def test_field_name_same_as_foreign_key -- cgit v1.2.3 From 1ecc3e83b6f62e2f6858c458231e3a49ef0bd916 Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Fri, 5 Jul 2013 01:13:59 +0300 Subject: #11288: Removed duplicated touching if belongs to model with touch option on touch Closes #11288 --- .../associations/belongs_to_associations_test.rb | 43 +++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index cc72ab7b2a..0267cdf6e0 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -1,4 +1,4 @@ -require "cases/helper" +require 'cases/helper' require 'models/developer' require 'models/project' require 'models/company' @@ -14,6 +14,8 @@ require 'models/sponsor' require 'models/member' require 'models/essay' require 'models/toy' +require 'models/invoice' +require 'models/line_item' class BelongsToAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :developers, :projects, :topics, @@ -324,6 +326,45 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_equal 1, Topic.find(topic.id)[:replies_count] end + def test_belongs_to_with_touch_option_on_touch + line_item = LineItem.create! + Invoice.create!(line_items: [line_item]) + + assert_queries(1) { line_item.touch } + end + + def test_belongs_to_with_touch_option_on_touch_and_removed_parent + line_item = LineItem.create! + Invoice.create!(line_items: [line_item]) + + line_item.invoice = nil + + assert_queries(2) { line_item.touch } + end + + def test_belongs_to_with_touch_option_on_update + line_item = LineItem.create! + Invoice.create!(line_items: [line_item]) + + assert_queries(2) { line_item.update amount: 10 } + end + + def test_belongs_to_with_touch_option_on_destroy + line_item = LineItem.create! + Invoice.create!(line_items: [line_item]) + + assert_queries(2) { line_item.destroy } + end + + def test_belongs_to_with_touch_option_on_touch_and_reassigned_parent + line_item = LineItem.create! + Invoice.create!(line_items: [line_item]) + + line_item.invoice = Invoice.create! + + assert_queries(3) { line_item.touch } + end + def test_belongs_to_counter_after_update topic = Topic.create!(title: "37s") topic.replies.create!(title: "re: 37s", content: "rails") -- cgit v1.2.3 From fd2ddb3eb8b7f33ec6319765595e6ede761e9999 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Tue, 9 Jul 2013 11:02:13 +0530 Subject: Remove redundant test about `push_with_attributes` removal. --- .../associations/has_and_belongs_to_many_associations_test.rb | 7 ------- 1 file changed, 7 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 0f9af8a0c3..c63f48e370 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -437,13 +437,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert george.treasures(true).empty? end - def test_deprecated_push_with_attributes_was_removed - jamis = developers(:jamis) - assert_raise(NoMethodError) do - jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today) - end - end - def test_associations_with_conditions assert_equal 3, projects(:active_record).developers.size assert_equal 1, projects(:active_record).developers_named_david.size -- cgit v1.2.3 From 54eee8b5f28e05376626e98650d4bde8b8451603 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 8 Jul 2013 19:51:15 +0900 Subject: Make sure that a joins Relation can be merged with has_many :through + association proxy Closes #11248. --- .../test/cases/associations/has_many_through_associations_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index ee88fd490a..119e94b831 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -5,6 +5,7 @@ require 'models/reference' require 'models/job' require 'models/reader' require 'models/comment' +require 'models/rating' require 'models/tag' require 'models/tagging' require 'models/author' @@ -616,6 +617,11 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal post.author.author_favorites, post.author_favorites end + def test_merge_join_association_with_has_many_through_association_proxy + author = authors(:mary) + assert_nothing_raised { author.comments.ratings.to_sql } + end + def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys assert_equal 2, owners(:blackbeard).toys.count end -- cgit v1.2.3 From 8998441967a8cfc6e4302c29664ab9d0acd77704 Mon Sep 17 00:00:00 2001 From: "Karunakar (Ruby)" Date: Thu, 25 Jul 2013 21:55:43 +0530 Subject: used flat_map instead of map.flatten --- .../test/cases/associations/has_many_through_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 119e94b831..85296a5a83 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -647,7 +647,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase sarah = Person.create!(:first_name => 'Sarah', :primary_contact_id => people(:susan).id, :gender => 'F', :number1_fan_id => 1) john = Person.create!(:first_name => 'John', :primary_contact_id => sarah.id, :gender => 'M', :number1_fan_id => 1) assert_equal sarah.agents, [john] - assert_equal people(:susan).agents.map(&:agents).flatten, people(:susan).agents_of_agents + assert_equal people(:susan).agents.flat_map(&:agents), people(:susan).agents_of_agents end def test_associate_existing_with_nonstandard_primary_key_on_belongs_to -- cgit v1.2.3 From 4cf4ea506def9a3e36cc90a2b0e073cbb8fc43df Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sun, 28 Jul 2013 16:51:55 +0530 Subject: make test not depend on order `NestedThroughAssociationsTest` adds records to `member_details` table. When test performs `@member_details[0]` then the order of record is not guaranteed. Hence it is best to start with a clean slate by deleting unwanted records. --- .../test/cases/associations/has_one_through_associations_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index 90c557e886..01a4137a5d 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -191,6 +191,7 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase end def test_preloading_has_one_through_on_belongs_to + MemberDetail.delete_all assert_not_nil @member.member_type @organization = organizations(:nsa) @member_detail = MemberDetail.new -- cgit v1.2.3 From 06ac57776cf9ca75521b1f4dd2a32edb53a572e6 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Fri, 26 Jul 2013 23:22:41 +0900 Subject: Unneeded assertion --- .../test/cases/associations/has_one_through_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index 01a4137a5d..f2723f2e18 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -202,7 +202,7 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase end @new_detail = @member_details[0] assert @new_detail.send(:association, :member_type).loaded? - assert_not_nil assert_no_queries { @new_detail.member_type } + assert_no_queries { @new_detail.member_type } end def test_save_of_record_with_loaded_has_one_through -- cgit v1.2.3 From 6c32baddc04be38aa0a3fdbcfff466fbe1362e53 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 27 Jul 2013 07:09:37 +0900 Subject: Clear class ivar before testing --- activerecord/test/cases/associations/belongs_to_associations_test.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 0267cdf6e0..a79f145e31 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -605,6 +605,8 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase end def test_dependent_delete_and_destroy_with_belongs_to + AuthorAddress.destroyed_author_address_ids.clear + author_address = author_addresses(:david_address) author_address_extra = author_addresses(:david_address_extra) assert_equal [], AuthorAddress.destroyed_author_address_ids -- cgit v1.2.3 From 92c5a2244ec3e58fd5e70e7dd2d7882b80183c80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 29 Jul 2013 23:18:33 -0300 Subject: Revert change on ActiveRecord::Relation#order method that prepends new order on the old ones The previous behavior added a major backward incompatibility since it impossible to have a upgrade path without major changes on the application code. We are taking the most conservative path to be consistent with the idea of having a smoother upgrade on Rails 4. We are reverting the behavior for what was in Rails 3.x and, if needed, we will implement a new API to prepend the order clauses in Rails 4.1. --- .../cases/associations/has_and_belongs_to_many_associations_test.rb | 4 ++-- activerecord/test/cases/associations/has_many_associations_test.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index c63f48e370..712a770133 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -506,9 +506,9 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal high_id_jamis, projects(:active_record).developers.find_by_name('Jamis') end - def test_find_should_prepend_to_association_order + def test_find_should_append_to_association_order ordered_developers = projects(:active_record).developers.order('projects.id') - assert_equal ['projects.id', 'developers.name desc, developers.id desc'], ordered_developers.order_values + assert_equal ['developers.name desc, developers.id desc', 'projects.id'], ordered_developers.order_values end def test_dynamic_find_all_should_respect_readonly_access diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 9c484a8bbe..ce51853bf3 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -253,9 +253,9 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, companies(:first_firm).limited_clients.limit(nil).to_a.size end - def test_find_should_prepend_to_association_order + def test_find_should_append_to_association_order ordered_clients = companies(:first_firm).clients_sorted_desc.order('companies.id') - assert_equal ['companies.id', 'id DESC'], ordered_clients.order_values + assert_equal ['id DESC', 'companies.id'], ordered_clients.order_values end def test_dynamic_find_should_respect_association_order -- cgit v1.2.3 From 389e9fe9cfab6587a554a48087c400a383a96550 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 30 Jul 2013 20:51:44 +0530 Subject: assert_no_queries should ignore certain sqls postgresql test if randomly executed then executes "SHOW max_identifier_length". Hence the need to ignore certain predefined sqls that deal with system calls. --- .../test/cases/associations/nested_through_associations_test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb index e75d43bda8..9b1abc3b81 100644 --- a/activerecord/test/cases/associations/nested_through_associations_test.rb +++ b/activerecord/test/cases/associations/nested_through_associations_test.rb @@ -186,7 +186,9 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase members = assert_queries(4) { Member.includes(:organization_member_details_2).to_a.sort_by(&:id) } groucho_details, other_details = member_details(:groucho), member_details(:some_other_guy) - assert_no_queries do + # postgresql test if randomly executed then executes "SHOW max_identifier_length". Hence + # the need to ignore certain predefined sqls that deal with system calls. + assert_no_queries(ignore_none: false) do assert_equal [groucho_details, other_details], members.first.organization_member_details_2.sort_by(&:id) end end -- cgit v1.2.3 From 655953cd4899bb5337bae6aad081dd1beab520eb Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 1 Aug 2013 10:53:56 -0700 Subject: assert that constants have been set rather than the names --- activerecord/test/cases/associations/extension_test.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index da767a2a7e..b1dacaefcc 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -59,9 +59,12 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase end def test_extension_name - assert_equal 'DeveloperAssociationNameAssociationExtension', extension_name(Developer) - assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', extension_name(MyApplication::Business::Developer) - assert_equal 'MyApplication::Business::DeveloperAssociationNameAssociationExtension', extension_name(MyApplication::Business::Developer) + extend!(Developer) + extend!(MyApplication::Business::Developer) + extend!(MyApplication::Business::Developer) + + assert Object.const_get 'DeveloperAssociationNameAssociationExtension' + assert MyApplication::Business.const_get 'DeveloperAssociationNameAssociationExtension' end def test_proxy_association_after_scoped @@ -72,9 +75,8 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase private - def extension_name(model) + def extend!(model) builder = ActiveRecord::Associations::Builder::HasMany.new(model, :association_name, nil, {}) { } - builder.send(:wrap_block_extension) - builder.extension_module.name + builder.define_extensions(model) end end -- cgit v1.2.3 From 2733e7d9902a1cf4833619e764d8069a4523d82e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 1 Aug 2013 11:14:59 -0700 Subject: no need to define the constant twice --- activerecord/test/cases/associations/extension_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index b1dacaefcc..4c1fdfdd9a 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -61,7 +61,6 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase def test_extension_name extend!(Developer) extend!(MyApplication::Business::Developer) - extend!(MyApplication::Business::Developer) assert Object.const_get 'DeveloperAssociationNameAssociationExtension' assert MyApplication::Business.const_get 'DeveloperAssociationNameAssociationExtension' -- cgit v1.2.3 From d80a5cce88aca297ca489b90b62e2cc164c238f1 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 1 Aug 2013 11:43:11 -0700 Subject: association builder classes no longer need the model decouple the builder classes from the model. Builder objects should be easier to reuse now. --- activerecord/test/cases/associations/extension_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index 4c1fdfdd9a..47dff7d0ea 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -75,7 +75,7 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase private def extend!(model) - builder = ActiveRecord::Associations::Builder::HasMany.new(model, :association_name, nil, {}) { } + builder = ActiveRecord::Associations::Builder::HasMany.new(:association_name, nil, {}) { } builder.define_extensions(model) end end -- cgit v1.2.3 From 7fc3ca5fadecd4c6c506078281f556c28a5c382d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 2 Aug 2013 15:29:47 -0700 Subject: add a test for concat on hm:t associations --- .../test/cases/associations/has_many_through_associations_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 85296a5a83..724d1cbbf8 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -91,6 +91,13 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end end + def test_concat + person = people(:david) + post = posts(:thinking) + post.people.concat [person] + assert_equal 1, post.people.size + assert_equal 1, post.people(true).size + end def test_associate_existing_record_twice_should_add_to_target_twice post = posts(:thinking) -- cgit v1.2.3 From 8910f12fb4d86a58dedb36b19f4b2470337662e7 Mon Sep 17 00:00:00 2001 From: Rajarshi Das Date: Tue, 13 Aug 2013 17:56:15 +0530 Subject: using assert_not instead of refute --- activerecord/test/cases/associations/inverse_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index 71cf1237e8..ce78332124 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -406,7 +406,7 @@ class InverseHasManyTests < ActiveRecord::TestCase interest = Interest.create!(man: man) man.interests.find(interest.id) - refute man.interests.loaded? + assert_not man.interests.loaded? end def test_raise_record_not_found_error_when_invalid_ids_are_passed -- cgit v1.2.3 From c9e2fa22cbf74f8184e502e7726fcfbe33bdb3e6 Mon Sep 17 00:00:00 2001 From: wangjohn Date: Wed, 14 Aug 2013 21:50:49 -0700 Subject: Fixing multi-word automatic inverse detection. Currently, ActiveRecord models with multiple words cannot have their inverse associations detected automatically. --- .../test/cases/associations/inverse_associations_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index ce78332124..2477e60e51 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -9,10 +9,24 @@ require 'models/rating' require 'models/comment' require 'models/car' require 'models/bulb' +require 'models/mixed_case_monkey' class AutomaticInverseFindingTests < ActiveRecord::TestCase fixtures :ratings, :comments, :cars + def test_has_one_and_belongs_to_should_find_inverse_automatically_on_multiple_word_name + monkey_reflection = MixedCaseMonkey.reflect_on_association(:man) + man_reflection = Man.reflect_on_association(:mixed_case_monkey) + + assert_respond_to monkey_reflection, :has_inverse? + assert monkey_reflection.has_inverse?, "The monkey reflection should have an inverse" + assert_equal man_reflection, monkey_reflection.inverse_of, "The monkey reflection's inverse should be the man reflection" + + assert_respond_to man_reflection, :has_inverse? + assert man_reflection.has_inverse?, "The man reflection should have an inverse" + assert_equal monkey_reflection, man_reflection.inverse_of, "The man reflection's inverse should be the monkey reflection" + end + def test_has_one_and_belongs_to_should_find_inverse_automatically car_reflection = Car.reflect_on_association(:bulb) bulb_reflection = Bulb.reflect_on_association(:car) -- cgit v1.2.3 From ec8ef1e1055c4e1598da13f49d30261f07f4a9b4 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Sat, 17 Aug 2013 21:38:53 +0530 Subject: Revert "Merge branch 'master' of github.com:rails/docrails" This reverts commit 70d6e16fbad75b89dd1798ed697e7732b8606fa3, reversing changes made to ea4db3bc078fb3093ecdddffdf4f2f4ff3e1e8f9. Seems to be a code merge done by mistake. --- .../test/cases/associations/has_many_through_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 85296a5a83..119e94b831 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -647,7 +647,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase sarah = Person.create!(:first_name => 'Sarah', :primary_contact_id => people(:susan).id, :gender => 'F', :number1_fan_id => 1) john = Person.create!(:first_name => 'John', :primary_contact_id => sarah.id, :gender => 'M', :number1_fan_id => 1) assert_equal sarah.agents, [john] - assert_equal people(:susan).agents.flat_map(&:agents), people(:susan).agents_of_agents + assert_equal people(:susan).agents.map(&:agents).flatten, people(:susan).agents_of_agents end def test_associate_existing_with_nonstandard_primary_key_on_belongs_to -- cgit v1.2.3 From 594ce0f0a84f6de7719d59b55eee4b859efd6461 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 27 Aug 2013 15:14:48 -0700 Subject: query the association rather than send the method for the association name --- .../test/cases/associations/cascaded_eager_loading_test.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index e693d34f99..811d91f849 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -52,12 +52,10 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase def test_cascaded_eager_association_loading_with_join_for_count categories = Category.joins(:categorizations).includes([{:posts=>:comments}, :authors]) - assert_nothing_raised do - assert_equal 4, categories.count - assert_equal 4, categories.to_a.count - assert_equal 3, categories.distinct.count - assert_equal 3, categories.to_a.uniq.size # Must uniq since instantiating with inner joins will get dupes - end + assert_equal 4, categories.count + assert_equal 4, categories.to_a.count + assert_equal 3, categories.distinct.count + assert_equal 3, categories.to_a.uniq.size # Must uniq since instantiating with inner joins will get dupes end def test_cascaded_eager_association_loading_with_duplicated_includes -- cgit v1.2.3 From 44046d0782be0737ec68964695a63a56727d17e1 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Aug 2013 11:49:23 -0700 Subject: pk should not be required for hm:t associations --- .../test/cases/associations/has_many_through_associations_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 941e851aae..788177ee15 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -36,6 +36,13 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase Reader.create :person_id => 0, :post_id => 0 end + def test_pk_is_not_required_for_join + post = Post.includes(:scategories).first + post2 = Post.includes(:categories).first + + assert_equal post2.categories, post.categories + end + def test_include? person = Person.new post = Post.new -- cgit v1.2.3 From c975da941351aad42387a82f0a70bd6f123f987a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 29 Aug 2013 13:22:27 -0700 Subject: make sure there are actually some categories when running the test --- .../test/cases/associations/has_many_through_associations_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 788177ee15..45087fdf8a 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -40,6 +40,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase post = Post.includes(:scategories).first post2 = Post.includes(:categories).first + assert_operator post.categories.length, :>, 0 assert_equal post2.categories, post.categories end -- cgit v1.2.3 From ffa56f73d5ae98fe0b8b6dd2ca6f0dffac9d9217 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 30 Aug 2013 10:39:02 -0700 Subject: add missing fixtures file --- .../test/cases/associations/has_many_through_associations_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 45087fdf8a..c00cae5750 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -28,7 +28,8 @@ require 'models/club' class HasManyThroughAssociationsTest < ActiveRecord::TestCase fixtures :posts, :readers, :people, :comments, :authors, :categories, :taggings, :tags, :owners, :pets, :toys, :jobs, :references, :companies, :members, :author_addresses, - :subscribers, :books, :subscriptions, :developers, :categorizations, :essays + :subscribers, :books, :subscriptions, :developers, :categorizations, :essays, + :categories_posts # Dummies to force column loads so query counts are clean. def setup -- cgit v1.2.3 From bf2c4280563ba5f86072c1bad8af27ff5e5da0f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a=20+=20Kassio=20Borges?= Date: Sat, 31 Aug 2013 14:53:28 -0300 Subject: Don't need to check if the scope respond to call We are checking this when defining the default scope and raising an ArgumentError --- .../test/cases/associations/nested_through_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb index 9b1abc3b81..cf3c07845c 100644 --- a/activerecord/test/cases/associations/nested_through_associations_test.rb +++ b/activerecord/test/cases/associations/nested_through_associations_test.rb @@ -371,7 +371,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase prev_default_scope = Club.default_scopes [:includes, :preload, :joins, :eager_load].each do |q| - Club.default_scopes = [Club.send(q, :category)] + Club.default_scopes = [proc { Club.send(q, :category) }] assert_equal categories(:general), members(:groucho).reload.club_category end ensure -- cgit v1.2.3 From 1f006cd5f10663286e70b4c3e972fba91ac8c9f9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 3 Sep 2013 14:16:29 -0700 Subject: support anonymous classes on has_many associations --- .../cases/associations/has_many_associations_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index ce51853bf3..4c0fa88917 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -45,6 +45,24 @@ class HasManyAssociationsTest < ActiveRecord::TestCase Client.destroyed_client_ids.clear end + def test_anonymous_has_many + developer = Class.new(ActiveRecord::Base) { + self.table_name = 'developers' + dev = self + + developer_project = Class.new(ActiveRecord::Base) { + self.table_name = 'developers_projects' + belongs_to :developer, :class => dev + } + has_many :developer_projects, :class => developer_project, :foreign_key => 'developer_id' + } + dev = developer.first + named = Developer.find(dev.id) + assert_operator dev.developer_projects.count, :>, 0 + assert_equal named.projects.map(&:id).sort, + dev.developer_projects.map(&:project_id).sort + end + def test_create_from_association_should_respect_default_scope car = Car.create(:name => 'honda') assert_equal 'honda', car.name -- cgit v1.2.3 From 489a0890b6dbb8c7954e2a3b120fe31de64f156c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 3 Sep 2013 16:53:50 -0700 Subject: adding a hm:t test for singleton ar objects --- .../has_many_through_associations_test.rb | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index c00cae5750..508faa9437 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -37,6 +37,33 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase Reader.create :person_id => 0, :post_id => 0 end + def make_model(name) + Class.new(ActiveRecord::Base) { define_singleton_method(:name) { name } } + end + + def test_singleton_has_many_through + book = make_model "Book" + subscription = make_model "Subscription" + subscriber = make_model "Subscriber" + + subscriber.primary_key = 'nick' + subscription.belongs_to :book, class: book + subscription.belongs_to :subscriber, class: subscriber + + book.has_many :subscriptions, class: subscription + book.has_many :subscribers, through: :subscriptions, class: subscriber + + anonbook = book.first + namebook = Book.find anonbook.id + + assert_operator anonbook.subscribers.count, :>, 0 + anonbook.subscribers.each do |s| + assert_instance_of subscriber, s + end + assert_equal namebook.subscribers.map(&:id).sort, + anonbook.subscribers.map(&:id).sort + end + def test_pk_is_not_required_for_join post = Post.includes(:scategories).first post2 = Post.includes(:categories).first -- cgit v1.2.3 From 3f1c0c2bd0b89255b0d7d8d6fe45ac2d50b05076 Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Wed, 21 Aug 2013 10:49:18 +0300 Subject: Extracted from `order` processing of arguments, and use it for `reorder` to be consistent. --- activerecord/test/cases/associations/eager_test.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 28bf48f4fd..8d3d6962fc 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1172,8 +1172,11 @@ class EagerAssociationTest < ActiveRecord::TestCase } end - test "works in combination with order(:symbol)" do - author = Author.includes(:posts).references(:posts).order(:name).where('posts.title IS NOT NULL').first + test "works in combination with order(:symbol) and reorder(:symbol)" do + author = Author.includes(:posts).references(:posts).order(:name).find_by('posts.title IS NOT NULL') + assert_equal authors(:bob), author + + author = Author.includes(:posts).references(:posts).reorder(:name).find_by('posts.title IS NOT NULL') assert_equal authors(:bob), author end end -- cgit v1.2.3 From 6a91a3307ff6556225ab8717617074cea20222e0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 6 Sep 2013 17:14:07 -0700 Subject: hm:t join tables may not have a primary key --- .../associations/has_many_through_associations_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 508faa9437..541f649ae9 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -64,6 +64,24 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase anonbook.subscribers.map(&:id).sort end + def test_no_pk_join_table_append + lesson = make_model 'Lesson' + student = make_model 'Student' + + lesson_student = make_model 'LessonStudent' + lesson_student.table_name = 'lessons_students' + + lesson_student.belongs_to :lesson, :class => lesson + lesson_student.belongs_to :student, :class => student + lesson.has_many :lesson_students, :class => lesson_student + lesson.has_many :students, :through => :lesson_students, :class => student + + sicp = lesson.new(:name => "SICP") + ben = student.new(:name => "Ben Bitdiddle") + sicp.students << ben + assert sicp.save! + end + def test_pk_is_not_required_for_join post = Post.includes(:scategories).first post2 = Post.includes(:categories).first -- cgit v1.2.3 From 8875e28a50b117aa862c8563c49f7e3a6ee7deff Mon Sep 17 00:00:00 2001 From: Lann Martin Date: Wed, 4 Sep 2013 16:47:28 -0700 Subject: Make CollectionAssociation first/last with integer fetch with query When first or last is called with an integer on an unloaded association, the entire collection is loaded. This differs surprisingly from the behavior of Relation#first/last, which translate the call into a limit query. For large collections this can make a big difference in performance. Change CollectionAssociation#fetch_first_or_last_using_find? to make this kind of call delegate to Relation. --- .../test/cases/associations/has_many_associations_test.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 4c0fa88917..ebeead0dc2 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1414,15 +1414,17 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end - def test_calling_first_or_last_with_integer_on_association_should_load_association + def test_calling_first_or_last_with_integer_on_association_should_not_load_association firm = companies(:first_firm) + firm.clients.create(:name => 'Foo') + assert !firm.clients.loaded? - assert_queries 1 do + assert_queries 2 do firm.clients.first(2) firm.clients.last(2) end - assert firm.clients.loaded? + assert !firm.clients.loaded? end def test_calling_many_should_count_instead_of_loading_association -- cgit v1.2.3 From e7facb35eb67836d446283bcb7d15d665b1bb668 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 10 Sep 2013 14:03:19 -0700 Subject: add a comment for sanity of other people to come --- .../associations/has_and_belongs_to_many_associations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 712a770133..bb927841ab 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -605,6 +605,10 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end def test_join_table_alias + # FIXME: `references` has no impact on the aliases generated for the join + # query. The fact that we pass `:developers_projects_join` to `references` + # and that the SQL string contains `developers_projects_join` is merely a + # coincidence. assert_equal( 3, Developer.references(:developers_projects_join).merge( @@ -615,6 +619,10 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end def test_join_with_group + # FIXME: `references` has no impact on the aliases generated for the join + # query. The fact that we pass `:developers_projects_join` to `references` + # and that the SQL string contains `developers_projects_join` is merely a + # coincidence. group = Developer.columns.inject([]) do |g, c| g << "developers.#{c.name}" g << "developers_projects_2.#{c.name}" -- cgit v1.2.3 From 7e0cac156eae758a81b2f0f4ac1b18afd7e5354e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 11 Sep 2013 11:23:11 -0700 Subject: fix deleting join models with no pk --- .../has_many_through_associations_test.rb | 52 +++++++++++++++++++--- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 541f649ae9..dd8b426b25 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -65,6 +65,52 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end def test_no_pk_join_table_append + lesson, _, student = make_no_pk_hm_t + + sicp = lesson.new(:name => "SICP") + ben = student.new(:name => "Ben Bitdiddle") + sicp.students << ben + assert sicp.save! + end + + def test_no_pk_join_table_delete + lesson, lesson_student, student = make_no_pk_hm_t + + sicp = lesson.new(:name => "SICP") + ben = student.new(:name => "Ben Bitdiddle") + louis = student.new(:name => "Louis Reasoner") + sicp.students << ben + sicp.students << louis + assert sicp.save! + + sicp.students.reload + assert_operator lesson_student.count, :>=, 2 + assert_no_difference('student.count') do + assert_difference('lesson_student.count', -2) do + sicp.students.destroy(*student.all.to_a) + end + end + end + + def test_no_pk_join_model_callbacks + lesson, lesson_student, student = make_no_pk_hm_t + + after_destroy_called = false + lesson_student.after_destroy do + after_destroy_called = true + end + + sicp = lesson.new(:name => "SICP") + ben = student.new(:name => "Ben Bitdiddle") + sicp.students << ben + assert sicp.save! + + sicp.students.reload + sicp.students.destroy(*student.all.to_a) + assert after_destroy_called, "after destroy should be called" + end + + def make_no_pk_hm_t lesson = make_model 'Lesson' student = make_model 'Student' @@ -75,11 +121,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase lesson_student.belongs_to :student, :class => student lesson.has_many :lesson_students, :class => lesson_student lesson.has_many :students, :through => :lesson_students, :class => student - - sicp = lesson.new(:name => "SICP") - ben = student.new(:name => "Ben Bitdiddle") - sicp.students << ben - assert sicp.save! + [lesson, lesson_student, student] end def test_pk_is_not_required_for_join -- cgit v1.2.3 From a94e2db05ccd0d4ae7681d26b4ce929184cffefc Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Wed, 4 Sep 2013 18:36:28 -0400 Subject: Avoid empty transaction from setting has_one association on new record. --- activerecord/test/cases/associations/has_one_associations_test.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 4fdf9a9643..9cd4db8dc9 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -505,6 +505,8 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_no_queries { company.account = nil } account = Account.find(2) assert_queries { company.account = account } + + assert_no_queries { Firm.new.account = account } end def test_has_one_assignment_triggers_save_on_change -- cgit v1.2.3 From 71cf717ee297bc2407de304e5a880f1e39ab4f3c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 11 Sep 2013 17:37:39 -0700 Subject: these are not real developer objects, so counting them doesn't make sense. Let's load the object to ensure it's an array and count the array. --- .../cases/associations/has_and_belongs_to_many_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index bb927841ab..f77066e6ab 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -651,7 +651,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end def test_find_scoped_grouped_having - assert_equal 2, projects(:active_record).well_payed_salary_groups.size + assert_equal 2, projects(:active_record).well_payed_salary_groups.to_a.size assert projects(:active_record).well_payed_salary_groups.all? { |g| g.salary > 10000 } end -- cgit v1.2.3 From fbbb6c87fd5e8fcd913c0dbf518024edd7538072 Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Sat, 24 Aug 2013 19:12:05 +0300 Subject: Collapse where constraints to one where constraint In order to remove duplication with joining arel where constraints with `AND`, all constraints on `build_arel` are collapsed into one head node: `Arel::Nodes::And` Closes: #11963 --- activerecord/test/cases/associations/inner_join_association_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inner_join_association_test.rb b/activerecord/test/cases/associations/inner_join_association_test.rb index de47a576c6..9fe5ff50d9 100644 --- a/activerecord/test/cases/associations/inner_join_association_test.rb +++ b/activerecord/test/cases/associations/inner_join_association_test.rb @@ -41,6 +41,11 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase assert_no_match(/WHERE/i, sql) end + def test_join_association_conditions_support_string_and_arel_expressions + assert_equal 0, Author.joins(:welcome_posts_with_comment).count + assert_equal 1, Author.joins(:welcome_posts_with_comments).count + end + def test_join_conditions_allow_nil_associations authors = Author.includes(:essays).where(:essays => {:id => nil}) assert_equal 2, authors.count -- cgit v1.2.3 From e5299c1ef693ef434f55811027a7da975cd55ba5 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 23 Sep 2013 15:58:34 -0700 Subject: hm:t preloading will respect order set on the RHS association --- .../associations/has_many_through_associations_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index dd8b426b25..99c71a1dd9 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -41,6 +41,21 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase Class.new(ActiveRecord::Base) { define_singleton_method(:name) { name } } end + def test_ordered_habtm + person_prime = Class.new(ActiveRecord::Base) do + def self.name; 'Person'; end + + has_many :readers + has_many :posts, -> { order('posts.id DESC') }, :through => :readers + end + posts = person_prime.includes(:posts).first.posts + + assert_operator posts.length, :>, 1 + posts.each_cons(2) do |left,right| + assert_operator left.id, :>, right.id + end + end + def test_singleton_has_many_through book = make_model "Book" subscription = make_model "Subscription" -- cgit v1.2.3 From 9b47142ce120d0a7d715648259ffa238526d1f82 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 23 Sep 2013 18:13:08 -0700 Subject: adding a test for sti on middle tables with sorting on RHS --- .../associations/has_many_through_associations_test.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 99c71a1dd9..149c9576ca 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -29,7 +29,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase fixtures :posts, :readers, :people, :comments, :authors, :categories, :taggings, :tags, :owners, :pets, :toys, :jobs, :references, :companies, :members, :author_addresses, :subscribers, :books, :subscriptions, :developers, :categorizations, :essays, - :categories_posts + :categories_posts, :clubs, :memberships # Dummies to force column loads so query counts are clean. def setup @@ -37,6 +37,19 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase Reader.create :person_id => 0, :post_id => 0 end + def test_preload_sti_middle_relation + club = Club.create!(name: 'Aaron cool banana club') + member1 = Member.create!(name: 'Aaron') + member2 = Member.create!(name: 'Cat') + + SuperMembership.create! club: club, member: member1 + CurrentMembership.create! club: club, member: member2 + + club1 = Club.includes(:members).find_by_id club.id + left, right = club1.members.map(&:id) + assert_operator left, :>, right + end + def make_model(name) Class.new(ActiveRecord::Base) { define_singleton_method(:name) { name } } end -- cgit v1.2.3 From bb9554ad62a3f37738ee7ad99dbbb9a37784c343 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 23 Sep 2013 18:15:38 -0700 Subject: we can't sort by lhs since the middle records have difference classes and possibly different rules for finding those objects --- .../test/cases/associations/has_many_through_associations_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 149c9576ca..29f6166b00 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -47,7 +47,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase club1 = Club.includes(:members).find_by_id club.id left, right = club1.members.map(&:id) - assert_operator left, :>, right + assert_equal [member1, member2].sort_by(&:id), + club1.members.sort_by(&:id) end def make_model(name) -- cgit v1.2.3 From b93d09dbc59b3b85e7208cd57c92be9d86fd51df Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 24 Sep 2013 11:28:28 -0700 Subject: push preloaded test up to the factory method so we can eliminate conditionals from the individual preloaded classes --- .../test/cases/associations/has_many_through_associations_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 29f6166b00..c0e80c5fe9 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -46,7 +46,6 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase CurrentMembership.create! club: club, member: member2 club1 = Club.includes(:members).find_by_id club.id - left, right = club1.members.map(&:id) assert_equal [member1, member2].sort_by(&:id), club1.members.sort_by(&:id) end -- cgit v1.2.3 From 3af4ae82e587b12664626e7b22bc6cc21ebbca2e Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 25 Sep 2013 14:24:27 -0400 Subject: Make sure inverse_of is visible on the has_many callbacks --- activerecord/test/cases/associations/has_many_associations_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index ebeead0dc2..fd9b129ed4 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -518,6 +518,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end + def test_inverse_on_before_validate + firm = companies(:first_firm) + assert_queries(1) do + firm.clients_of_firm << Client.new("name" => "Natural Company") + end + end + def test_new_aliased_to_build company = companies(:first_firm) new_client = assert_no_queries { company.clients_of_firm.new("name" => "Another Client") } -- cgit v1.2.3 From 645af000a4bb0c742063e61d8da01aa5053bc9da Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 25 Sep 2013 19:06:00 -0400 Subject: fix .find when inverse is set .find([1]) should return an Array of entries, even when a invese object is in memory already --- .../test/cases/associations/has_many_associations_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index fd9b129ed4..064e31f634 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -337,6 +337,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) } end + def test_find_ids_and_inverse_of + force_signal37_to_load_all_clients_of_firm + + firm = companies(:first_firm) + client = firm.clients_of_firm.find(3) + assert_kind_of Client, client + + client_ary = firm.clients_of_firm.find([3]) + assert_kind_of Array, client_ary + assert_equal client, client_ary.first + end + def test_find_all firm = Firm.all.merge!(:order => "id").first assert_equal 2, firm.clients.where("#{QUOTED_TYPE} = 'Client'").to_a.length -- cgit v1.2.3 From 3e0a60e4e2316ee696bdcf1c115582f8f450ad07 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 27 Sep 2013 16:56:49 -0700 Subject: adding a test to demonstrate how to use STI subclasses on the far right side of a hm:t association along with preloading. --- .../test/cases/associations/has_many_through_associations_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index c0e80c5fe9..5299e4e17e 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -37,6 +37,13 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase Reader.create :person_id => 0, :post_id => 0 end + def test_preload_sti_rhs_class + developers = Developer.includes(:firms).all.to_a + assert_no_queries do + developers.each { |d| d.firms } + end + end + def test_preload_sti_middle_relation club = Club.create!(name: 'Aaron cool banana club') member1 = Member.create!(name: 'Aaron') -- cgit v1.2.3 From 8fb0de2cae8e6f26c71ab8e4267d3841a38a29b9 Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Sun, 14 Jul 2013 17:32:56 +0300 Subject: Removed where_values_hash from AR::NullRelation In order to build associated records for owners which has not been saved need to get where values to use as default attributes. But for new record owner uses `ActiveRecord::NullRelation` which override `where_values_hash` to return empty hash stub. `where_values_hash` is not used to invoke any sql query, but good to build others chains (even will be never executed) like: ```ruby post = Post.new admin_comment = post.admin_comments.build assert_equal 'Admin', admin_comment.author ``` Closes #11376, #11676, #11675 --- .../associations/has_many_associations_test.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 064e31f634..caa916346a 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -80,6 +80,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 'exotic', bulb.name end + def test_build_from_association_should_respect_scope + author = Author.new + + post = author.thinking_posts.build + assert_equal 'So I was thinking', post.title + end + def test_create_from_association_with_nil_values_should_work car = Car.create(:name => 'honda') @@ -1629,6 +1636,22 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal car.id, bulb.attributes_after_initialize['car_id'] end + def test_attributes_are_set_when_initialized_from_has_many_null_relationship + car = Car.new name: 'honda' + bulb = car.bulbs.where(name: 'headlight').first_or_initialize + assert_equal 'headlight', bulb.name + end + + def test_attributes_are_set_when_initialized_from_polymorphic_has_many_null_relationship + post = Post.new title: 'title', body: 'bar' + tag = Tag.create!(name: 'foo') + + tagging = post.taggings.where(tag: tag).first_or_initialize + + assert_equal tag.id, tagging.tag_id + assert_equal 'Post', tagging.taggable_type + end + def test_replace car = Car.create(:name => 'honda') bulb1 = car.bulbs.create -- cgit v1.2.3 From ad7b5efb55bcc2e0ccd3e7f22a81e984df8676d1 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 1 Oct 2013 15:40:40 -0700 Subject: Squashed commit of the following: commit 02d3b059608c30e98136fde78bc710928f080566 Author: Aaron Patterson Date: Mon Sep 30 15:31:39 2013 -0700 habtm works in terms of hm:t commit 71ac336bbb41f5047a4ee307883a95eca7195742 Author: Aaron Patterson Date: Mon Sep 30 15:27:07 2013 -0700 passing before_add callbacks commit d846a7bf9872a79c3aa8082917abe806278fa159 Author: Aaron Patterson Date: Fri Sep 27 17:57:53 2013 -0700 reducing diff against master commit 96bd97de47d61a71c368ae367bc59a2dbec3c9ab Author: Aaron Patterson Date: Fri Sep 27 17:52:35 2013 -0700 fixing more tests commit 0620399fc231df87c5f08664db1c37e5c1fa5a05 Author: Aaron Patterson Date: Fri Sep 27 17:32:52 2013 -0700 self-referential tables in hm:t are allowed to have the same pk commit 48eb90e27921d10b6ba3e400ab2c784ed75d5ec4 Author: Aaron Patterson Date: Fri Sep 27 17:03:19 2013 -0700 translating more options commit 5cace7b2cb546fd6b096543bfc49c4b7197ad21a Author: Aaron Patterson Date: Fri Sep 27 17:01:02 2013 -0700 handling more hm:t cases commit 69985ca2cabff2c3f58f5d0a7eb12d7b414c1a01 Merge: d417ec8 3e0a60e Author: Aaron Patterson Date: Fri Sep 27 16:57:12 2013 -0700 Merge branch 'master' into rmhbtm * master: (21 commits) adding a test to demonstrate how to use STI subclasses on the far right side of a hm:t association along with preloading. Fixed grammatical typo in configuring.md guide. Getting Started Guide: Fix code container in Chapter 5.2 [ci-skip] Getting Started Guide: Update sentence in Chapter 5.8 [ci-skip] Add new line after create action on CommentsController code example Adds template dependencies rake task from cache_digests gem. [ci skip] Update scaffold output and change some words. [ci skip] escape unintended url in docs Getting Started Guide: post.rb -> Post [ci skip] Add missing periods and update link name and some wording. quote `false` reference in querying guide. Getting Started Guide: update link_to string argument to use single-quote mark, following document style [ci skip] Fix small typo in docs changelog entry fix .find when inverse is set update changelog for #12359 Make sure inverse_of is visible on the has_many callbacks Getting Started Guide: posts_controller -> PostsController [ci skip] [ci skip] Correct the explanation of the example for find_or_create_by when used with create_with in ActiveRecord Querying guide added "id: false" to HABTM join table example ... commit d417ec82e8f83c32124d1c1a19824d023cfdf015 Author: Aaron Patterson Date: Wed Sep 25 16:49:08 2013 -0700 another case is passing commit 5c68280500962e4b2b6819dd863ebe8b398e5834 Author: Aaron Patterson Date: Wed Sep 25 16:21:22 2013 -0700 this seems to work commit e458c5e55c04a2444e96aca1ff192be42bc4ce7f Author: Aaron Patterson Date: Wed Sep 25 16:13:07 2013 -0700 add another case commit fc6203b0d49c847b8efb1cc33d358897625f2115 Author: Aaron Patterson Date: Wed Sep 25 15:51:45 2013 -0700 delete center records on habtm commit 9af5156098f6bc8f8ce8eb559a51137960b4938b Merge: 3a60b03 e2fd64f Author: Aaron Patterson Date: Wed Sep 25 11:33:13 2013 -0700 Merge branch 'master' into rmhbtm * master: (100 commits) remove initialize method extract association resetting to a method hash insertion order doesn't matter anymore, so only loop over the owners once always populate the preloaded records instance variable so we can remove the @associated_records_by_owner ivar keep preloaded records in a list rather than extract from a hash Getting Started Guide: Hello Rails! -> Hello, Rails! and wrap code tag push slice loading to it's own method so we can remove the type casting code Add CHANGELOG entry for #12344 Add regression test to #12343 Fix typo in number_to_human docs: you -> your guarantee that `klass` is not nil inside the preloader objects [Documentation] Add a missing validation to I18n docs Use the given name in html_options for the hidden field in collection_check_boxes eliminate unused ivar all records have a preloaded, so eliminate that conditional eliminate the `loaded?` conditional push preloaded test up to the factory method so we can eliminate conditionals from the individual preloaded classes assign_attributes should return if argument is blank. No need the else clause Use join to concat the both side of the AST ... commit 3a60b038a40532397b6c204dfb09d6d43a9336ac Author: Aaron Patterson Date: Wed Sep 18 17:50:11 2013 -0700 start with a clean slate commit f30d3631af11ea6144d3ae7a068a7c0072e93a82 Author: Aaron Patterson Date: Wed Sep 18 17:30:59 2013 -0700 make sure the class name goes on the rhs belongs_to commit f7516b724014504ddb2e706fea1b5438dc5332c3 Author: Aaron Patterson Date: Wed Sep 18 16:31:40 2013 -0700 remove unused variable commit 61ffc5b9854dc2fe83ee502b17ba8028270ff8a7 Merge: 6cf41cd 460eb83 Author: Aaron Patterson Date: Wed Sep 18 16:25:17 2013 -0700 Merge branch 'master' into rmhbtm * master: support objects with blank string primary keys ActiveRecord::Base#<=> has been removed. Primary keys may not be in order, or even be numbers, so sorting by id doesn't make sense. Please use `sort_by` and specify the attribute you wish to sort with. For example, change: do what the superclass does in the case that objects do not match commit 6cf41cd98a82e6f4fe6d868ad323df3d72a9748f Author: Aaron Patterson Date: Wed Sep 18 15:06:28 2013 -0700 heating up habtm cache commit d7f6c3aa491f27ba71fdb2b9d0b9d1780664f4dc Merge: c68c904 56bfd8a Author: Aaron Patterson Date: Wed Sep 18 14:07:51 2013 -0700 Merge branch 'master' into rmhbtm * master: (58 commits) Fix an issue where router can't recognize downcased url encoding path. There's no need to do this Remove tzinfo dependency from Action Pack [ci skip] Improve readability of 4.3's NOTE in migration.md. Removes unused code related to DatabaseTasks. [ci skip] Consistency wording of 9.6 in form_helpers.md [ci skip] Update plugins.md Removing ActiveSupport::Concern, it's not needed Fixing comment typo in ActionController::Base Don't require using application_name before options Collapse where constraints to one where constraint Custom flash should be defined only for the class that defines it and it's subclasses. Fix typos: the indefinite articles(a -> an) Missing destroy command Update 3_2_release_notes.md Add CHANGELOG entry for #11698 Add CHANGELOG entry for #12149 Use the Rails binary when generating task Remove unnecessary loop "generates" applies to "collection radio" so it should be singular ... commit c68c904866ef9562c3bd9b54574206a416184414 Merge: 0f5d8e0 71cf717 Author: Aaron Patterson Date: Wed Sep 11 17:45:53 2013 -0700 Merge branch 'master' into rmhbtm * master: these are not real developer objects, so counting them doesn't make sense. Let's load the object to ensure it's an array and count the array. Remove conditional adding a new method Fix inverted conditional Remove invalid comment Check if the SQL is not a prepared statement Whitespaces Revert "Add meta tag with charset information to application layout." Avoid empty transaction from setting has_one association on new record. Reduce Duration#inspect to a single series of transformations Relation#merge should not lose readonly(false) flag. Reduce allocations when extracting AR models Perf: avoid dupes add fallback logic for coders commit 0f5d8e0febd3128cf4121ff36f1764b9284d9f7d Author: Aaron Patterson Date: Wed Sep 11 11:43:29 2013 -0700 everything works with extensions commit d003c103b5908fb3a6427f39bddd1748ef2c2576 Merge: 5768c38 7e0cac1 Author: Aaron Patterson Date: Wed Sep 11 11:23:59 2013 -0700 Merge branch 'master' into rmhbtm * master: fix deleting join models with no pk remove sentence err [ci skip] 'previous version of Rails' is gramatically incorrect Add meta tag with charset information to application layout. add a comment for sanity of other people to come commit 5768c38d53fd66a97814faaea8e07c70722b310f Author: Aaron Patterson Date: Tue Sep 10 15:11:05 2013 -0700 habtms with a scope seem to be working commit 5ee9108d95c544d4befd682a72139383d0780d68 Merge: d5478e6 e64b5da Author: Aaron Patterson Date: Tue Sep 10 11:55:48 2013 -0700 Merge branch 'master' into rmhbtm * master: ask the association for records rather than calling `send` commit d5478e64bbf80337ec35462368edabb373feeb74 Author: Aaron Patterson Date: Tue Sep 10 11:54:39 2013 -0700 ask the association for records rather than calling `send` commit 93020bc1ad51363c3f400370f91c9494690dcea8 Merge: 11b3d5f d68419a Author: Aaron Patterson Date: Tue Sep 10 11:30:03 2013 -0700 Merge branch 'master' into rmhbtm * master: Use Ruby 2.0 caller_locations instead of caller if available Update Rails 3.2.x guide link [ci skip] Be sure to restore the default I18n.locale after changed its value in a test Fixes typo in Object#try! More unused associations in AR test models :scissors: [ci skip] change function def self.table_name to self.table_name Clean up unused associations in AR test model Reset ActionView::Base.logger instead of AC::Base.logger Refactor handling of action normalization Don't mutate the Base settings by merge!ing the given value Make AC standalone rendering work use assert_empty in activemodel conditional validation test cases Removed unused modules and classes Removed unnecessary require Remove helper fixtures not used in any test Back AV description in gemspec Fix order dependent test grab executable from rubygems Fixed API task file commit 11b3d5fa45b57fc4e7dddb09be583498b120b185 Author: Aaron Patterson Date: Mon Sep 9 16:19:48 2013 -0700 change query count since we are using hm:t associations commit f59daebedab3ed13f31c99244ff71a4a5d6e554b Author: Aaron Patterson Date: Mon Sep 9 15:35:07 2013 -0700 delegate compute_type to a real AR class commit c84a40d2ed76e5843b994c5a2b9e29ced3816511 Author: Aaron Patterson Date: Mon Sep 9 15:29:51 2013 -0700 define callbacks with the builder commit d08b1b6b3efc9ed8b0d5476892f048fbffb39e40 Merge: acebec1 0c5d0be Author: Aaron Patterson Date: Mon Sep 9 15:17:40 2013 -0700 Merge branch 'master' into rmhbtm * master: let the class cache object clean up user input make @bitsweat happy. :heart: commit acebec128e108ac2b4855e540d8764629670cb83 Merge: 2de68a4 e1cbd42 Author: Aaron Patterson Date: Mon Sep 9 14:47:30 2013 -0700 Merge branch 'master' into rmhbtm * master: stop using deprecated api in the tests commit 2de68a464641f76067743957f889ac87dff395a0 Merge: 7504df9 1385ae1 Author: Aaron Patterson Date: Mon Sep 9 14:34:37 2013 -0700 Merge branch 'master' into rmhbtm * master: Remove BasicRendering tests Remove remaining coupling with AV in MimeResponds Remove BasicRendering and remove template functionality from AbsC::Rendering Improves a sentence in guides/security [ci skip] Change link name of Rails i18n wiki. Typo in Changelog. Fix fixtures regression that required table names to map to classes only, not class names Use MiniTest::Unit::TestCase instead of Minitest::Test Use Ruby on Rails Coding Conventions for code examples in the guides commit 7504df92f21ed3d5da7c2760d760f026728ed04d Author: Aaron Patterson Date: Mon Sep 9 14:34:02 2013 -0700 fake class name should be a valid class name commit 6609620ea86dc0fb7c4bbfb0db950f6e3fc75b56 Author: Aaron Patterson Date: Fri Sep 6 15:52:03 2013 -0700 move another habtm commit 2c95a36e2c3dfe92f2930f3cca44bc4452732a23 Author: Aaron Patterson Date: Fri Sep 6 15:50:18 2013 -0700 use the habtm name to generate the rhs name on the join model commit bd963f720b1db19b0bec186bc33bef9203d8b011 Author: Aaron Patterson Date: Fri Sep 6 15:47:29 2013 -0700 don't hardcode the name commit 461759e2caf66f23dca4eff988648bf769a2b533 Author: Aaron Patterson Date: Fri Sep 6 15:45:27 2013 -0700 we do not need to specify the fk commit 9c223f01db6e36adbb7570e2aa1bcaec1d142c87 Author: Aaron Patterson Date: Fri Sep 6 15:44:08 2013 -0700 just call the left side :left_side commit 5661622a82154eff877fe0993bfffad13dacad7a Author: Aaron Patterson Date: Fri Sep 6 15:40:36 2013 -0700 initial habtm implementation is working --- activerecord/test/cases/associations/eager_test.rb | 6 ++++++ .../has_and_belongs_to_many_associations_test.rb | 14 ++++---------- .../cases/associations/nested_through_associations_test.rb | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 8d3d6962fc..874ae77ff7 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -747,6 +747,8 @@ class EagerAssociationTest < ActiveRecord::TestCase end def test_eager_with_default_scope_as_block + # warm up the habtm cache + EagerDeveloperWithBlockDefaultScope.where(:name => 'David').first.projects developer = EagerDeveloperWithBlockDefaultScope.where(:name => 'David').first projects = Project.order(:id).to_a assert_no_queries do @@ -1136,6 +1138,10 @@ class EagerAssociationTest < ActiveRecord::TestCase end def test_deep_including_through_habtm + # warm up habtm cache + posts = Post.all.merge!(:includes => {:categories => :categorizations}, :order => "posts.id").to_a + posts[0].categories[0].categorizations.length + posts = Post.all.merge!(:includes => {:categories => :categorizations}, :order => "posts.id").to_a assert_no_queries { assert_equal 2, posts[0].categories[0].categorizations.length } assert_no_queries { assert_equal 1, posts[0].categories[1].categorizations.length } diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index f77066e6ab..be928ec8ee 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -613,7 +613,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase 3, Developer.references(:developers_projects_join).merge( :includes => {:projects => :developers}, - :where => 'developers_projects_join.joined_on IS NOT NULL' + :where => 'projects_developers_projects_join.joined_on IS NOT NULL' ).to_a.size ) end @@ -632,7 +632,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal( 3, Developer.references(:developers_projects_join).merge( - :includes => {:projects => :developers}, :where => 'developers_projects_join.joined_on IS NOT NULL', + :includes => {:projects => :developers}, :where => 'projects_developers_projects_join.joined_on IS NOT NULL', :group => group.join(",") ).to_a.size ) @@ -646,8 +646,8 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end def test_find_scoped_grouped - assert_equal 5, categories(:general).posts_grouped_by_title.size - assert_equal 1, categories(:technology).posts_grouped_by_title.size + assert_equal 5, categories(:general).posts_grouped_by_title.to_a.size + assert_equal 1, categories(:technology).posts_grouped_by_title.to_a.size end def test_find_scoped_grouped_having @@ -718,12 +718,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal project, developer.projects.first end - def test_self_referential_habtm_without_foreign_key_set_should_raise_exception - assert_raise(ActiveRecord::HasAndBelongsToManyAssociationForeignKeyNeeded) { - SelfMember.new.friends - } - end - def test_dynamic_find_should_respect_association_include # SQL error in sort clause if :include is not included # due to Unknown column 'authors.id' diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb index cf3c07845c..95f49a37eb 100644 --- a/activerecord/test/cases/associations/nested_through_associations_test.rb +++ b/activerecord/test/cases/associations/nested_through_associations_test.rb @@ -214,7 +214,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase end def test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflection_preload - authors = assert_queries(3) { Author.includes(:post_categories).to_a.sort_by(&:id) } + authors = assert_queries(4) { Author.includes(:post_categories).to_a.sort_by(&:id) } general, cooking = categories(:general), categories(:cooking) assert_no_queries do @@ -242,7 +242,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase end def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload - categories = assert_queries(3) { Category.includes(:post_comments).to_a.sort_by(&:id) } + categories = assert_queries(4) { Category.includes(:post_comments).to_a.sort_by(&:id) } greetings, more = comments(:greetings), comments(:more_greetings) assert_no_queries do @@ -270,7 +270,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase end def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload - authors = assert_queries(5) { Author.includes(:category_post_comments).to_a.sort_by(&:id) } + authors = assert_queries(6) { Author.includes(:category_post_comments).to_a.sort_by(&:id) } greetings, more = comments(:greetings), comments(:more_greetings) assert_no_queries do -- cgit v1.2.3 From cb0aa02e74d2ee3c79adebaf1657e7544c08018e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 2 Oct 2013 17:22:28 -0700 Subject: preheat habtm column cache --- activerecord/test/cases/associations/nested_through_associations_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb index 95f49a37eb..8ef351cda8 100644 --- a/activerecord/test/cases/associations/nested_through_associations_test.rb +++ b/activerecord/test/cases/associations/nested_through_associations_test.rb @@ -242,6 +242,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase end def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload + Category.includes(:post_comments).to_a # preheat cache categories = assert_queries(4) { Category.includes(:post_comments).to_a.sort_by(&:id) } greetings, more = comments(:greetings), comments(:more_greetings) -- cgit v1.2.3 From 37cd223cb68f1ebb615bffc31bf69e93e6dbf382 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 4 Oct 2013 11:15:55 -0400 Subject: add regression test for set_inverse_instance on add_to_target --- .../test/cases/associations/inverse_associations_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index 2477e60e51..8c81e00865 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -446,6 +446,19 @@ class InverseHasManyTests < ActiveRecord::TestCase def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.secret_interests } end + + def test_child_instance_should_point_to_parent_without_saving + man = Man.new + i = Interest.create(:topic => 'Industrial Revolution Re-enactment') + + man.interests << i + assert_not_nil i.man + + i.man.name = "Charles" + assert_equal i.man.name, man.name + + assert !man.persisted? + end end class InverseBelongsToTests < ActiveRecord::TestCase -- cgit v1.2.3 From 0ee7331c35994e543df396548c5b455c00c96cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 9 Oct 2013 00:48:56 -0300 Subject: Define the association extensions without need to have a builder instance --- activerecord/test/cases/associations/extension_test.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index 47dff7d0ea..f8f2832ab1 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -75,7 +75,6 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase private def extend!(model) - builder = ActiveRecord::Associations::Builder::HasMany.new(:association_name, nil, {}) { } - builder.define_extensions(model) + ActiveRecord::Associations::Builder::HasMany.define_extensions(model, :association_name) { } end end -- cgit v1.2.3 From 91fe499275f8a2240e47f0bbb83cfe03aac1285d Mon Sep 17 00:00:00 2001 From: Arun Agrawal Date: Wed, 9 Oct 2013 14:38:44 +0200 Subject: Using flat_map instead of map and flatten original commit 8998441967a8cfc6e4302c29664ab9d0acd77704 Reverted here ec8ef1e1055c4e1598da13f49d30261f07f4a9b4 --- .../test/cases/associations/has_many_through_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 5299e4e17e..3b61b91d62 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -785,7 +785,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase sarah = Person.create!(:first_name => 'Sarah', :primary_contact_id => people(:susan).id, :gender => 'F', :number1_fan_id => 1) john = Person.create!(:first_name => 'John', :primary_contact_id => sarah.id, :gender => 'M', :number1_fan_id => 1) assert_equal sarah.agents, [john] - assert_equal people(:susan).agents.map(&:agents).flatten, people(:susan).agents_of_agents + assert_equal people(:susan).agents.flat_map(&:agents), people(:susan).agents_of_agents end def test_associate_existing_with_nonstandard_primary_key_on_belongs_to -- cgit v1.2.3 From 05b178085a2ad3bf3f2c0dcd1ea40fb5e8c8dc0d Mon Sep 17 00:00:00 2001 From: Dmitry Polushkin Date: Fri, 1 Mar 2013 01:08:27 +0000 Subject: inversed instance should not be reloaded after stale state was changed check at association reader that record is inverted and should not be reloaded because of stale was changed at target record --- .../test/cases/associations/inverse_associations_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index 8c81e00865..893030345f 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -603,6 +603,18 @@ class InversePolymorphicBelongsToTests < ActiveRecord::TestCase assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to replaced-parent-owned instance" end + def test_inversed_instance_should_not_be_reloaded_after_stale_state_changed + new_man = Man.new + face = Face.new + new_man.face = face + + old_inversed_man = face.man + new_man.save! + new_inversed_man = face.man + + assert_equal old_inversed_man.object_id, new_inversed_man.object_id + end + def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many i = interests(:llama_wrangling) m = i.polymorphic_man -- cgit v1.2.3 From 3d7efb1627e2214aa92e5c74601351465b597f9f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 14 Oct 2013 18:45:47 -0700 Subject: this method does not exist anymore --- activerecord/test/cases/associations/join_dependency_test.rb | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 activerecord/test/cases/associations/join_dependency_test.rb (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/join_dependency_test.rb b/activerecord/test/cases/associations/join_dependency_test.rb deleted file mode 100644 index 08c166dc33..0000000000 --- a/activerecord/test/cases/associations/join_dependency_test.rb +++ /dev/null @@ -1,8 +0,0 @@ -require "cases/helper" -require 'models/edge' - -class JoinDependencyTest < ActiveRecord::TestCase - def test_column_names_with_alias_handles_nil_primary_key - assert_equal Edge.column_names, ActiveRecord::Associations::JoinDependency::JoinBase.new(Edge).column_names_with_alias.map(&:first) - end -end \ No newline at end of file -- cgit v1.2.3 From 8022fc4913d5fa285889795617a1f37c5aa705a9 Mon Sep 17 00:00:00 2001 From: laurocaetano Date: Thu, 24 Oct 2013 00:06:49 -0200 Subject: Save association when primary key is manually set --- .../test/cases/associations/has_one_associations_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 9cd4db8dc9..cdd386187b 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -524,4 +524,15 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_equal 'new name', pirate.ship.reload.name end + def test_has_one_autosave_with_primary_key_manually_set + post = Post.create(id: 1234, title: "Some title", body: 'Some content') + author = Author.new(id: 33, name: 'Hank Moody') + + author.post = post + author.save + author.reload + + assert_not_nil author.post + assert_equal author.post, post + end end -- cgit v1.2.3 From 6b71a1416cb1457517b27cd0a55cb32df0cdf0c3 Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Sun, 27 Oct 2013 19:39:22 +0200 Subject: Skip `include_values` from through associations chains for building target scope Fixes: #12242, #9517, #10240 --- .../test/cases/associations/has_many_through_associations_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 3b61b91d62..b1e823729e 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -1085,4 +1085,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase readers(:michael_authorless).update(first_post_id: 1) assert_equal [posts(:thinking)], person.reload.first_posts end + + def test_has_many_through_with_includes_in_through_association_scope + posts(:welcome).author_address_extra_with_address.to_a + end end -- cgit v1.2.3 From 8a0086609915bc666d08ff416717950da1b6b8da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sun, 27 Oct 2013 18:10:27 -0200 Subject: Assert the return value in the test --- .../test/cases/associations/has_many_through_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index b1e823729e..c450b1beb5 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -1087,6 +1087,6 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end def test_has_many_through_with_includes_in_through_association_scope - posts(:welcome).author_address_extra_with_address.to_a + assert_not_empty posts(:welcome).author_address_extra_with_address end end -- cgit v1.2.3 From e562ddedbae5d44cc98ada8a597fd84e80aea96d Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 28 Oct 2013 21:26:03 -0700 Subject: Add failing test for preloading with a polymorphic association and using the existential predicate --- activerecord/test/cases/associations/eager_test.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 874ae77ff7..05e50cdee2 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -4,6 +4,7 @@ require 'models/tagging' require 'models/tag' require 'models/comment' require 'models/author' +require 'models/essay' require 'models/category' require 'models/company' require 'models/person' @@ -24,7 +25,7 @@ require 'models/categorization' require 'models/sponsor' class EagerAssociationTest < ActiveRecord::TestCase - fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts, + fixtures :posts, :comments, :authors, :essays, :author_addresses, :categories, :categories_posts, :companies, :accounts, :tags, :taggings, :people, :readers, :categorizations, :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books, :developers, :projects, :developers_projects, :members, :memberships, :clubs, :sponsors @@ -1185,4 +1186,12 @@ class EagerAssociationTest < ActiveRecord::TestCase author = Author.includes(:posts).references(:posts).reorder(:name).find_by('posts.title IS NOT NULL') assert_equal authors(:bob), author end + + test "preloading with a polymorphic association and using the existential predicate" do + assert_equal authors(:david), authors(:david).essays.includes(:writer).first.writer + + assert_nothing_raised do + authors(:david).essays.includes(:writer).any? + end + end end -- cgit v1.2.3 From ab177d8cbe3ed208334ce9f550aa7afff65ba334 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 28 Oct 2013 21:48:02 -0700 Subject: Fix broken delete_all test, which will now be failing since #delete_all is broken --- .../test/cases/associations/has_many_associations_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index caa916346a..8bdbd84ad1 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -785,10 +785,10 @@ class HasManyAssociationsTest < ActiveRecord::TestCase companies(:first_firm).clients_of_firm.create("name" => "Another Client") clients = companies(:first_firm).clients_of_firm.to_a assert_equal 2, clients.count - deleted = companies(:first_firm).clients_of_firm.delete_all - assert_equal clients.sort_by(&:id), deleted.sort_by(&:id) - assert_equal 0, companies(:first_firm).clients_of_firm.size - assert_equal 0, companies(:first_firm).clients_of_firm(true).size + + assert_difference "Client.count", -(clients.count) do + companies(:first_firm).clients_of_firm.delete_all + end end def test_delete_all_with_not_yet_loaded_association_collection -- cgit v1.2.3 From 1d75efed8400cb28a7c56e6625be7df31e926c5a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 30 Oct 2013 14:17:54 -0700 Subject: :cut: whitespace --- activerecord/test/cases/associations/eager_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 05e50cdee2..498a4e8144 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1186,10 +1186,10 @@ class EagerAssociationTest < ActiveRecord::TestCase author = Author.includes(:posts).references(:posts).reorder(:name).find_by('posts.title IS NOT NULL') assert_equal authors(:bob), author end - + test "preloading with a polymorphic association and using the existential predicate" do assert_equal authors(:david), authors(:david).essays.includes(:writer).first.writer - + assert_nothing_raised do authors(:david).essays.includes(:writer).any? end -- cgit v1.2.3 From a221b30b3fb309f1edfa0c330fd57c1efaadd509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 1 Nov 2013 14:09:49 -0200 Subject: Test with the right association --- activerecord/test/cases/associations/has_many_associations_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 8bdbd84ad1..b824a0ee7c 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -782,12 +782,12 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_delete_all force_signal37_to_load_all_clients_of_firm - companies(:first_firm).clients_of_firm.create("name" => "Another Client") - clients = companies(:first_firm).clients_of_firm.to_a + companies(:first_firm).dependent_clients_of_firm.create("name" => "Another Client") + clients = companies(:first_firm).dependent_clients_of_firm.to_a assert_equal 2, clients.count assert_difference "Client.count", -(clients.count) do - companies(:first_firm).clients_of_firm.delete_all + companies(:first_firm).dependent_clients_of_firm.delete_all end end -- cgit v1.2.3 From 1918b12c0429caec2a6134ac5e5b42ade103fe90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 1 Nov 2013 19:04:30 -0200 Subject: Fix wrong behavior where associations with dependent: :destroy options was using nullify strategy This caused a regression in applications trying to upgrade. Also if the user set the dependent option as destroy he expects to get the records removed from the database. --- activerecord/test/cases/associations/has_many_associations_test.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index b824a0ee7c..dfc8a68e8c 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -101,11 +101,10 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end def test_do_not_call_callbacks_for_delete_all - bulb_count = Bulb.count car = Car.create(:name => 'honda') car.funky_bulbs.create! assert_nothing_raised { car.reload.funky_bulbs.delete_all } - assert_equal bulb_count + 1, Bulb.count, "bulbs should have been deleted using :nullify strategey" + assert_equal 0, Bulb.count, "bulbs should have been deleted using :delete_all strategey" end def test_building_the_associated_object_with_implicit_sti_base_class @@ -864,7 +863,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, firm.dependent_clients_of_firm.size assert_equal 1, Client.find_by_id(client_id).client_of - # :nullify is called on each client + # :delete_all is called on each client since the dependent options is :destroy firm.dependent_clients_of_firm.clear assert_equal 0, firm.dependent_clients_of_firm.size @@ -872,7 +871,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [], Client.destroyed_client_ids[firm.id] # Should be destroyed since the association is dependent. - assert_nil Client.find_by_id(client_id).client_of + assert_nil Client.find_by_id(client_id) end def test_delete_all_with_option_delete_all -- cgit v1.2.3 From 5741a8aae595950ab9129b3b4aed08adc22e0ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 8 Nov 2013 14:19:09 -0200 Subject: Mark broken test as pending This will avoid the broken window effect in our test suite --- activerecord/test/cases/associations/eager_test.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 498a4e8144..fa54eb28b4 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1188,6 +1188,8 @@ class EagerAssociationTest < ActiveRecord::TestCase end test "preloading with a polymorphic association and using the existential predicate" do + skip 'broken test' + assert_equal authors(:david), authors(:david).essays.includes(:writer).first.writer assert_nothing_raised do -- cgit v1.2.3 From 3ed5642e69b9a132c78bc0b1e54cbdc3753d8a94 Mon Sep 17 00:00:00 2001 From: Denis Redozubov Date: Sat, 9 Nov 2013 19:38:27 +0400 Subject: Fixes problem with replacing has_one association record with itself --- .../cases/associations/has_one_associations_test.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index cdd386187b..a7a8e0c5c6 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -509,16 +509,30 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_no_queries { Firm.new.account = account } end - def test_has_one_assignment_triggers_save_on_change + def test_has_one_assignment_dont_triggers_save_on_change_of_same_object pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?") ship = pirate.build_ship(name: 'old name') ship.save! ship.name = 'new name' assert ship.changed? + assert_queries(1) do + # One query for updating name, not triggering query for updating pirate_id + pirate.ship = ship + end + + assert_equal 'new name', pirate.ship.reload.name + end + + def test_has_one_assignment_triggers_save_on_change_on_replacing_object + pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?") + ship = pirate.build_ship(name: 'old name') + ship.save! + + new_ship = Ship.create(name: 'new name') assert_queries(2) do # One query for updating name and second query for updating pirate_id - pirate.ship = ship + pirate.ship = new_ship end assert_equal 'new name', pirate.ship.reload.name -- cgit v1.2.3 From dbb7ee1bfd16d9aabd3b3ed1a566c8752e9af3c0 Mon Sep 17 00:00:00 2001 From: dm1try Date: Mon, 11 Nov 2013 19:41:54 +0300 Subject: Prevent the counter cache from being decremented twice when destroying a record on a has_many :through association. :destroy method has own counter_cache callbacks. --- .../cases/associations/has_many_through_associations_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index c450b1beb5..47592f312e 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -514,6 +514,15 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal(post.taggings.count, post.taggings_count) end + def test_update_counter_caches_on_destroy + post = posts(:welcome) + tag = post.tags.create!(name: 'doomed') + + assert_difference 'post.reload.taggings_count', -1 do + tag.tagged_posts.destroy(post) + end + end + def test_replace_association assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)} -- cgit v1.2.3 From 421c81bd1875eb4e163bea8ce18b1ae9c2224e7d Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 14 Nov 2013 14:43:14 -0800 Subject: Fix that eager loading of polymorphic associations did not work with association empty?/any? predicates any more (there is still a problem when select is applied to a relation, or if you try association#exists? -- but its easier to work around) --- activerecord/test/cases/associations/eager_test.rb | 2 -- activerecord/test/cases/associations/inner_join_association_test.rb | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index fa54eb28b4..498a4e8144 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1188,8 +1188,6 @@ class EagerAssociationTest < ActiveRecord::TestCase end test "preloading with a polymorphic association and using the existential predicate" do - skip 'broken test' - assert_equal authors(:david), authors(:david).essays.includes(:writer).first.writer assert_nothing_raised do diff --git a/activerecord/test/cases/associations/inner_join_association_test.rb b/activerecord/test/cases/associations/inner_join_association_test.rb index 9fe5ff50d9..dffee42e7d 100644 --- a/activerecord/test/cases/associations/inner_join_association_test.rb +++ b/activerecord/test/cases/associations/inner_join_association_test.rb @@ -70,7 +70,7 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase end def test_find_with_implicit_inner_joins_does_not_set_associations - authors = Author.joins(:posts).select('authors.*') + authors = Author.joins(:posts).select('authors.*').to_a assert !authors.empty?, "expected authors to be non-empty" assert authors.all? { |a| !a.instance_variable_defined?(:@posts) }, "expected no authors to have the @posts association loaded" end -- cgit v1.2.3 From 6b84de457589335754b0cd2f26d620ce93c3de82 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 14 Nov 2013 23:25:41 -0200 Subject: Fix test name [ci skip] --- activerecord/test/cases/associations/has_one_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index a7a8e0c5c6..1f78c73f71 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -509,7 +509,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_no_queries { Firm.new.account = account } end - def test_has_one_assignment_dont_triggers_save_on_change_of_same_object + def test_has_one_assignment_dont_trigger_save_on_change_of_same_object pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?") ship = pirate.build_ship(name: 'old name') ship.save! -- cgit v1.2.3 From 02ca5580bd5f70dbd5ffcd147da6b99e2c90c265 Mon Sep 17 00:00:00 2001 From: Edo Balvers Date: Sat, 16 Nov 2013 21:18:02 +0100 Subject: Checks to see if the record contains the foreign_key to set the inverse automatically --- activerecord/test/cases/associations/has_many_associations_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index dfc8a68e8c..b11d27467b 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -460,6 +460,10 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal ['id'], posts(:welcome).comments.select(:id).first.attributes.keys end + def test_select_without_foreign_key + assert_equal companies(:first_firm).accounts.first.credit_limit, companies(:first_firm).accounts.select(:credit_limit).first.credit_limit + end + def test_adding force_signal37_to_load_all_clients_of_firm natural = Client.new("name" => "Natural Company") -- cgit v1.2.3 From 64b9e93bb571160315987862583a83222e506734 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Wed, 20 Nov 2013 21:51:35 +0000 Subject: Fix ActiveRecord::Relation#unscope I'm pretty confused about the addition of this method. The documentation says that it was intended to allow the removal of values from the default scope (in contrast to #except). However it behaves exactly the same as except: https://gist.github.com/jonleighton/7537008 (other than having a slightly enhanced syntax). The removal of the default scope is allowed by 94924dc32baf78f13e289172534c2e71c9c8cade, which was not a change we could make until 4.1 due to the need to deprecate things. However after that change #unscope still gives us nothing that #except doesn't already give us. However there *is* a desire to be able to unscope stuff in a way that persists across merges, which would allow associations to be defined which unscope stuff from the default scope of the associated model. E.g. has_many :comments, -> { unscope where: :trashed } So that's what this change implements. I've also corrected the documentation. I removed the guide references to #except as I think unscope really supercedes #except now. While we're here, there's also a potential desire to be able to write this: has_many :comments, -> { unscoped } However, it doesn't make sense and would not be straightforward to implement. While with #unscope we're specifying exactly what we want to be removed from the relation, with "unscoped" we're just saying that we want it to not have some things which were added earlier on by the default scope. However in the case of an association, we surely don't want *all* conditions to be removed, otherwise the above would just become "SELECT * FROM comments" with no foreign key constraint. To make the above work, we'd have to somehow tag the relation values which get added when evaluating the default scope in order to differentiate them from other relation values. Which is way too much complexity and therefore not worth it when most use cases can be satisfied with unscope. Closes #10643, #11061. --- .../test/cases/associations/has_many_associations_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index b11d27467b..359bcfba5f 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1761,4 +1761,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, speedometer.minivans.to_a.size, "Only one association should be present:\n#{speedometer.minivans.to_a}" assert_equal 1, speedometer.reload.minivans.to_a.size end + + test "can unscope the default scope of the associated model" do + car = Car.create! + bulb1 = Bulb.create! name: "defaulty", car: car + bulb2 = Bulb.create! name: "other", car: car + + assert_equal [bulb1], car.bulbs + assert_equal [bulb1, bulb2], car.all_bulbs.sort_by(&:id) + end end -- cgit v1.2.3 From 82de1eda7c8a8f11b65d8edf70c4af7ce08507ec Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Mon, 25 Nov 2013 23:56:57 +0530 Subject: Fix some minor typos [ci skip] --- activerecord/test/cases/associations/has_many_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 359bcfba5f..a025d49fa3 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -104,7 +104,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase car = Car.create(:name => 'honda') car.funky_bulbs.create! assert_nothing_raised { car.reload.funky_bulbs.delete_all } - assert_equal 0, Bulb.count, "bulbs should have been deleted using :delete_all strategey" + assert_equal 0, Bulb.count, "bulbs should have been deleted using :delete_all strategy" end def test_building_the_associated_object_with_implicit_sti_base_class -- cgit v1.2.3 From 5aab0c053832ded70a3a4b58cb97f8f8bba796ba Mon Sep 17 00:00:00 2001 From: Brian Thomas Storti Date: Sat, 23 Nov 2013 09:24:52 -0200 Subject: Raise `RecordNotDestroyed` when children can't be replaced Fixes #12812 Raise `ActiveRecord::RecordNotDestroyed` when a child marked with `dependent: destroy` can't be destroyed. The following code: ```ruby class Post < ActiveRecord::Base has_many :comments, dependent: :destroy end class Comment < ActiveRecord::Base before_destroy do return false end end post = Post.create!(comments: [Comment.create!]) post.comments = [Comment.create!] ```` would result in a `post` with two `comments`. With this commit, the same code would raise a `RecordNotDestroyed` exception, keeping the `post` with the same `comment`. --- .../test/cases/associations/has_many_associations_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index a025d49fa3..45bc974025 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1770,4 +1770,15 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [bulb1], car.bulbs assert_equal [bulb1, bulb2], car.all_bulbs.sort_by(&:id) end + + test "raises RecordNotDestroyed when replaced child can't be destroyed" do + car = Car.create! + original_child = FailedBulb.create!(car: car) + + assert_raise(ActiveRecord::RecordNotDestroyed) do + car.failed_bulbs = [FailedBulb.create!] + end + + assert_equal [original_child], car.reload.failed_bulbs + end end -- cgit v1.2.3 From 45d4d141f971e50e2df40bbf3559ee254ebc81b9 Mon Sep 17 00:00:00 2001 From: heruku Date: Sun, 24 Nov 2013 03:47:17 -0600 Subject: changed update counter to act on unscoped model --- .../test/cases/associations/has_many_associations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 45bc974025..bfb80afa61 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1781,4 +1781,12 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [original_child], car.reload.failed_bulbs end + + test 'updates counter cache when default scope is given' do + topic = DefaultRejectedTopic.create approved: true + + assert_difference "topic.reload.replies_count", 1 do + topic.approved_replies.create! + end + end end -- cgit v1.2.3 From 647cff3a134772bc6374b89e95d13497e23b5309 Mon Sep 17 00:00:00 2001 From: Kuldeep Aggarwal Date: Fri, 29 Nov 2013 18:49:56 +0530 Subject: updating options documentation for associations removed unnecessary test case and improved test case for belongs_to having invalid options --- .../test/cases/associations/belongs_to_associations_test.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index a79f145e31..7c913bc78b 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -619,16 +619,11 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids end - def test_invalid_belongs_to_dependent_option_nullify_raises_exception - assert_raise ArgumentError do + def test_belongs_to_invalid_dependent_option_raises_exception + error = assert_raise ArgumentError do Class.new(Author).belongs_to :special_author_address, :dependent => :nullify end - end - - def test_invalid_belongs_to_dependent_option_restrict_raises_exception - assert_raise ArgumentError do - Class.new(Author).belongs_to :special_author_address, :dependent => :restrict - end + assert_equal error.message, 'The :dependent option must be one of [:destroy, :delete], but is :nullify' end def test_attributes_are_being_set_when_initialized_from_belongs_to_association_with_where_clause -- cgit v1.2.3 From 35fd2d401938df1afc595de9b87dadd4421f44a5 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Fri, 29 Nov 2013 19:21:49 -0800 Subject: Raise `ArgumentError` when `has_one` is used with `counter_cache` Previously, the `has_one` macro incorrectly accepts the `counter_cache` option due to a bug, although that options was never supported nor functional on `has_one` and `has_one ... through` relationships. It now correctly raises an `ArgumentError` when passed that option. For reference, this bug was introduced in 52f8e4b9. --- activerecord/test/cases/associations/has_one_associations_test.rb | 8 ++++++++ .../test/cases/associations/has_one_through_associations_test.rb | 8 ++++++++ 2 files changed, 16 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 1f78c73f71..5a41461edf 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -549,4 +549,12 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_not_nil author.post assert_equal author.post, post end + + def test_has_one_relationship_cannot_have_a_counter_cache + assert_raise(ArgumentError) do + Class.new(ActiveRecord::Base) do + has_one :thing, counter_cache: true + end + end + end end diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index f2723f2e18..a2725441b3 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -315,4 +315,12 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase def test_has_one_through_with_custom_select_on_join_model_default_scope assert_equal clubs(:boring_club), members(:groucho).selected_club end + + def test_has_one_through_relationship_cannot_have_a_counter_cache + assert_raise(ArgumentError) do + Class.new(ActiveRecord::Base) do + has_one :thing, through: :other_thing, counter_cache: true + end + end + end end -- cgit v1.2.3 From 7a036ebd30a6333f22684bdd33dcf5ad4d101d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 11 Dec 2013 19:28:32 -0200 Subject: Revert the whole refactoring in the association builder classes. This is to get activerecord-deprecated_finders work again --- activerecord/test/cases/associations/extension_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index f8f2832ab1..4c1fdfdd9a 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -75,6 +75,7 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase private def extend!(model) - ActiveRecord::Associations::Builder::HasMany.define_extensions(model, :association_name) { } + builder = ActiveRecord::Associations::Builder::HasMany.new(model, :association_name, nil, {}) { } + builder.define_extensions(model) end end -- cgit v1.2.3 From da3891c898fd79ab28dad3ce4c9e52d876c9e4e9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 12 Dec 2013 10:43:36 -0800 Subject: make sure cached table name is a string. fixes #12582 --- .../associations/has_and_belongs_to_many_associations_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index be928ec8ee..8aee7ff40e 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -570,6 +570,13 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert !developer.special_projects.include?(other_project) end + def test_symbol_join_table + developer = Developer.first + sp = developer.sym_special_projects.create("name" => "omg") + developer.reload + assert_includes developer.sym_special_projects, sp + end + def test_update_attributes_after_push_without_duplicate_join_table_rows developer = Developer.new("name" => "Kano") project = SpecialProject.create("name" => "Special Project") -- cgit v1.2.3 From c141dfc838a5dca9f197814410fa5d44c143129c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 17 Dec 2013 21:45:55 -0700 Subject: Add a failing test for assigning nil to a polymorphic belongs_to not nullifying its _type column --- .../test/cases/associations/belongs_to_associations_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 7c913bc78b..6d01fcf50c 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -578,6 +578,19 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_nil essay.writer_id end + def test_polymorphic_assignment_with_nil + essay = Essay.new + assert_nil essay.writer_id + assert_nil essay.writer_type + + essay.writer_id = 1 + essay.writer_type = 'Author' + + essay.writer = nil + assert_nil essay.writer_id + assert_nil essay.writer_type + end + def test_belongs_to_proxy_should_not_respond_to_private_methods assert_raise(NoMethodError) { companies(:first_firm).private_method } assert_raise(NoMethodError) { companies(:second_client).firm.private_method } -- cgit v1.2.3 From c5b76b5362e4ba28ff3a5649156c50dff9a86de7 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 19 Dec 2013 09:03:39 -0200 Subject: Prefer assert_raise instead of flunk + rescue to test for exceptions Change most tests to make use of assert_raise returning the raised exception rather than relying on a combination of flunk + rescue to check for exception types/messages. --- .../test/cases/associations/has_many_associations_test.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index bfb80afa61..c6291e8aa4 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -318,9 +318,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_belongs_to_sanity c = Client.new - assert_nil c.firm - - flunk "belongs_to failed if check" if c.firm + assert_nil c.firm, "belongs_to failed sanity check on new object" end def test_find_ids @@ -1781,12 +1779,12 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [original_child], car.reload.failed_bulbs end - + test 'updates counter cache when default scope is given' do topic = DefaultRejectedTopic.create approved: true assert_difference "topic.reload.replies_count", 1 do topic.approved_replies.create! end - end + end end -- cgit v1.2.3 From 2bcf7158d346e1b619aebbbec360ee0153ef8d06 Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Sun, 22 Dec 2013 18:18:40 +0200 Subject: On destroying do not touch destroyed belongs to association. Fixes: #13445 --- .../test/cases/associations/belongs_to_associations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 6d01fcf50c..3205d0c28b 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -356,6 +356,14 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_queries(2) { line_item.destroy } end + def test_belongs_to_with_touch_option_on_destroy_with_destroyed_parent + line_item = LineItem.create! + invoice = Invoice.create!(line_items: [line_item]) + invoice.destroy + + assert_queries(1) { line_item.destroy } + end + def test_belongs_to_with_touch_option_on_touch_and_reassigned_parent line_item = LineItem.create! Invoice.create!(line_items: [line_item]) -- cgit v1.2.3 From bb17c3b2105da30ef3260c3b489da85e4865eaa5 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Tue, 31 Dec 2013 02:44:27 +0530 Subject: https://github.com/rails/rails/commit/2075f39d726cef361170218fd16421fc52bed5a8 introduced a regression in includes/preloades by calling `read_attribute` on an association when preloading takes places, instead of using loaded records in `association.target`. tl;dr Records are not made properly available via `read_attribute` when preloding in simultaneous, but value of `@loaded` is already set true, and records concatenated in `association.target` on an association object. When `@loaded` is true we return an object of `AlreadyLoaded` in preload_for. In `AlreadyLoaded` to return preloaded records we make wrong use of `read_attribute`, instead of `target` records. The regression is fixed by making use of the loaded records in `association.target` when the preloading takes place. Fixes #13437 --- .../test/cases/associations/cascaded_eager_loading_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index 811d91f849..904d85266e 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -174,4 +174,18 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase sink = Vertex.all.merge!(:includes=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC').first assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first } end + + def test_eager_association_loading_with_cascaded_interdependent_one_level_and_two_levels + authors_relation = Author.all.merge!(:includes => [:comments, {:posts => :categorizations}], :order => "authors.id") + assert_nothing_raised do + authors_relation.to_a + end + authors = authors_relation.to_a + assert_equal 3, authors.size + assert_equal 10, authors[0].comments.size + assert_equal 1, authors[1].comments.size + assert_equal 5, authors[0].posts.size + assert_equal 3, authors[1].posts.size + assert_equal 3, authors[0].posts.collect { |post| post.categorizations.size }.inject(0) { |sum, i| sum+i } + end end -- cgit v1.2.3 From da0463cf5e93905978e5f9f8b64e75fc33a9c7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 1 Jan 2014 14:15:01 -0200 Subject: Improve the tests to not call assert_nothing_raised --- activerecord/test/cases/associations/cascaded_eager_loading_test.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index 904d85266e..71c0609df5 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -176,10 +176,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase end def test_eager_association_loading_with_cascaded_interdependent_one_level_and_two_levels - authors_relation = Author.all.merge!(:includes => [:comments, {:posts => :categorizations}], :order => "authors.id") - assert_nothing_raised do - authors_relation.to_a - end + authors_relation = Author.all.merge!(includes: [:comments, { posts: :categorizations }], order: "authors.id") authors = authors_relation.to_a assert_equal 3, authors.size assert_equal 10, authors[0].comments.size -- cgit v1.2.3 From bf556880f22e759d5de6b203671636d82ecc2f18 Mon Sep 17 00:00:00 2001 From: Matthias Zirnstein Date: Sun, 5 Jan 2014 17:55:39 +0100 Subject: Remove method redefined warnings for test suite has_many definitions with "name" as singular and as plural e.g. has_many :welcome_posts_with_comment has_many :welcome_posts_with_comments Ruby mentions it with: lib/active_record/associations/builder/collection_association.rb:65: warning: method redefined; discarding old welcome_posts_with_comment_ids lib/active_record/associations/builder/collection_association.rb:65: warning: previous definition of welcome_posts_with_comment_ids was here lib/active_record/associations/builder/collection_association.rb:75: warning: method redefined; discarding old welcome_posts_with_comment_ids= lib/active_record/associations/builder/collection_association.rb:75: warning: previous definition of welcome_posts_with_comment_ids= was here --- activerecord/test/cases/associations/inner_join_association_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/inner_join_association_test.rb b/activerecord/test/cases/associations/inner_join_association_test.rb index dffee42e7d..a9efa6d86a 100644 --- a/activerecord/test/cases/associations/inner_join_association_test.rb +++ b/activerecord/test/cases/associations/inner_join_association_test.rb @@ -42,7 +42,7 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase end def test_join_association_conditions_support_string_and_arel_expressions - assert_equal 0, Author.joins(:welcome_posts_with_comment).count + assert_equal 0, Author.joins(:welcome_posts_with_one_comment).count assert_equal 1, Author.joins(:welcome_posts_with_comments).count end -- cgit v1.2.3