aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/test')
-rwxr-xr-xactiverecord/test/cases/associations_test.rb19
-rw-r--r--activerecord/test/cases/json_serialization_test.rb2
-rw-r--r--activerecord/test/fixtures/authors.yml2
-rw-r--r--activerecord/test/models/author.rb14
-rw-r--r--activerecord/test/schema/schema.rb2
5 files changed, 35 insertions, 4 deletions
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb
index b21f0cc9e2..301f8a6e2c 100755
--- a/activerecord/test/cases/associations_test.rb
+++ b/activerecord/test/cases/associations_test.rb
@@ -470,7 +470,7 @@ end
class HasManyAssociationsTest < ActiveSupport::TestCase
fixtures :accounts, :companies, :developers, :projects,
- :developers_projects, :topics, :authors, :comments
+ :developers_projects, :topics, :authors, :comments, :author_addresses
def setup
Client.destroyed_client_ids.clear
@@ -995,6 +995,23 @@ class HasManyAssociationsTest < ActiveSupport::TestCase
assert_equal 1, Client.find_all_by_client_of(firm.id).size
end
+ def test_dependent_delete_and_destroy_with_belongs_to
+ author_address = author_addresses(:david_address)
+ assert_equal [], AuthorAddress.destroyed_author_address_ids[authors(:david).id]
+
+ assert_difference "AuthorAddress.count", -2 do
+ authors(:david).destroy
+ end
+
+ assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids[authors(:david).id]
+ end
+
+ def test_invalid_belongs_to_dependent_option_raises_exception
+ assert_raises ArgumentError do
+ Author.belongs_to :special_author_address, :dependent => :nullify
+ end
+ end
+
def test_clearing_without_initial_access
firm = companies(:first_firm)
diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb
index 465c2454fc..779b8e02ad 100644
--- a/activerecord/test/cases/json_serialization_test.rb
+++ b/activerecord/test/cases/json_serialization_test.rb
@@ -151,7 +151,7 @@ class DatabaseConnectedJsonEncodingTest < ActiveSupport::TestCase
def test_should_allow_except_option_for_list_of_authors
authors = [@david, @mary]
- assert_equal %([{"id": 1}, {"id": 2}]), authors.to_json(:except => [:name, :author_address_id])
+ assert_equal %([{"id": 1}, {"id": 2}]), authors.to_json(:except => [:name, :author_address_id, :author_address_extra_id])
end
def test_should_allow_includes_for_list_of_authors
diff --git a/activerecord/test/fixtures/authors.yml b/activerecord/test/fixtures/authors.yml
index f59b84fa45..de2ec7d38b 100644
--- a/activerecord/test/fixtures/authors.yml
+++ b/activerecord/test/fixtures/authors.yml
@@ -1,6 +1,8 @@
david:
id: 1
name: David
+ author_address_id: 1
+ author_address_extra_id: 2
mary:
id: 2
diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb
index bf0c993c50..593d77342e 100644
--- a/activerecord/test/models/author.rb
+++ b/activerecord/test/models/author.rb
@@ -65,7 +65,8 @@ class Author < ActiveRecord::Base
has_many :tags, :through => :posts # through has_many :through
has_many :post_categories, :through => :posts, :source => :categories
- belongs_to :author_address
+ belongs_to :author_address, :dependent => :destroy
+ belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
attr_accessor :post_log
@@ -101,6 +102,17 @@ end
class AuthorAddress < ActiveRecord::Base
has_one :author
+
+ def self.destroyed_author_address_ids
+ @destroyed_author_address_ids ||= Hash.new { |h,k| h[k] = [] }
+ end
+
+ before_destroy do |author_address|
+ if author_address.author
+ AuthorAddress.destroyed_author_address_ids[author_address.author.id] << author_address.id
+ end
+ true
+ end
end
class AuthorFavorite < ActiveRecord::Base
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 2d187a601a..307ad8b935 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -239,9 +239,9 @@ ActiveRecord::Schema.define do
add_column :posts, :taggings_count, :integer, :default => 0
add_column :authors, :author_address_id, :integer
+ add_column :authors, :author_address_extra_id, :integer
create_table :author_addresses, :force => true do |t|
- t.column :author_address_id, :integer
end
create_table :author_favorites, :force => true do |t|