aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/deprecated_associations_test.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-15 17:45:16 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-15 17:45:16 +0000
commit823554eafef9e8ee8fe2788f6231a3e665c2cbbf (patch)
tree6059c8e2c943a7fb45a56bf80cc5786934b70de1 /activerecord/test/deprecated_associations_test.rb
parent62f0512e54d594c4bb6fcb8d16101fdeb87b89e8 (diff)
downloadrails-823554eafef9e8ee8fe2788f6231a3e665c2cbbf.tar.gz
rails-823554eafef9e8ee8fe2788f6231a3e665c2cbbf.tar.bz2
rails-823554eafef9e8ee8fe2788f6231a3e665c2cbbf.zip
Added support for associating unsaved objects #402 [Tim Bates]
Added replace to associations, so you can do project.manager.replace(new_manager) or project.milestones.replace(new_milestones) #402 [Tim Bates] Added build and create methods to has_one and belongs_to associations, so you can now do project.manager.build(attributes) #402 [Tim Bates] Fixed that Base#== wouldn't work for multiple references to the same unsaved object #402 [Tim Bates] Added that if a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last. #402 [Tim Bates] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@417 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/deprecated_associations_test.rb')
-rwxr-xr-xactiverecord/test/deprecated_associations_test.rb38
1 files changed, 36 insertions, 2 deletions
diff --git a/activerecord/test/deprecated_associations_test.rb b/activerecord/test/deprecated_associations_test.rb
index ed1d3c4055..f746275215 100755
--- a/activerecord/test/deprecated_associations_test.rb
+++ b/activerecord/test/deprecated_associations_test.rb
@@ -138,14 +138,14 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
end
def test_force_reload
- firm = Firm.new
+ firm = Firm.new("name" => "A New Firm, Inc")
firm.save
firm.clients.each {|c|} # forcing to load all clients
assert firm.clients.empty?, "New firm shouldn't have client objects"
assert !firm.has_clients?, "New firm shouldn't have clients"
assert_equal 0, firm.clients_count, "New firm should have 0 clients"
- client = Client.new("firm_id" => firm.id)
+ client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
client.save
assert firm.clients.empty?, "New firm should have cached no client objects"
@@ -340,4 +340,38 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
assert_equal 2, Firm.find_first.find_all_in_clients("type = 'Client'").length
assert_equal 1, Firm.find_first.find_all_in_clients("name = 'Summit'").length
end
+
+ def test_has_one
+ assert @signals37.account?(Account.find(1))
+ assert @signals37.has_account?, "37signals should have an account"
+ assert Account.find(1).firm?(@signals37), "37signals account should be able to backtrack"
+ assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
+
+ assert !Account.find(2).has_firm?, "Unknown isn't linked"
+ assert !Account.find(2).firm?(@signals37), "Unknown isn't linked"
+ end
+
+ def test_has_one_build
+ firm = Firm.new("name" => "GlobalMegaCorp")
+ assert firm.save
+
+ account = firm.build_account("credit_limit" => 1000)
+ assert account.save
+ assert_equal account, firm.account
+ end
+
+ def test_has_one_failing_build_association
+ firm = Firm.new("name" => "GlobalMegaCorp")
+ firm.save
+
+ account = firm.build_account
+ assert !account.save
+ assert_equal "can't be empty", account.errors.on("credit_limit")
+ end
+
+ def test_has_one_create
+ firm = Firm.new("name" => "GlobalMegaCorp")
+ firm.save
+ assert_equal firm.create_account("credit_limit" => 1000), firm.account
+ end
end