提交 800b899f 编写于 作者: J Jeremy Kemper

Remove deprecated push_with_attributes.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6997 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 813e8ba9
...@@ -5,7 +5,7 @@ def initialize(owner, reflection) ...@@ -5,7 +5,7 @@ def initialize(owner, reflection)
super super
construct_sql construct_sql
end end
def build(attributes = {}) def build(attributes = {})
load_target load_target
record = @reflection.klass.new(attributes) record = @reflection.klass.new(attributes)
...@@ -27,7 +27,7 @@ def create(attributes = {}) ...@@ -27,7 +27,7 @@ def create(attributes = {})
def find_first def find_first
load_target.first load_target.first
end end
def find(*args) def find(*args)
options = Base.send(:extract_options_from_args!, args) options = Base.send(:extract_options_from_args!, args)
...@@ -66,24 +66,7 @@ def find(*args) ...@@ -66,24 +66,7 @@ def find(*args)
args << options args << options
@reflection.klass.find(*args) @reflection.klass.find(*args)
end end
end
# Deprecated as of Rails 1.2. If your associations require attributes
# you should be using has_many :through
def push_with_attributes(record, join_attributes = {})
raise_on_type_mismatch(record)
join_attributes.each { |key, value| record[key.to_s] = value }
callback(:before_add, record)
insert_record(record) unless @owner.new_record?
@target << record
callback(:after_add, record)
self
end end
deprecate :push_with_attributes => "consider using has_many :through instead"
alias :concat_with_attributes :push_with_attributes
protected protected
def count_records def count_records
...@@ -121,10 +104,10 @@ def insert_record(record) ...@@ -121,10 +104,10 @@ def insert_record(record)
@owner.connection.execute(sql) @owner.connection.execute(sql)
end end
return true return true
end end
def delete_records(records) def delete_records(records)
if sql = @reflection.options[:delete_sql] if sql = @reflection.options[:delete_sql]
records.each { |record| @owner.connection.execute(interpolate_sql(sql, record)) } records.each { |record| @owner.connection.execute(interpolate_sql(sql, record)) }
...@@ -134,7 +117,7 @@ def delete_records(records) ...@@ -134,7 +117,7 @@ def delete_records(records)
@owner.connection.execute(sql) @owner.connection.execute(sql)
end end
end end
def construct_sql def construct_sql
interpolate_sql_options!(@reflection.options, :finder_sql) interpolate_sql_options!(@reflection.options, :finder_sql)
......
...@@ -15,29 +15,29 @@ def setup ...@@ -15,29 +15,29 @@ def setup
@authorless = posts(:authorless) @authorless = posts(:authorless)
assert @david.post_log.empty? assert @david.post_log.empty?
end end
def test_adding_macro_callbacks def test_adding_macro_callbacks
@david.posts_with_callbacks << @thinking @david.posts_with_callbacks << @thinking
assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
@david.posts_with_callbacks << @thinking @david.posts_with_callbacks << @thinking
assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}", assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
"after_adding#{@thinking.id}"], @david.post_log "after_adding#{@thinking.id}"], @david.post_log
end end
def test_adding_with_proc_callbacks def test_adding_with_proc_callbacks
@david.posts_with_proc_callbacks << @thinking @david.posts_with_proc_callbacks << @thinking
assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
@david.posts_with_proc_callbacks << @thinking @david.posts_with_proc_callbacks << @thinking
assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}", assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
"after_adding#{@thinking.id}"], @david.post_log "after_adding#{@thinking.id}"], @david.post_log
end end
def test_removing_with_macro_callbacks def test_removing_with_macro_callbacks
first_post, second_post = @david.posts_with_callbacks[0, 2] first_post, second_post = @david.posts_with_callbacks[0, 2]
@david.posts_with_callbacks.delete(first_post) @david.posts_with_callbacks.delete(first_post)
assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
@david.posts_with_callbacks.delete(second_post) @david.posts_with_callbacks.delete(second_post)
assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}", assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
"after_removing#{second_post.id}"], @david.post_log "after_removing#{second_post.id}"], @david.post_log
end end
...@@ -46,20 +46,20 @@ def test_removing_with_proc_callbacks ...@@ -46,20 +46,20 @@ def test_removing_with_proc_callbacks
@david.posts_with_proc_callbacks.delete(first_post) @david.posts_with_proc_callbacks.delete(first_post)
assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
@david.posts_with_proc_callbacks.delete(second_post) @david.posts_with_proc_callbacks.delete(second_post)
assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}", assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
"after_removing#{second_post.id}"], @david.post_log "after_removing#{second_post.id}"], @david.post_log
end end
def test_multiple_callbacks def test_multiple_callbacks
@david.posts_with_multiple_callbacks << @thinking @david.posts_with_multiple_callbacks << @thinking
assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}", assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
"after_adding_proc#{@thinking.id}"], @david.post_log "after_adding_proc#{@thinking.id}"], @david.post_log
@david.posts_with_multiple_callbacks << @thinking @david.posts_with_multiple_callbacks << @thinking
assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}", assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
"after_adding_proc#{@thinking.id}", "before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding_proc#{@thinking.id}", "before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}",
"after_adding#{@thinking.id}", "after_adding_proc#{@thinking.id}"], @david.post_log "after_adding#{@thinking.id}", "after_adding_proc#{@thinking.id}"], @david.post_log
end end
def test_has_and_belongs_to_many_add_callback def test_has_and_belongs_to_many_add_callback
david = developers(:david) david = developers(:david)
ar = projects(:active_record) ar = projects(:active_record)
...@@ -67,10 +67,10 @@ def test_has_and_belongs_to_many_add_callback ...@@ -67,10 +67,10 @@ def test_has_and_belongs_to_many_add_callback
ar.developers_with_callbacks << david ar.developers_with_callbacks << david
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], ar.developers_log assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], ar.developers_log
ar.developers_with_callbacks << david ar.developers_with_callbacks << david
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}", assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
"after_adding#{david.id}"], ar.developers_log "after_adding#{david.id}"], ar.developers_log
end end
def test_has_and_belongs_to_many_remove_callback def test_has_and_belongs_to_many_remove_callback
david = developers(:david) david = developers(:david)
jamis = developers(:jamis) jamis = developers(:jamis)
...@@ -78,9 +78,9 @@ def test_has_and_belongs_to_many_remove_callback ...@@ -78,9 +78,9 @@ def test_has_and_belongs_to_many_remove_callback
assert activerecord.developers_log.empty? assert activerecord.developers_log.empty?
activerecord.developers_with_callbacks.delete(david) activerecord.developers_with_callbacks.delete(david)
assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], activerecord.developers_log assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], activerecord.developers_log
activerecord.developers_with_callbacks.delete(jamis) activerecord.developers_with_callbacks.delete(jamis)
assert_equal ["before_removing#{david.id}", "after_removing#{david.id}", "before_removing#{jamis.id}", assert_equal ["before_removing#{david.id}", "after_removing#{david.id}", "before_removing#{jamis.id}",
"after_removing#{jamis.id}"], activerecord.developers_log "after_removing#{jamis.id}"], activerecord.developers_log
end end
...@@ -97,7 +97,7 @@ def test_has_and_belongs_to_many_remove_callback_on_clear ...@@ -97,7 +97,7 @@ def test_has_and_belongs_to_many_remove_callback_on_clear
assert activerecord.developers_with_callbacks.clear assert activerecord.developers_with_callbacks.clear
assert_equal log_array, activerecord.developers_log.sort assert_equal log_array, activerecord.developers_log.sort
end end
def test_dont_add_if_before_callback_raises_exception def test_dont_add_if_before_callback_raises_exception
assert !@david.unchangable_posts.include?(@authorless) assert !@david.unchangable_posts.include?(@authorless)
begin begin
...@@ -109,18 +109,5 @@ def test_dont_add_if_before_callback_raises_exception ...@@ -109,18 +109,5 @@ def test_dont_add_if_before_callback_raises_exception
@david.reload @david.reload
assert !@david.unchangable_posts.include?(@authorless) assert !@david.unchangable_posts.include?(@authorless)
end end
def test_push_with_attributes
assert_deprecated 'push_with_attributes' do
david = developers(:david)
activerecord = projects(:active_record)
assert activerecord.developers_log.empty?
activerecord.developers_with_callbacks.push_with_attributes(david, {})
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], activerecord.developers_log
activerecord.developers_with_callbacks.push_with_attributes(david, {})
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
"after_adding#{david.id}"], activerecord.developers_log
end
end
end end
...@@ -70,7 +70,7 @@ def test_storing_in_pstore ...@@ -70,7 +70,7 @@ def test_storing_in_pstore
class AssociationProxyTest < Test::Unit::TestCase class AssociationProxyTest < Test::Unit::TestCase
fixtures :authors, :posts, :categorizations, :categories fixtures :authors, :posts, :categorizations, :categories
def test_proxy_accessors def test_proxy_accessors
welcome = posts(:welcome) welcome = posts(:welcome)
assert_equal welcome, welcome.author.proxy_owner assert_equal welcome, welcome.author.proxy_owner
...@@ -83,7 +83,7 @@ def test_proxy_accessors ...@@ -83,7 +83,7 @@ def test_proxy_accessors
assert_equal david.class.reflect_on_association(:posts), david.posts.proxy_reflection assert_equal david.class.reflect_on_association(:posts), david.posts.proxy_reflection
david.posts.first # force load target david.posts.first # force load target
assert_equal david.posts, david.posts.proxy_target assert_equal david.posts, david.posts.proxy_target
assert_equal david, david.posts_with_extension.testing_proxy_owner assert_equal david, david.posts_with_extension.testing_proxy_owner
assert_equal david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection assert_equal david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection
david.posts_with_extension.first # force load target david.posts_with_extension.first # force load target
...@@ -92,21 +92,21 @@ def test_proxy_accessors ...@@ -92,21 +92,21 @@ def test_proxy_accessors
def test_push_does_not_load_target def test_push_does_not_load_target
david = authors(:david) david = authors(:david)
david.categories << categories(:technology) david.categories << categories(:technology)
assert !david.categories.loaded? assert !david.categories.loaded?
assert david.categories.include?(categories(:technology)) assert david.categories.include?(categories(:technology))
end end
end end
class HasOneAssociationsTest < Test::Unit::TestCase class HasOneAssociationsTest < Test::Unit::TestCase
fixtures :accounts, :companies, :developers, :projects, :developers_projects fixtures :accounts, :companies, :developers, :projects, :developers_projects
def setup def setup
Account.destroyed_account_ids.clear Account.destroyed_account_ids.clear
end end
def test_has_one def test_has_one
assert_equal companies(:first_firm).account, Account.find(1) assert_equal companies(:first_firm).account, Account.find(1)
assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
...@@ -154,22 +154,22 @@ def test_natural_assignment ...@@ -154,22 +154,22 @@ def test_natural_assignment
apple.account = citibank apple.account = citibank
assert_equal apple.id, citibank.firm_id assert_equal apple.id, citibank.firm_id
end end
def test_natural_assignment_to_nil def test_natural_assignment_to_nil
old_account_id = companies(:first_firm).account.id old_account_id = companies(:first_firm).account.id
companies(:first_firm).account = nil companies(:first_firm).account = nil
companies(:first_firm).save companies(:first_firm).save
assert_nil companies(:first_firm).account assert_nil companies(:first_firm).account
# account is dependent, therefore is destroyed when reference to owner is lost # account is dependent, therefore is destroyed when reference to owner is lost
assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) } assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
end end
def test_assignment_without_replacement def test_assignment_without_replacement
apple = Firm.create("name" => "Apple") apple = Firm.create("name" => "Apple")
citibank = Account.create("credit_limit" => 10) citibank = Account.create("credit_limit" => 10)
apple.account = citibank apple.account = citibank
assert_equal apple.id, citibank.firm_id assert_equal apple.id, citibank.firm_id
hsbc = apple.build_account({ :credit_limit => 20}, false) hsbc = apple.build_account({ :credit_limit => 20}, false)
assert_equal apple.id, hsbc.firm_id assert_equal apple.id, hsbc.firm_id
hsbc.save hsbc.save
...@@ -186,7 +186,7 @@ def test_assignment_without_replacement_on_create ...@@ -186,7 +186,7 @@ def test_assignment_without_replacement_on_create
citibank = Account.create("credit_limit" => 10) citibank = Account.create("credit_limit" => 10)
apple.account = citibank apple.account = citibank
assert_equal apple.id, citibank.firm_id assert_equal apple.id, citibank.firm_id
hsbc = apple.create_account({:credit_limit => 10}, false) hsbc = apple.create_account({:credit_limit => 10}, false)
assert_equal apple.id, hsbc.firm_id assert_equal apple.id, hsbc.firm_id
hsbc.save hsbc.save
...@@ -299,7 +299,7 @@ def test_build_before_either_saved ...@@ -299,7 +299,7 @@ def test_build_before_either_saved
def test_failing_build_association def test_failing_build_association
firm = Firm.new("name" => "GlobalMegaCorp") firm = Firm.new("name" => "GlobalMegaCorp")
firm.save firm.save
firm.account = account = Account.new firm.account = account = Account.new
assert_equal account, firm.account assert_equal account, firm.account
assert !account.save assert !account.save
...@@ -390,16 +390,16 @@ def test_not_resaved_when_unchanged ...@@ -390,16 +390,16 @@ def test_not_resaved_when_unchanged
firm.account = Account.find(:first).clone firm.account = Account.find(:first).clone
assert_queries(2) { firm.save! } assert_queries(2) { firm.save! }
end end
def test_save_still_works_after_accessing_nil_has_one def test_save_still_works_after_accessing_nil_has_one
jp = Company.new :name => 'Jaded Pixel' jp = Company.new :name => 'Jaded Pixel'
jp.dummy_account.nil? jp.dummy_account.nil?
assert_nothing_raised do assert_nothing_raised do
jp.save! jp.save!
end end
end end
def test_deprecated_inferred_foreign_key def test_deprecated_inferred_foreign_key
assert_not_deprecated { Company.belongs_to :firm } assert_not_deprecated { Company.belongs_to :firm }
assert_not_deprecated { Company.belongs_to :client, :foreign_key => "firm_id" } assert_not_deprecated { Company.belongs_to :client, :foreign_key => "firm_id" }
...@@ -498,7 +498,7 @@ def test_counting_using_sql ...@@ -498,7 +498,7 @@ def test_counting_using_sql
def test_counting_non_existant_items_using_sql def test_counting_non_existant_items_using_sql
assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size
end end
def test_belongs_to_sanity def test_belongs_to_sanity
c = Client.new c = Client.new
assert_nil c.firm assert_nil c.firm
...@@ -602,13 +602,13 @@ def test_adding_using_create ...@@ -602,13 +602,13 @@ def test_adding_using_create
assert_equal 3, first_firm.plain_clients.length assert_equal 3, first_firm.plain_clients.length
assert_equal 3, first_firm.plain_clients.size assert_equal 3, first_firm.plain_clients.size
end end
def test_adding_a_mismatch_class def test_adding_a_mismatch_class
assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil } assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 } assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) } assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
end end
def test_adding_a_collection def test_adding_a_collection
force_signal37_to_load_all_clients_of_firm force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")]) companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")])
...@@ -640,7 +640,7 @@ def test_adding_before_save ...@@ -640,7 +640,7 @@ def test_adding_before_save
assert_equal 2, new_firm.clients_of_firm.size assert_equal 2, new_firm.clients_of_firm.size
assert_equal 2, new_firm.clients_of_firm(true).size assert_equal 2, new_firm.clients_of_firm(true).size
end end
def test_invalid_adding def test_invalid_adding
firm = Firm.find(1) firm = Firm.find(1)
assert !(firm.clients_of_firm << c = Client.new) assert !(firm.clients_of_firm << c = Client.new)
...@@ -686,7 +686,7 @@ def test_build_without_loading_association ...@@ -686,7 +686,7 @@ def test_build_without_loading_association
Reply.column_names Reply.column_names
assert_equal 1, first_topic.replies.length assert_equal 1, first_topic.replies.length
assert_no_queries do assert_no_queries do
first_topic.replies.build(:title => "Not saved", :content => "Superstars") first_topic.replies.build(:title => "Not saved", :content => "Superstars")
assert_equal 2, first_topic.replies.size assert_equal 2, first_topic.replies.size
...@@ -702,11 +702,11 @@ def test_create_without_loading_association ...@@ -702,11 +702,11 @@ def test_create_without_loading_association
assert_equal 1, first_firm.clients_of_firm.size assert_equal 1, first_firm.clients_of_firm.size
first_firm.clients_of_firm.reset first_firm.clients_of_firm.reset
assert_queries(1) do assert_queries(1) do
first_firm.clients_of_firm.create(:name => "Superstars") first_firm.clients_of_firm.create(:name => "Superstars")
end end
assert_equal 2, first_firm.clients_of_firm.size assert_equal 2, first_firm.clients_of_firm.size
end end
...@@ -719,7 +719,7 @@ def test_invalid_build ...@@ -719,7 +719,7 @@ def test_invalid_build
assert new_client.new_record? assert new_client.new_record?
assert_equal 1, companies(:first_firm).clients_of_firm(true).size assert_equal 1, companies(:first_firm).clients_of_firm(true).size
end end
def test_create def test_create
force_signal37_to_load_all_clients_of_firm force_signal37_to_load_all_clients_of_firm
new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client") new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
...@@ -727,12 +727,12 @@ def test_create ...@@ -727,12 +727,12 @@ def test_create
assert_equal new_client, companies(:first_firm).clients_of_firm.last assert_equal new_client, companies(:first_firm).clients_of_firm.last
assert_equal new_client, companies(:first_firm).clients_of_firm(true).last assert_equal new_client, companies(:first_firm).clients_of_firm(true).last
end end
def test_create_many def test_create_many
companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}]) companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
assert_equal 3, companies(:first_firm).clients_of_firm(true).size assert_equal 3, companies(:first_firm).clients_of_firm(true).size
end end
def test_find_or_initialize def test_find_or_initialize
the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client") the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client")
assert_equal companies(:first_firm).id, the_client.firm_id assert_equal companies(:first_firm).id, the_client.firm_id
...@@ -771,7 +771,7 @@ def test_deleting_a_collection ...@@ -771,7 +771,7 @@ def test_deleting_a_collection
assert_equal 0, companies(:first_firm).clients_of_firm.size assert_equal 0, companies(:first_firm).clients_of_firm.size
assert_equal 0, companies(:first_firm).clients_of_firm(true).size assert_equal 0, companies(:first_firm).clients_of_firm(true).size
end end
def test_delete_all def test_delete_all
force_signal37_to_load_all_clients_of_firm force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.create("name" => "Another Client") companies(:first_firm).clients_of_firm.create("name" => "Another Client")
...@@ -841,7 +841,7 @@ def test_clearing_an_exclusively_dependent_association_collection ...@@ -841,7 +841,7 @@ def test_clearing_an_exclusively_dependent_association_collection
# Should be destroyed since the association is exclusively dependent. # Should be destroyed since the association is exclusively dependent.
assert Client.find_by_id(client_id).nil? assert Client.find_by_id(client_id).nil?
end end
def test_dependent_association_respects_optional_conditions_on_delete def test_dependent_association_respects_optional_conditions_on_delete
firm = companies(:odegy) firm = companies(:odegy)
...@@ -853,7 +853,7 @@ def test_dependent_association_respects_optional_conditions_on_delete ...@@ -853,7 +853,7 @@ def test_dependent_association_respects_optional_conditions_on_delete
firm.destroy firm.destroy
# only the correctly associated client should have been deleted # only the correctly associated client should have been deleted
assert_equal 1, Client.find_all_by_client_of(firm.id).size assert_equal 1, Client.find_all_by_client_of(firm.id).size
end end
def test_dependent_association_respects_optional_sanitized_conditions_on_delete def test_dependent_association_respects_optional_sanitized_conditions_on_delete
firm = companies(:odegy) firm = companies(:odegy)
...@@ -928,7 +928,7 @@ def test_three_levels_of_dependence ...@@ -928,7 +928,7 @@ def test_three_levels_of_dependence
topic = Topic.create "title" => "neat and simple" topic = Topic.create "title" => "neat and simple"
reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it" reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it"
silly_reply = reply.replies.create "title" => "neat and simple", "content" => "ain't complaining" silly_reply = reply.replies.create "title" => "neat and simple", "content" => "ain't complaining"
assert_nothing_raised { topic.destroy } assert_nothing_raised { topic.destroy }
end end
...@@ -953,16 +953,16 @@ def test_dependence_on_account ...@@ -953,16 +953,16 @@ def test_dependence_on_account
def test_depends_and_nullify def test_depends_and_nullify
num_accounts = Account.count num_accounts = Account.count
num_companies = Company.count num_companies = Company.count
core = companies(:rails_core) core = companies(:rails_core)
assert_equal accounts(:rails_core_account), core.account assert_equal accounts(:rails_core_account), core.account
assert_equal [companies(:leetsoft), companies(:jadedpixel)], core.companies assert_equal [companies(:leetsoft), companies(:jadedpixel)], core.companies
core.destroy core.destroy
assert_nil accounts(:rails_core_account).reload.firm_id assert_nil accounts(:rails_core_account).reload.firm_id
assert_nil companies(:leetsoft).reload.client_of assert_nil companies(:leetsoft).reload.client_of
assert_nil companies(:jadedpixel).reload.client_of assert_nil companies(:jadedpixel).reload.client_of
assert_equal num_accounts, Account.count assert_equal num_accounts, Account.count
end end
...@@ -985,9 +985,9 @@ def test_replace_with_less ...@@ -985,9 +985,9 @@ def test_replace_with_less
assert firm.save, "Could not save firm" assert firm.save, "Could not save firm"
firm.reload firm.reload
assert_equal 1, firm.clients.length assert_equal 1, firm.clients.length
end end
def test_replace_with_new def test_replace_with_new
firm = Firm.find(:first) firm = Firm.find(:first)
new_client = Client.new("name" => "New Client") new_client = Client.new("name" => "New Client")
...@@ -997,7 +997,7 @@ def test_replace_with_new ...@@ -997,7 +997,7 @@ def test_replace_with_new
assert_equal 2, firm.clients.length assert_equal 2, firm.clients.length
assert !firm.clients.include?(:first_client) assert !firm.clients.include?(:first_client)
end end
def test_replace_on_new_object def test_replace_on_new_object
firm = Firm.new("name" => "New Firm") firm = Firm.new("name" => "New Firm")
firm.clients = [companies(:second_client), Client.new("name" => "New Client")] firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
...@@ -1006,11 +1006,11 @@ def test_replace_on_new_object ...@@ -1006,11 +1006,11 @@ def test_replace_on_new_object
assert_equal 2, firm.clients.length assert_equal 2, firm.clients.length
assert firm.clients.include?(Client.find_by_name("New Client")) assert firm.clients.include?(Client.find_by_name("New Client"))
end end
def test_get_ids def test_get_ids
assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids
end end
def test_assign_ids def test_assign_ids
firm = Firm.new("name" => "Apple") firm = Firm.new("name" => "Apple")
firm.client_ids = [companies(:first_client).id, companies(:second_client).id] firm.client_ids = [companies(:first_client).id, companies(:second_client).id]
...@@ -1034,11 +1034,11 @@ def test_assign_ids_ignoring_blanks ...@@ -1034,11 +1034,11 @@ def test_assign_ids_ignoring_blanks
class BelongsToAssociationsTest < Test::Unit::TestCase class BelongsToAssociationsTest < Test::Unit::TestCase
fixtures :accounts, :companies, :developers, :projects, :topics, fixtures :accounts, :companies, :developers, :projects, :topics,
:developers_projects, :computers, :authors, :posts, :tags, :taggings :developers_projects, :computers, :authors, :posts, :tags, :taggings
def test_belongs_to def test_belongs_to
Client.find(3).firm.name Client.find(3).firm.name
assert_equal companies(:first_firm).name, Client.find(3).firm.name assert_equal companies(:first_firm).name, Client.find(3).firm.name
assert !Client.find(3).firm.nil?, "Microsoft should have a firm" assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
end end
def test_proxy_assignment def test_proxy_assignment
...@@ -1091,7 +1091,7 @@ def test_building_the_belonging_object ...@@ -1091,7 +1091,7 @@ def test_building_the_belonging_object
citibank.save citibank.save
assert_equal apple.id, citibank.firm_id assert_equal apple.id, citibank.firm_id
end end
def test_natural_assignment_to_nil def test_natural_assignment_to_nil
client = Client.find(3) client = Client.find(3)
client.firm = nil client.firm = nil
...@@ -1099,7 +1099,7 @@ def test_natural_assignment_to_nil ...@@ -1099,7 +1099,7 @@ def test_natural_assignment_to_nil
assert_nil client.firm(true) assert_nil client.firm(true)
assert_nil client.client_of assert_nil client.client_of
end end
def test_with_different_class_name def test_with_different_class_name
assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm" assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
...@@ -1109,7 +1109,7 @@ def test_with_condition ...@@ -1109,7 +1109,7 @@ def test_with_condition
assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm" assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
end end
def test_belongs_to_counter def test_belongs_to_counter
debate = Topic.create("title" => "debate") debate = Topic.create("title" => "debate")
assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet" assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
...@@ -1212,7 +1212,7 @@ def test_field_name_same_as_foreign_key ...@@ -1212,7 +1212,7 @@ def test_field_name_same_as_foreign_key
def test_counter_cache def test_counter_cache
topic = Topic.create :title => "Zoom-zoom-zoom" topic = Topic.create :title => "Zoom-zoom-zoom"
assert_equal 0, topic[:replies_count] assert_equal 0, topic[:replies_count]
reply = Reply.create(:title => "re: zoom", :content => "speedy quick!") reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
reply.topic = topic reply.topic = topic
...@@ -1240,10 +1240,10 @@ def test_custom_counter_cache ...@@ -1240,10 +1240,10 @@ def test_custom_counter_cache
def test_store_two_association_with_one_save def test_store_two_association_with_one_save
num_orders = Order.count num_orders = Order.count
num_customers = Customer.count num_customers = Customer.count
order = Order.new order = Order.new
customer1 = order.billing = Customer.new customer1 = order.billing = Customer.new
customer2 = order.shipping = Customer.new customer2 = order.shipping = Customer.new
assert order.save assert order.save
assert_equal customer1, order.billing assert_equal customer1, order.billing
assert_equal customer2, order.shipping assert_equal customer2, order.shipping
...@@ -1251,28 +1251,28 @@ def test_store_two_association_with_one_save ...@@ -1251,28 +1251,28 @@ def test_store_two_association_with_one_save
order.reload order.reload
assert_equal customer1, order.billing assert_equal customer1, order.billing
assert_equal customer2, order.shipping assert_equal customer2, order.shipping
assert_equal num_orders +1, Order.count assert_equal num_orders +1, Order.count
assert_equal num_customers +2, Customer.count assert_equal num_customers +2, Customer.count
end end
def test_store_association_in_two_relations_with_one_save def test_store_association_in_two_relations_with_one_save
num_orders = Order.count num_orders = Order.count
num_customers = Customer.count num_customers = Customer.count
order = Order.new order = Order.new
customer = order.billing = order.shipping = Customer.new customer = order.billing = order.shipping = Customer.new
assert order.save assert order.save
assert_equal customer, order.billing assert_equal customer, order.billing
assert_equal customer, order.shipping assert_equal customer, order.shipping
order.reload order.reload
assert_equal customer, order.billing assert_equal customer, order.billing
assert_equal customer, order.shipping assert_equal customer, order.shipping
assert_equal num_orders +1, Order.count assert_equal num_orders +1, Order.count
assert_equal num_customers +1, Customer.count assert_equal num_customers +1, Customer.count
end end
...@@ -1281,46 +1281,46 @@ def test_store_association_in_two_relations_with_one_save_in_existing_object ...@@ -1281,46 +1281,46 @@ def test_store_association_in_two_relations_with_one_save_in_existing_object
num_orders = Order.count num_orders = Order.count
num_customers = Customer.count num_customers = Customer.count
order = Order.create order = Order.create
customer = order.billing = order.shipping = Customer.new customer = order.billing = order.shipping = Customer.new
assert order.save assert order.save
assert_equal customer, order.billing assert_equal customer, order.billing
assert_equal customer, order.shipping assert_equal customer, order.shipping
order.reload order.reload
assert_equal customer, order.billing assert_equal customer, order.billing
assert_equal customer, order.shipping assert_equal customer, order.shipping
assert_equal num_orders +1, Order.count assert_equal num_orders +1, Order.count
assert_equal num_customers +1, Customer.count assert_equal num_customers +1, Customer.count
end end
def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
num_orders = Order.count num_orders = Order.count
num_customers = Customer.count num_customers = Customer.count
order = Order.create order = Order.create
customer = order.billing = order.shipping = Customer.new customer = order.billing = order.shipping = Customer.new
assert order.save assert order.save
assert_equal customer, order.billing assert_equal customer, order.billing
assert_equal customer, order.shipping assert_equal customer, order.shipping
order.reload order.reload
customer = order.billing = order.shipping = Customer.new customer = order.billing = order.shipping = Customer.new
assert order.save assert order.save
order.reload order.reload
assert_equal customer, order.billing assert_equal customer, order.billing
assert_equal customer, order.shipping assert_equal customer, order.shipping
assert_equal num_orders +1, Order.count assert_equal num_orders +1, Order.count
assert_equal num_customers +2, Customer.count assert_equal num_customers +2, Customer.count
end end
def test_association_assignment_sticks def test_association_assignment_sticks
post = Post.find(:first) post = Post.find(:first)
...@@ -1341,7 +1341,7 @@ def test_association_assignment_sticks ...@@ -1341,7 +1341,7 @@ def test_association_assignment_sticks
# the author id of the post should be the id we set # the author id of the post should be the id we set
assert_equal post.author_id, author2.id assert_equal post.author_id, author2.id
end end
end end
...@@ -1373,7 +1373,7 @@ class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base ...@@ -1373,7 +1373,7 @@ class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects
def test_has_and_belongs_to_many def test_has_and_belongs_to_many
david = Developer.find(1) david = Developer.find(1)
...@@ -1393,11 +1393,11 @@ def test_triple_equality ...@@ -1393,11 +1393,11 @@ def test_triple_equality
def test_adding_single def test_adding_single
jamis = Developer.find(2) jamis = Developer.find(2)
jamis.projects.reload # causing the collection to load jamis.projects.reload # causing the collection to load
action_controller = Project.find(2) action_controller = Project.find(2)
assert_equal 1, jamis.projects.size assert_equal 1, jamis.projects.size
assert_equal 1, action_controller.developers.size assert_equal 1, action_controller.developers.size
jamis.projects << action_controller jamis.projects << action_controller
assert_equal 2, jamis.projects.size assert_equal 2, jamis.projects.size
...@@ -1418,8 +1418,8 @@ def test_adding_from_the_project ...@@ -1418,8 +1418,8 @@ def test_adding_from_the_project
assert_equal 1, jamis.projects.size assert_equal 1, jamis.projects.size
assert_equal 1, action_controller.developers.size assert_equal 1, action_controller.developers.size
action_controller.developers << jamis action_controller.developers << jamis
assert_equal 2, jamis.projects(true).size assert_equal 2, jamis.projects(true).size
assert_equal 2, action_controller.developers.size assert_equal 2, action_controller.developers.size
assert_equal 2, action_controller.developers(true).size assert_equal 2, action_controller.developers(true).size
...@@ -1469,18 +1469,6 @@ def test_adding_uses_default_values_on_join_table ...@@ -1469,18 +1469,6 @@ def test_adding_uses_default_values_on_join_table
assert_equal 1, project.access_level.to_i assert_equal 1, project.access_level.to_i
end end
def test_adding_uses_explicit_values_on_join_table
ac = projects(:action_controller)
assert !developers(:jamis).projects.include?(ac)
assert_deprecated do
developers(:jamis).projects.push_with_attributes(ac, :access_level => 3)
end
assert developers(:jamis, :reload).projects.include?(ac)
project = developers(:jamis).projects.detect { |p| p == ac }
assert_equal 3, project.access_level.to_i
end
def test_hatbm_attribute_access_and_respond_to def test_hatbm_attribute_access_and_respond_to
project = developers(:jamis).projects[0] project = developers(:jamis).projects[0]
assert project.has_attribute?("name") assert project.has_attribute?("name")
...@@ -1512,31 +1500,6 @@ def test_habtm_adding_before_save ...@@ -1512,31 +1500,6 @@ def test_habtm_adding_before_save
assert_equal 2, aredridel.projects(true).size assert_equal 2, aredridel.projects(true).size
end end
def test_habtm_adding_before_save_with_join_attributes
no_of_devels = Developer.count
no_of_projects = Project.count
now = Date.today
ken = Developer.new("name" => "Ken")
assert_deprecated do
ken.projects.push_with_attributes( Project.find(1), :joined_on => now )
end
p = Project.new("name" => "Foomatic")
assert_deprecated do
ken.projects.push_with_attributes( p, :joined_on => now )
end
assert ken.new_record?
assert p.new_record?
assert ken.save
assert !ken.new_record?
assert_equal no_of_devels+1, Developer.count
assert_equal no_of_projects+1, Project.count
assert_equal 2, ken.projects.size
assert_equal 2, ken.projects(true).size
kenReloaded = Developer.find_by_name 'Ken'
kenReloaded.projects.each {|prj| assert_date_from_db(now, prj.joined_on.to_date)}
end
def test_habtm_saving_multiple_relationships def test_habtm_saving_multiple_relationships
new_project = Project.new("name" => "Grimetime") new_project = Project.new("name" => "Grimetime")
amount_of_developers = 4 amount_of_developers = 4
...@@ -1566,7 +1529,7 @@ def test_build ...@@ -1566,7 +1529,7 @@ def test_build
assert_equal devel.projects.last, proj assert_equal devel.projects.last, proj
assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
end end
def test_build_by_new_record def test_build_by_new_record
devel = Developer.new(:name => "Marcel", :salary => 75000) devel = Developer.new(:name => "Marcel", :salary => 75000)
proj1 = devel.projects.build(:name => "Make bed") proj1 = devel.projects.build(:name => "Make bed")
...@@ -1579,7 +1542,7 @@ def test_build_by_new_record ...@@ -1579,7 +1542,7 @@ def test_build_by_new_record
assert_equal devel.projects.last, proj2 assert_equal devel.projects.last, proj2
assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated
end end
def test_create def test_create
devel = Developer.find(1) devel = Developer.find(1)
proj = devel.projects.create("name" => "Projekt") proj = devel.projects.create("name" => "Projekt")
...@@ -1587,7 +1550,7 @@ def test_create ...@@ -1587,7 +1550,7 @@ def test_create
assert !proj.new_record? assert !proj.new_record?
assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
end end
def test_create_by_new_record def test_create_by_new_record
devel = Developer.new(:name => "Marcel", :salary => 75000) devel = Developer.new(:name => "Marcel", :salary => 75000)
proj1 = devel.projects.create(:name => "Make bed") proj1 = devel.projects.create(:name => "Make bed")
...@@ -1600,7 +1563,7 @@ def test_create_by_new_record ...@@ -1600,7 +1563,7 @@ def test_create_by_new_record
assert_equal devel.projects.last, proj2 assert_equal devel.projects.last, proj2
assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated
end end
def test_uniq_after_the_fact def test_uniq_after_the_fact
developers(:jamis).projects << projects(:active_record) developers(:jamis).projects << projects(:active_record)
developers(:jamis).projects << projects(:active_record) developers(:jamis).projects << projects(:active_record)
...@@ -1613,7 +1576,7 @@ def test_uniq_before_the_fact ...@@ -1613,7 +1576,7 @@ def test_uniq_before_the_fact
projects(:active_record).developers << developers(:david) projects(:active_record).developers << developers(:david)
assert_equal 3, projects(:active_record, :reload).developers.size assert_equal 3, projects(:active_record, :reload).developers.size
end end
def test_deleting def test_deleting
david = Developer.find(1) david = Developer.find(1)
active_record = Project.find(1) active_record = Project.find(1)
...@@ -1622,7 +1585,7 @@ def test_deleting ...@@ -1622,7 +1585,7 @@ def test_deleting
assert_equal 3, active_record.developers.size assert_equal 3, active_record.developers.size
david.projects.delete(active_record) david.projects.delete(active_record)
assert_equal 1, david.projects.size assert_equal 1, david.projects.size
assert_equal 1, david.projects(true).size assert_equal 1, david.projects(true).size
assert_equal 2, active_record.developers(true).size assert_equal 2, active_record.developers(true).size
...@@ -1641,7 +1604,7 @@ def test_deleting_with_sql ...@@ -1641,7 +1604,7 @@ def test_deleting_with_sql
active_record = Project.find(1) active_record = Project.find(1)
active_record.developers.reload active_record.developers.reload
assert_equal 3, active_record.developers_by_sql.size assert_equal 3, active_record.developers_by_sql.size
active_record.developers_by_sql.delete(david) active_record.developers_by_sql.delete(david)
assert_equal 2, active_record.developers_by_sql(true).size assert_equal 2, active_record.developers_by_sql(true).size
end end
...@@ -1650,7 +1613,7 @@ def test_deleting_array_with_sql ...@@ -1650,7 +1613,7 @@ def test_deleting_array_with_sql
active_record = Project.find(1) active_record = Project.find(1)
active_record.developers.reload active_record.developers.reload
assert_equal 3, active_record.developers_by_sql.size assert_equal 3, active_record.developers_by_sql.size
active_record.developers_by_sql.delete(Developer.find(:all)) active_record.developers_by_sql.delete(Developer.find(:all))
assert_equal 0, active_record.developers_by_sql(true).size assert_equal 0, active_record.developers_by_sql(true).size
end end
...@@ -1674,7 +1637,7 @@ def test_removing_associations_on_destroy ...@@ -1674,7 +1637,7 @@ def test_removing_associations_on_destroy
def test_additional_columns_from_join_table def test_additional_columns_from_join_table
assert_date_from_db Date.new(2004, 10, 10), Developer.find(1).projects.first.joined_on.to_date assert_date_from_db Date.new(2004, 10, 10), Developer.find(1).projects.first.joined_on.to_date
end end
def test_destroy_all def test_destroy_all
david = Developer.find(1) david = Developer.find(1)
david.projects.reload david.projects.reload
...@@ -1686,12 +1649,9 @@ def test_destroy_all ...@@ -1686,12 +1649,9 @@ def test_destroy_all
def test_rich_association def test_rich_association
jamis = developers(:jamis) jamis = developers(:jamis)
assert_deprecated 'push_with_attributes' do assert_raise(NoMethodError) do
jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today) jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today)
end end
assert_date_from_db Date.today, jamis.projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on
assert_date_from_db Date.today, developers(:jamis).projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on
end end
def test_associations_with_conditions def test_associations_with_conditions
...@@ -1706,11 +1666,11 @@ def test_associations_with_conditions ...@@ -1706,11 +1666,11 @@ def test_associations_with_conditions
projects(:active_record).developers_named_david.clear projects(:active_record).developers_named_david.clear
assert_equal 2, projects(:active_record, :reload).developers.size assert_equal 2, projects(:active_record, :reload).developers.size
end end
def test_find_in_association def test_find_in_association
# Using sql # Using sql
assert_equal developers(:david), projects(:active_record).developers.find(developers(:david).id), "SQL find" assert_equal developers(:david), projects(:active_record).developers.find(developers(:david).id), "SQL find"
# Using ruby # Using ruby
active_record = projects(:active_record) active_record = projects(:active_record)
active_record.developers.reload active_record.developers.reload
...@@ -1719,7 +1679,7 @@ def test_find_in_association ...@@ -1719,7 +1679,7 @@ def test_find_in_association
def test_find_in_association_with_custom_finder_sql 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" assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id), "SQL find"
active_record = projects(:active_record) active_record = projects(:active_record)
active_record.developers_with_finder_sql.reload active_record.developers_with_finder_sql.reload
assert_equal developers(:david), active_record.developers_with_finder_sql.find(developers(:david).id), "Ruby find" assert_equal developers(:david), active_record.developers_with_finder_sql.find(developers(:david).id), "Ruby find"
...@@ -1750,11 +1710,11 @@ def test_new_with_values_in_collection ...@@ -1750,11 +1710,11 @@ def test_new_with_values_in_collection
def test_find_in_association_with_options def test_find_in_association_with_options
developers = projects(:active_record).developers.find(:all) developers = projects(:active_record).developers.find(:all)
assert_equal 3, developers.size assert_equal 3, developers.size
assert_equal developers(:poor_jamis), projects(:active_record).developers.find(:first, :conditions => "salary < 10000") assert_equal developers(:poor_jamis), projects(:active_record).developers.find(:first, :conditions => "salary < 10000")
assert_equal developers(:jamis), projects(:active_record).developers.find(:first, :order => "salary DESC") assert_equal developers(:jamis), projects(:active_record).developers.find(:first, :order => "salary DESC")
end end
def test_replace_with_less def test_replace_with_less
david = developers(:david) david = developers(:david)
david.projects = [projects(:action_controller)] david.projects = [projects(:action_controller)]
...@@ -1769,7 +1729,7 @@ def test_replace_with_new ...@@ -1769,7 +1729,7 @@ def test_replace_with_new
assert_equal 2, david.projects.length assert_equal 2, david.projects.length
assert !david.projects.include?(projects(:active_record)) assert !david.projects.include?(projects(:active_record))
end end
def test_replace_on_new_object def test_replace_on_new_object
new_developer = Developer.new("name" => "Matz") new_developer = Developer.new("name" => "Matz")
new_developer.projects = [projects(:action_controller), Project.new("name" => "ActionWebSearch")] new_developer.projects = [projects(:action_controller), Project.new("name" => "ActionWebSearch")]
...@@ -1780,16 +1740,16 @@ def test_replace_on_new_object ...@@ -1780,16 +1740,16 @@ def test_replace_on_new_object
def test_consider_type def test_consider_type
developer = Developer.find(:first) developer = Developer.find(:first)
special_project = SpecialProject.create("name" => "Special Project") special_project = SpecialProject.create("name" => "Special Project")
other_project = developer.projects.first other_project = developer.projects.first
developer.special_projects << special_project developer.special_projects << special_project
developer.reload developer.reload
assert developer.projects.include?(special_project) assert developer.projects.include?(special_project)
assert developer.special_projects.include?(special_project) assert developer.special_projects.include?(special_project)
assert !developer.special_projects.include?(other_project) assert !developer.special_projects.include?(other_project)
end end
def test_update_attributes_after_push_without_duplicate_join_table_rows def test_update_attributes_after_push_without_duplicate_join_table_rows
developer = Developer.new("name" => "Kano") developer = Developer.new("name" => "Kano")
project = SpecialProject.create("name" => "Special Project") project = SpecialProject.create("name" => "Special Project")
...@@ -1802,20 +1762,20 @@ def test_update_attributes_after_push_without_duplicate_join_table_rows ...@@ -1802,20 +1762,20 @@ def test_update_attributes_after_push_without_duplicate_join_table_rows
AND developer_id = #{developer.id} AND developer_id = #{developer.id}
end_sql end_sql
end end
def test_updating_attributes_on_non_rich_associations def test_updating_attributes_on_non_rich_associations
welcome = categories(:technology).posts.first welcome = categories(:technology).posts.first
welcome.title = "Something else" welcome.title = "Something else"
assert welcome.save! assert welcome.save!
end end
def test_updating_attributes_on_rich_associations def test_updating_attributes_on_rich_associations
david = projects(:action_controller).developers.first david = projects(:action_controller).developers.first
david.name = "DHH" david.name = "DHH"
assert_raises(ActiveRecord::ReadOnlyRecord) { david.save! } assert_raises(ActiveRecord::ReadOnlyRecord) { david.save! }
end end
def test_updating_attributes_on_rich_associations_with_limited_find def test_updating_attributes_on_rich_associations_with_limited_find
david = projects(:action_controller).developers.find(:all, :select => "developers.*").first david = projects(:action_controller).developers.find(:all, :select => "developers.*").first
david.name = "DHH" david.name = "DHH"
...@@ -1825,7 +1785,7 @@ def test_updating_attributes_on_rich_associations_with_limited_find ...@@ -1825,7 +1785,7 @@ def test_updating_attributes_on_rich_associations_with_limited_find
def test_join_table_alias def test_join_table_alias
assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL').size assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL').size
end end
def test_join_with_group def test_join_with_group
group = Developer.columns.inject([]) do |g, c| group = Developer.columns.inject([]) do |g, c|
g << "developers.#{c.name}" g << "developers.#{c.name}"
...@@ -1847,8 +1807,8 @@ def test_assign_ids ...@@ -1847,8 +1807,8 @@ def test_assign_ids
developer.save developer.save
developer.reload developer.reload
assert_equal 2, developer.projects.length assert_equal 2, developer.projects.length
assert_equal projects(:active_record), developer.projects[0] assert_equal projects(:active_record), developer.projects[0]
assert_equal projects(:action_controller), developer.projects[1] assert_equal projects(:action_controller), developer.projects[1]
end end
def test_assign_ids_ignoring_blanks def test_assign_ids_ignoring_blanks
...@@ -1857,8 +1817,8 @@ def test_assign_ids_ignoring_blanks ...@@ -1857,8 +1817,8 @@ def test_assign_ids_ignoring_blanks
developer.save developer.save
developer.reload developer.reload
assert_equal 2, developer.projects.length assert_equal 2, developer.projects.length
assert_equal projects(:active_record), developer.projects[0] assert_equal projects(:active_record), developer.projects[0]
assert_equal projects(:action_controller), developer.projects[1] assert_equal projects(:action_controller), developer.projects[1]
end end
def test_select_limited_ids_list def test_select_limited_ids_list
...@@ -1874,10 +1834,10 @@ def test_select_limited_ids_list ...@@ -1874,10 +1834,10 @@ def test_select_limited_ids_list
projects = Project.send(:select_limited_ids_list, {:order => 'developers.created_at'}, join_dep) projects = Project.send(:select_limited_ids_list, {:order => 'developers.created_at'}, join_dep)
assert_equal %w(1 2), projects.scan(/\d/).sort assert_equal %w(1 2), projects.scan(/\d/).sort
end end
def test_scoped_find_on_through_association_doesnt_return_read_only_records def test_scoped_find_on_through_association_doesnt_return_read_only_records
tag = Post.find(1).tags.find_by_name("General") tag = Post.find(1).tags.find_by_name("General")
assert_nothing_raised do assert_nothing_raised do
tag.save! tag.save!
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册