Skip to content

Commit

Permalink
remove calls to find(:first), find(:last) and find(:all)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Apr 26, 2012
1 parent b309963 commit 75f03ec
Show file tree
Hide file tree
Showing 27 changed files with 114 additions and 119 deletions.
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/mysql/schema_test.rb
Expand Up @@ -20,7 +20,7 @@ def self.name; 'Post'; end
end

def test_schema
assert @omgpost.find(:first)
assert @omgpost.first
end

def test_primary_key
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/mysql2/schema_test.rb
Expand Up @@ -20,7 +20,7 @@ def self.name; 'Post'; end
end

def test_schema
assert @omgpost.find(:first)
assert @omgpost.first
end

def test_primary_key
Expand Down
Expand Up @@ -394,7 +394,7 @@ def test_custom_counter_cache
end

def test_association_assignment_sticks
post = Post.find(:first)
post = Post.first

author1, author2 = Author.find(:all, :limit => 2)
assert_not_nil author1
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -585,7 +585,7 @@ def test_count_eager_with_has_many_and_limit_and_high_offset
end

def test_eager_with_has_many_and_limit_with_no_results
posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => "posts.title = 'magic forest'").find(:all)
posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => "posts.title = 'magic forest'").all
assert_equal 0, posts.size
end

Expand Down Expand Up @@ -1036,7 +1036,7 @@ def test_preload_has_many_uses_exclusive_scope
end

def test_preload_has_many_using_primary_key
expected = Firm.find(:first).clients_using_primary_key.to_a
expected = Firm.first.clients_using_primary_key.to_a
firm = Firm.find :first, :include => :clients_using_primary_key
assert_no_queries do
assert_equal expected, firm.clients_using_primary_key
Expand Down
Expand Up @@ -355,7 +355,7 @@ def test_deleting
def test_deleting_array
david = Developer.find(1)
david.projects.reload
david.projects.delete(Project.find(:all))
david.projects.delete(Project.all)
assert_equal 0, david.projects.size
assert_equal 0, david.projects(true).size
end
Expand All @@ -375,7 +375,7 @@ def test_deleting_array_with_sql
active_record.developers.reload
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.all)
assert_equal 0, active_record.developers_by_sql(true).size
end

Expand Down Expand Up @@ -548,7 +548,7 @@ def test_find_in_association_with_custom_finder_sql_and_string_id

def test_find_with_merged_options
assert_equal 1, projects(:active_record).limited_developers.size
assert_equal 1, projects(:active_record).limited_developers.find(:all).size
assert_equal 1, projects(:active_record).limited_developers.all.size
assert_equal 3, projects(:active_record).limited_developers.find(:all, :limit => nil).size
end

Expand Down Expand Up @@ -632,7 +632,7 @@ def test_replace_on_new_object
end

def test_consider_type
developer = Developer.find(:first)
developer = Developer.first
special_project = SpecialProject.create("name" => "Special Project")

other_project = developer.projects.first
Expand Down Expand Up @@ -667,7 +667,7 @@ def test_habtm_respects_select
categories(:technology).select_testing_posts(true).each do |o|
assert_respond_to o, :correctness_marker
end
assert_respond_to categories(:technology).select_testing_posts.find(:first), :correctness_marker
assert_respond_to categories(:technology).select_testing_posts.first, :correctness_marker
end

def test_habtm_selects_all_columns_by_default
Expand Down Expand Up @@ -780,8 +780,8 @@ def test_symbols_as_keys

assert_equal 1, project.developers.size
assert_equal 1, developer.projects.size
assert_equal developer, project.developers.find(:first)
assert_equal project, developer.projects.find(:first)
assert_equal developer, project.developers.first
assert_equal project, developer.projects.first
end

def test_self_referential_habtm_without_foreign_key_set_should_raise_exception
Expand Down Expand Up @@ -820,7 +820,7 @@ def test_count_with_finder_sql

def test_association_proxy_transaction_method_starts_transaction_in_association_class
Post.expects(:transaction)
Category.find(:first).posts.transaction do
Category.first.posts.transaction do
# nothing
end
end
Expand Down
17 changes: 6 additions & 11 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Expand Up @@ -279,7 +279,7 @@ def test_find_with_blank_conditions

def test_find_many_with_merged_options
assert_equal 1, companies(:first_firm).limited_clients.size
assert_equal 1, companies(:first_firm).limited_clients.find(:all).size
assert_equal 1, companies(:first_firm).limited_clients.all.size
assert_equal 2, companies(:first_firm).limited_clients.find(:all, :limit => nil).size
end

Expand Down Expand Up @@ -312,11 +312,6 @@ def test_dynamic_find_all_limit_should_override_association_limit
assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length
end

def test_dynamic_find_all_should_respect_readonly_access
companies(:first_firm).readonly_clients.find(:all).each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
companies(:first_firm).readonly_clients.find(:all).each { |c| assert c.readonly? }
end

def test_dynamic_find_or_create_from_two_attributes_using_an_association
author = authors(:david)
number_of_posts = Post.count
Expand Down Expand Up @@ -1000,14 +995,14 @@ def test_dependent_association_respects_optional_hash_conditions_on_delete
end

def test_delete_all_association_with_primary_key_deletes_correct_records
firm = Firm.find(:first)
firm = Firm.first
# break the vanilla firm_id foreign key
assert_equal 2, firm.clients.count
firm.clients.first.update_column(:firm_id, nil)
assert_equal 1, firm.clients(true).count
assert_equal 1, firm.clients_using_primary_key_with_delete_all.count
old_record = firm.clients_using_primary_key_with_delete_all.first
firm = Firm.find(:first)
firm = Firm.first
firm.destroy
assert_nil Client.find_by_id(old_record.id)
end
Expand Down Expand Up @@ -1216,12 +1211,12 @@ def test_included_in_collection
end

def test_adding_array_and_collection
assert_nothing_raised { Firm.find(:first).clients + Firm.find(:all).last.clients }
assert_nothing_raised { Firm.first.clients + Firm.all.last.clients }
end

def test_find_all_without_conditions
firm = companies(:first_firm)
assert_equal 2, firm.clients.find(:all).length
assert_equal 2, firm.clients.all.length
end

def test_replace_with_less
Expand Down Expand Up @@ -1561,7 +1556,7 @@ def test_joins_with_namespaced_model_should_use_correct_type

def test_association_proxy_transaction_method_starts_transaction_in_association_class
Comment.expects(:transaction)
Post.find(:first).comments.transaction do
Post.first.comments.transaction do
# nothing
end
end
Expand Down
Expand Up @@ -568,7 +568,7 @@ def test_get_ids_for_unloaded_associations_does_not_load_them

def test_association_proxy_transaction_method_starts_transaction_in_association_class
Tag.expects(:transaction)
Post.find(:first).tags.transaction do
Post.first.tags.transaction do
# nothing
end
end
Expand Down Expand Up @@ -651,7 +651,7 @@ def test_collection_singular_ids_getter_with_string_primary_keys

def test_collection_singular_ids_setter
company = companies(:rails_core)
dev = Developer.find(:first)
dev = Developer.first

company.developer_ids = [dev.id]
assert_equal [dev], company.developers
Expand All @@ -671,7 +671,7 @@ def test_collection_singular_ids_setter_with_string_primary_keys

def test_collection_singular_ids_setter_raises_exception_when_invalid_ids_set
company = companies(:rails_core)
ids = [Developer.find(:first).id, -9999]
ids = [Developer.first.id, -9999]
assert_raises(ActiveRecord::RecordNotFound) {company.developer_ids= ids}
end

Expand Down
Expand Up @@ -294,13 +294,13 @@ def test_dependence_with_missing_association

def test_dependence_with_missing_association_and_nullify
Account.destroy_all
firm = DependentFirm.find(:first)
firm = DependentFirm.first
assert_nil firm.account
firm.destroy
end

def test_finding_with_interpolated_condition
firm = Firm.find(:first)
firm = Firm.first
superior = firm.clients.create(:name => 'SuperiorCo')
superior.rating = 10
superior.save
Expand Down
36 changes: 18 additions & 18 deletions activerecord/test/cases/associations/inverse_associations_test.rb
Expand Up @@ -114,7 +114,7 @@ def test_parent_instance_should_be_shared_with_eager_loaded_child_on_find
end

def test_parent_instance_should_be_shared_with_newly_built_child
m = Man.find(:first)
m = Man.first
f = m.build_face(:description => 'haunted')
assert_not_nil f.man
assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
Expand All @@ -125,7 +125,7 @@ def test_parent_instance_should_be_shared_with_newly_built_child
end

def test_parent_instance_should_be_shared_with_newly_created_child
m = Man.find(:first)
m = Man.first
f = m.create_face(:description => 'haunted')
assert_not_nil f.man
assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
Expand All @@ -136,7 +136,7 @@ def test_parent_instance_should_be_shared_with_newly_created_child
end

def test_parent_instance_should_be_shared_with_newly_created_child_via_bang_method
m = Man.find(:first)
m = Man.first
f = m.create_face!(:description => 'haunted')
assert_not_nil f.man
assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
Expand All @@ -147,7 +147,7 @@ def test_parent_instance_should_be_shared_with_newly_created_child_via_bang_meth
end

def test_parent_instance_should_be_shared_with_replaced_via_accessor_child
m = Man.find(:first)
m = Man.first
f = Face.new(:description => 'haunted')
m.face = f
assert_not_nil f.man
Expand All @@ -159,7 +159,7 @@ def test_parent_instance_should_be_shared_with_replaced_via_accessor_child
end

def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.find(:first).dirty_face }
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.dirty_face }
end
end

Expand Down Expand Up @@ -201,7 +201,7 @@ def test_parent_instance_should_be_shared_with_eager_loaded_children
end

def test_parent_instance_should_be_shared_with_newly_block_style_built_child
m = Man.find(:first)
m = Man.first
i = m.interests.build {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
assert_not_nil i.topic, "Child attributes supplied to build via blocks should be populated"
assert_not_nil i.man
Expand All @@ -213,7 +213,7 @@ def test_parent_instance_should_be_shared_with_newly_block_style_built_child
end

def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child
m = Man.find(:first)
m = Man.first
i = m.interests.create!(:topic => 'Industrial Revolution Re-enactment')
assert_not_nil i.man
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
Expand All @@ -224,7 +224,7 @@ def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_chi
end

def test_parent_instance_should_be_shared_with_newly_block_style_created_child
m = Man.find(:first)
m = Man.first
i = m.interests.create {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
assert_not_nil i.topic, "Child attributes supplied to create via blocks should be populated"
assert_not_nil i.man
Expand All @@ -248,7 +248,7 @@ def test_parent_instance_should_be_shared_with_poked_in_child
end

def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
m = Man.find(:first)
m = Man.first
i = Interest.new(:topic => 'Industrial Revolution Re-enactment')
m.interests = [i]
assert_not_nil i.man
Expand All @@ -260,7 +260,7 @@ def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
end

def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.find(:first).secret_interests }
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.secret_interests }
end
end

Expand Down Expand Up @@ -331,7 +331,7 @@ def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
end

def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
f = Face.find(:first)
f = Face.first
m = Man.new(:name => 'Charles')
f.man = m
assert_not_nil m.face
Expand All @@ -343,7 +343,7 @@ def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
end

def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_man }
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_man }
end
end

Expand Down Expand Up @@ -421,19 +421,19 @@ def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many

def test_trying_to_access_inverses_that_dont_exist_shouldnt_raise_an_error
# Ideally this would, if only for symmetry's sake with other association types
assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_polymorphic_man }
assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_polymorphic_man }
end

def test_trying_to_set_polymorphic_inverses_that_dont_exist_at_all_should_raise_an_error
# fails because no class has the correct inverse_of for horrible_polymorphic_man
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_polymorphic_man = Man.first }
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_polymorphic_man = Man.first }
end

def test_trying_to_set_polymorphic_inverses_that_dont_exist_on_the_instance_being_set_should_raise_an_error
# passes because Man does have the correct inverse_of
assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).polymorphic_man = Man.first }
assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.polymorphic_man = Man.first }
# fails because Interest does have the correct inverse_of
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).polymorphic_man = Interest.first }
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.polymorphic_man = Interest.first }
end
end

Expand All @@ -444,15 +444,15 @@ class InverseMultipleHasManyInversesForSameModel < ActiveRecord::TestCase

def test_that_we_can_load_associations_that_have_the_same_reciprocal_name_from_different_models
assert_nothing_raised(ActiveRecord::AssociationTypeMismatch) do
i = Interest.find(:first)
i = Interest.first
i.zine
i.man
end
end

def test_that_we_can_create_associations_that_have_the_same_reciprocal_name_from_different_models
assert_nothing_raised(ActiveRecord::AssociationTypeMismatch) do
i = Interest.find(:first)
i = Interest.first
i.build_zine(:title => 'Get Some in Winter! 2008')
i.build_man(:name => 'Gordon')
i.save!
Expand Down

0 comments on commit 75f03ec

Please sign in to comment.