From 3704088ebde5ef074d186bff0d380858a9a01055 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 29 Aug 2006 17:06:27 +0000 Subject: has_one supports the :dependent => :delete option which skips the typical callback chain and deletes the associated object directly from the database. Closes #5927. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4848 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations_test.rb | 18 ++++++++++++++++++ activerecord/test/calculations_test.rb | 16 ++++++++-------- activerecord/test/finder_test.rb | 4 ++-- activerecord/test/fixtures/accounts.yml | 7 ++++++- activerecord/test/fixtures/companies.yml | 7 ++++++- activerecord/test/fixtures/company.rb | 15 +++++++++++++++ activerecord/test/inheritance_test.rb | 2 +- 7 files changed, 56 insertions(+), 13 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 6e93b7430c..100b30f126 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -91,6 +91,10 @@ end class HasOneAssociationsTest < Test::Unit::TestCase fixtures :accounts, :companies, :developers, :projects, :developers_projects + def setup + Account.destroyed_account_ids.clear + end + def test_has_one assert_equal companies(:first_firm).account, Account.find(1) assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit @@ -168,8 +172,22 @@ class HasOneAssociationsTest < Test::Unit::TestCase num_accounts = Account.count firm = Firm.find(1) assert !firm.account.nil? + account_id = firm.account.id + assert_equal [], Account.destroyed_account_ids[firm.id] firm.destroy assert_equal num_accounts - 1, Account.count + assert_equal [account_id], Account.destroyed_account_ids[firm.id] + end + + def test_exclusive_dependence + num_accounts = Account.count + firm = ExclusivelyDependentFirm.find(9) + assert !firm.account.nil? + account_id = firm.account.id + assert_equal [], Account.destroyed_account_ids[firm.id] + firm.destroy + assert_equal num_accounts - 1, Account.count + assert_equal [], Account.destroyed_account_ids[firm.id] end def test_succesful_build_association diff --git a/activerecord/test/calculations_test.rb b/activerecord/test/calculations_test.rb index 9a0d0434a5..397b7a9026 100644 --- a/activerecord/test/calculations_test.rb +++ b/activerecord/test/calculations_test.rb @@ -8,7 +8,7 @@ class CalculationsTest < Test::Unit::TestCase fixtures :companies, :accounts, :topics def test_should_sum_field - assert_equal 265, Account.sum(:credit_limit) + assert_equal 318, Account.sum(:credit_limit) end def test_should_average_field @@ -49,13 +49,13 @@ class CalculationsTest < Test::Unit::TestCase def test_should_order_by_grouped_field c = Account.sum(:credit_limit, :group => :firm_id, :order => "firm_id") - assert_equal [1, 2, 6], c.keys.compact + assert_equal [1, 2, 6, 9], c.keys.compact end def test_should_order_by_calculation c = Account.sum(:credit_limit, :group => :firm_id, :order => "sum_credit_limit desc, firm_id") - assert_equal [105, 60, 50, 50], c.keys.collect { |k| c[k] } - assert_equal [6, 2, 1], c.keys.compact + assert_equal [105, 60, 53, 50, 50], c.keys.collect { |k| c[k] } + assert_equal [6, 2, 9, 1], c.keys.compact end def test_should_limit_calculation @@ -114,8 +114,8 @@ class CalculationsTest < Test::Unit::TestCase end def test_should_calculate_with_invalid_field - assert_equal 5, Account.calculate(:count, '*') - assert_equal 5, Account.calculate(:count, :all) + assert_equal 6, Account.calculate(:count, '*') + assert_equal 6, Account.calculate(:count, :all) end def test_should_calculate_grouped_with_invalid_field @@ -193,7 +193,7 @@ class CalculationsTest < Test::Unit::TestCase end def test_should_count_selected_field_with_include - assert_equal 5, Account.count(:distinct => true, :include => :firm) - assert_equal 3, Account.count(:distinct => true, :include => :firm, :select => :credit_limit) + assert_equal 6, Account.count(:distinct => true, :include => :firm) + assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit) end end diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb index 4a79f129c0..bd72348916 100644 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -454,8 +454,8 @@ class FinderTest < Test::Unit::TestCase end def test_select_values - assert_equal ["1","2","3","4","5","6","7","8"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s } - assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel"], Company.connection.select_values("SELECT name FROM companies ORDER BY id") + assert_equal ["1","2","3","4","5","6","7","8","9"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s } + assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy"], Company.connection.select_values("SELECT name FROM companies ORDER BY id") end protected diff --git a/activerecord/test/fixtures/accounts.yml b/activerecord/test/fixtures/accounts.yml index a3d6742d79..b2d0191900 100644 --- a/activerecord/test/fixtures/accounts.yml +++ b/activerecord/test/fixtures/accounts.yml @@ -20,4 +20,9 @@ last_account: rails_core_account_2: id: 5 firm_id: 6 - credit_limit: 55 \ No newline at end of file + credit_limit: 55 + +odegy_account: + id: 6 + firm_id: 9 + credit_limit: 53 diff --git a/activerecord/test/fixtures/companies.yml b/activerecord/test/fixtures/companies.yml index f2e638d382..c61128c09b 100644 --- a/activerecord/test/fixtures/companies.yml +++ b/activerecord/test/fixtures/companies.yml @@ -47,4 +47,9 @@ leetsoft: jadedpixel: id: 8 name: Jadedpixel - client_of: 6 \ No newline at end of file + client_of: 6 + +odegy: + id: 9 + name: Odegy + type: ExclusivelyDependentFirm diff --git a/activerecord/test/fixtures/company.rb b/activerecord/test/fixtures/company.rb index 250deebb21..a55632360f 100755 --- a/activerecord/test/fixtures/company.rb +++ b/activerecord/test/fixtures/company.rb @@ -42,6 +42,9 @@ class DependentFirm < Company has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :nullify end +class ExclusivelyDependentFirm < Company + has_one :account, :foreign_key => "firm_id", :dependent => :delete +end class Client < Company belongs_to :firm, :foreign_key => "client_of" @@ -83,6 +86,18 @@ end class Account < ActiveRecord::Base belongs_to :firm + def self.destroyed_account_ids + @destroyed_account_ids ||= Hash.new { |h,k| h[k] = [] } + end + + before_destroy do |account| + if account.firm + Account.destroyed_account_ids[account.firm.id] << account.id + end + true + end + + protected def validate errors.add_on_empty "credit_limit" diff --git a/activerecord/test/inheritance_test.rb b/activerecord/test/inheritance_test.rb index db10e1f048..65f5d70a1f 100755 --- a/activerecord/test/inheritance_test.rb +++ b/activerecord/test/inheritance_test.rb @@ -58,7 +58,7 @@ class InheritanceTest < Test::Unit::TestCase end def test_inheritance_condition - assert_equal 8, Company.count + assert_equal 9, Company.count assert_equal 2, Firm.count assert_equal 3, Client.count end -- cgit v1.2.3