diff options
author | Guillermo Iguaran <guilleiguaran@gmail.com> | 2013-01-18 05:06:42 -0800 |
---|---|---|
committer | Guillermo Iguaran <guilleiguaran@gmail.com> | 2013-01-18 05:06:42 -0800 |
commit | 637a7d9d357a0f3f725b0548282ca8c5e7d4af4a (patch) | |
tree | d0b3307df008377ba4b0449866d45c1a66556991 | |
parent | 5937bd02dee112646469848d7fe8a8bfcef5b4c1 (diff) | |
parent | 7baecc48028e2d0cb57936ad10b9102ed129c9f9 (diff) | |
download | rails-637a7d9d357a0f3f725b0548282ca8c5e7d4af4a.tar.gz rails-637a7d9d357a0f3f725b0548282ca8c5e7d4af4a.tar.bz2 rails-637a7d9d357a0f3f725b0548282ca8c5e7d4af4a.zip |
Merge pull request #8989 from robertomiranda/use-rails-4-find-by
Replace deprecated find_by_* with find_by
23 files changed, 70 insertions, 70 deletions
diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb index 812a35735f..93bb1d7189 100644 --- a/actionpack/lib/abstract_controller/helpers.rb +++ b/actionpack/lib/abstract_controller/helpers.rb @@ -29,7 +29,7 @@ module AbstractController # helper_method :current_user, :logged_in? # # def current_user - # @current_user ||= User.find_by_id(session[:user]) + # @current_user ||= User.find_by(id: session[:user]) # end # # def logged_in? diff --git a/actionpack/lib/action_controller/metal/http_authentication.rb b/actionpack/lib/action_controller/metal/http_authentication.rb index e295002b16..308b386d9a 100644 --- a/actionpack/lib/action_controller/metal/http_authentication.rb +++ b/actionpack/lib/action_controller/metal/http_authentication.rb @@ -29,7 +29,7 @@ module ActionController # # protected # def set_account - # @account = Account.find_by_url_name(request.subdomains.first) + # @account = Account.find_by(url_name: request.subdomains.first) # end # # def authenticate @@ -344,7 +344,7 @@ module ActionController # # protected # def set_account - # @account = Account.find_by_url_name(request.subdomains.first) + # @account = Account.find_by(url_name: request.subdomains.first) # end # # def authenticate diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index d8206b573d..9a41b27108 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -275,7 +275,7 @@ module ActionController # assert_response :found # # # Assert that the controller really put the book in the database. - # assert_not_nil Book.find_by_title("Love Hina") + # assert_not_nil Book.find_by(title: "Love Hina") # end # end # diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 6e67cd2285..789bdd74bf 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -46,7 +46,7 @@ module ActiveModel # # A newly instantiated object is unchanged: # - # person = Person.find_by_name('Uncle Bob') + # person = Person.find_by(name: 'Uncle Bob') # person.changed? # => false # # Change the name: diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index 6644b60609..762716a343 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -37,8 +37,8 @@ module ActiveModel # user.save # => true # user.authenticate('notright') # => false # user.authenticate('mUc3m00RsqyRe') # => user - # User.find_by_name('david').try(:authenticate, 'notright') # => false - # User.find_by_name('david').try(:authenticate, 'mUc3m00RsqyRe') # => user + # User.find_by(name: 'david').try(:authenticate, 'notright') # => false + # User.find_by(name: 'david').try(:authenticate, 'mUc3m00RsqyRe') # => user def has_secure_password(options = {}) # Load bcrypt-ruby only when has_secure_password is used. # This is to avoid ActiveModel (and by extension the entire framework) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 16a46a59d1..232c0323e6 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -987,7 +987,7 @@ module ActiveRecord # associated objects themselves. So with +has_and_belongs_to_many+ and +has_many+ # <tt>:through</tt>, the join records will be deleted, but the associated records won't. # - # This makes sense if you think about it: if you were to call <tt>post.tags.delete(Tag.find_by_name('food'))</tt> + # This makes sense if you think about it: if you were to call <tt>post.tags.delete(Tag.find_by(name: 'food'))</tt> # you would want the 'food' tag to be unlinked from the post, rather than for the tag itself # to be removed from the database. # diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 704998301c..a1e229adb9 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -62,14 +62,14 @@ module ActiveRecord # Note that the model is _not_ yet removed from the database: # # id = post.author.id - # Author.find_by_id(id).nil? # => false + # Author.find_by(id: id).nil? # => false # # post.save # post.reload.author # => nil # # Now it _is_ removed from the database: # - # Author.find_by_id(id).nil? # => true + # Author.find_by(id: id).nil? # => true # # === One-to-many Example # @@ -113,14 +113,14 @@ module ActiveRecord # Note that the model is _not_ yet removed from the database: # # id = post.comments.last.id - # Comment.find_by_id(id).nil? # => false + # Comment.find_by(id: id).nil? # => false # # post.save # post.reload.comments.length # => 1 # # Now it _is_ removed from the database: # - # Comment.find_by_id(id).nil? # => true + # Comment.find_by(id: id).nil? # => true module AutosaveAssociation extend ActiveSupport::Concern diff --git a/activerecord/lib/active_record/integration.rb b/activerecord/lib/active_record/integration.rb index 7f877a6471..5eaa4c6987 100644 --- a/activerecord/lib/active_record/integration.rb +++ b/activerecord/lib/active_record/integration.rb @@ -19,7 +19,7 @@ module ActiveRecord # <tt>resources :users</tt> route. Normally, +user_path+ will # construct a path with the user object's 'id' in it: # - # user = User.find_by_name('Phusion') + # user = User.find_by(name: 'Phusion') # user_path(user) # => "/users/1" # # You can override +to_param+ in your model to make +user_path+ construct @@ -31,7 +31,7 @@ module ActiveRecord # end # end # - # user = User.find_by_name('Phusion') + # user = User.find_by(name: 'Phusion') # user_path(user) # => "/users/Phusion" def to_param # We can't use alias_method here, because method 'id' optimizes itself on the fly. diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 3f154bd1cc..2736281e33 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -76,7 +76,7 @@ module ActiveRecord # puts values["Drake"] # # => 43 # - # drake = Family.find_by_last_name('Drake') + # drake = Family.find_by(last_name: 'Drake') # values = Person.group(:family).maximum(:age) # Person belongs_to :family # puts values[drake] # # => 43 diff --git a/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb b/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb index 75a6295350..b0a6e0a550 100644 --- a/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb +++ b/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb @@ -24,11 +24,11 @@ class EagerLoadIncludeFullStiClassNamesTest < ActiveRecord::TestCase old = ActiveRecord::Base.store_full_sti_class ActiveRecord::Base.store_full_sti_class = false - post = Namespaced::Post.includes(:tagging).find_by_title('Great stuff') + post = Namespaced::Post.includes(:tagging).find_by(title: 'Great stuff') assert_nil post.tagging ActiveRecord::Base.store_full_sti_class = true - post = Namespaced::Post.includes(:tagging).find_by_title('Great stuff') + post = Namespaced::Post.includes(:tagging).find_by(title: 'Great stuff') assert_instance_of Tagging, post.tagging ensure ActiveRecord::Base.store_full_sti_class = old diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 1b1b479f1a..3c3355b0f8 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -33,7 +33,7 @@ class ProjectWithAfterCreateHook < ActiveRecord::Base after_create :add_david def add_david - david = DeveloperForProjectWithAfterCreateHook.find_by_name('David') + david = DeveloperForProjectWithAfterCreateHook.find_by(name: 'David') david.projects << self end end @@ -268,7 +268,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert devel.persisted? assert proj2.persisted? 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 def test_create @@ -293,7 +293,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert devel.persisted? assert proj2.persisted? 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 def test_creation_respects_hash_condition @@ -567,7 +567,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase high_id_jamis = projects(:active_record).developers.create(:name => 'Jamis') assert_equal high_id_jamis, projects(:active_record).developers.merge(:where => "name = 'Jamis'").first - assert_equal high_id_jamis, projects(:active_record).developers.find_by_name('Jamis') + assert_equal high_id_jamis, projects(:active_record).developers.find_by(name: 'Jamis') end def test_find_should_prepend_to_association_order @@ -581,8 +581,8 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end def test_new_with_values_in_collection - jamis = DeveloperForProjectWithAfterCreateHook.find_by_name('Jamis') - david = DeveloperForProjectWithAfterCreateHook.find_by_name('David') + jamis = DeveloperForProjectWithAfterCreateHook.find_by(name: 'Jamis') + david = DeveloperForProjectWithAfterCreateHook.find_by(name: 'David') project = ProjectWithAfterCreateHook.new(:name => "Cooking with Bertie") project.developers << jamis project.save! @@ -751,7 +751,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end 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 tag.save! @@ -783,7 +783,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase def test_dynamic_find_should_respect_association_include # SQL error in sort clause if :include is not included # due to Unknown column 'authors.id' - assert Category.find(1).posts_with_authors_sorted_by_author_id.find_by_title('Welcome to the weblog') + assert Category.find(1).posts_with_authors_sorted_by_author_id.find_by(title: 'Welcome to the weblog') end def test_count diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index d42630e1b7..031978f065 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -253,7 +253,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase post = Post.first assert_equal [], person.readers - assert_nil person.readers.find_by_post_id(post.id) + assert_nil person.readers.find_by(post_id: post.id) person.readers.create(:post_id => post.id) @@ -311,7 +311,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_dynamic_find_should_respect_association_order assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.where("type = 'Client'").first - assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client') + assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find_by(type: 'Client') end def test_cant_save_has_many_readonly_association @@ -885,7 +885,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [client_id], Client.destroyed_client_ids[firm.id] # Should be destroyed since the association is dependent. - assert_nil Client.find_by_id(client_id) + assert_nil Client.find_by(id: client_id) end def test_clearing_an_exclusively_dependent_association_collection @@ -905,7 +905,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [], Client.destroyed_client_ids[firm.id] # Should be destroyed since the association is exclusively dependent. - assert_nil Client.find_by_id(client_id) + assert_nil Client.find_by(id: client_id) end def test_dependent_association_respects_optional_conditions_on_delete @@ -954,7 +954,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase old_record = firm.clients_using_primary_key_with_delete_all.first firm = Firm.first firm.destroy - assert_nil Client.find_by_id(old_record.id) + assert_nil Client.find_by(id: old_record.id) end def test_creation_respects_hash_condition @@ -980,7 +980,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_deleting_a_item_which_is_not_in_the_collection force_signal37_to_load_all_clients_of_firm - summit = Client.find_by_name('Summit') + summit = Client.find_by(name: 'Summit') companies(:first_firm).clients_of_firm.delete(summit) assert_equal 1, companies(:first_firm).clients_of_firm.size assert_equal 1, companies(:first_firm).clients_of_firm(true).size @@ -1306,7 +1306,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_dynamic_find_should_respect_association_order_for_through assert_equal Comment.find(10), authors(:david).comments_desc.where("comments.type = 'SpecialComment'").first - assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type('SpecialComment') + assert_equal Comment.find(10), authors(:david).comments_desc.find_by(type: 'SpecialComment') end def test_has_many_through_respects_hash_conditions diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index af91fb2920..0e9af3acec 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -524,7 +524,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_dynamic_find_should_respect_association_include # SQL error in sort clause if :include is not included # due to Unknown column 'comments.id' - assert Person.find(1).posts_with_comments_sorted_by_comment_id.find_by_title('Welcome to the weblog') + assert Person.find(1).posts_with_comments_sorted_by_comment_id.find_by(title: 'Welcome to the weblog') end def test_count_with_include_should_alias_join_table diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 4ed09a3bf7..65562709a2 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -38,7 +38,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase def test_finding_using_primary_key firm = companies(:first_firm) - assert_equal Account.find_by_firm_id(firm.id), firm.account + assert_equal Account.find_by(firm_id: firm.id), firm.account firm.firm_id = companies(:rails_core).id assert_equal accounts(:rails_core_account), firm.account_using_primary_key end @@ -46,7 +46,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase def test_update_with_foreign_and_primary_keys firm = companies(:first_firm) account = firm.account_using_foreign_and_primary_keys - assert_equal Account.find_by_firm_name(firm.name), account + assert_equal Account.find_by(firm_name: firm.name), account firm.save firm.reload assert_equal account, firm.account_using_foreign_and_primary_keys diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index 10ec33be75..368d862f4e 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -448,7 +448,7 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase end def test_has_many_through_uses_correct_attributes - assert_nil posts(:thinking).tags.find_by_name("General").attributes["tag_id"] + assert_nil posts(:thinking).tags.find_by(name: "General").attributes["tag_id"] end def test_associating_unsaved_records_with_has_many_through diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index e5cb4f8f7a..a138b579a3 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -497,7 +497,7 @@ class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCa assert firm.save firm.reload 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 @@ -591,11 +591,11 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase id = @pirate.ship.id assert @pirate.ship.marked_for_destruction? - assert Ship.find_by_id(id) + assert Ship.find_by(id:id) @pirate.save assert_nil @pirate.reload.ship - assert_nil Ship.find_by_id(id) + assert_nil Ship.find_by(id: id) end def test_should_skip_validation_on_a_child_association_if_marked_for_destruction @@ -638,11 +638,11 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase id = @ship.pirate.id assert @ship.pirate.marked_for_destruction? - assert Pirate.find_by_id(id) + assert Pirate.find_by(id: id) @ship.save assert_nil @ship.reload.pirate - assert_nil Pirate.find_by_id(id) + assert_nil Pirate.find_by(id:id) end def test_should_skip_validation_on_a_parent_association_if_marked_for_destruction @@ -698,11 +698,11 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase ids = @pirate.birds.map(&:id) assert @pirate.birds.all? { |child| child.marked_for_destruction? } - ids.each { |id| assert klass.find_by_id(id) } + ids.each { |id| assert klass.find_by(id: id) } @pirate.save assert @pirate.reload.birds.empty? - ids.each { |id| assert_nil klass.find_by_id(id) } + ids.each { |id| assert_nil klass.find_by(id: id) } end def test_should_skip_validation_on_has_many_if_marked_for_destruction diff --git a/activerecord/test/cases/callbacks_test.rb b/activerecord/test/cases/callbacks_test.rb index 7457bafd4e..0a6c071523 100644 --- a/activerecord/test/cases/callbacks_test.rb +++ b/activerecord/test/cases/callbacks_test.rb @@ -476,7 +476,7 @@ class CallbacksTest < ActiveRecord::TestCase david = ImmutableDeveloper.find(1) assert !david.destroy assert_raise(ActiveRecord::RecordNotDestroyed) { david.destroy! } - assert_not_nil ImmutableDeveloper.find_by_id(1) + assert_not_nil ImmutableDeveloper.find_by(id: 1) someone = CallbackCancellationDeveloper.find(1) someone.cancel_before_destroy = true diff --git a/activerecord/test/cases/deprecated_dynamic_methods_test.rb b/activerecord/test/cases/deprecated_dynamic_methods_test.rb index 8e842d8758..cfc5c5d292 100644 --- a/activerecord/test/cases/deprecated_dynamic_methods_test.rb +++ b/activerecord/test/cases/deprecated_dynamic_methods_test.rb @@ -403,9 +403,9 @@ class DeprecatedDynamicMethodsTest < ActiveRecord::TestCase post = Post.first assert_equal [], person.readers - assert_nil person.readers.find_by_post_id(post.id) + assert_nil person.readers.find_by(post_id: post.id) - person.readers.find_or_create_by_post_id(post.id) + person.readers.find_or_create_by(post_id: post.id) assert_equal 1, person.readers.count assert_equal 1, person.readers.length @@ -513,34 +513,34 @@ class DeprecatedDynamicMethodsTest < ActiveRecord::TestCase def test_finder_block t = Topic.first found = nil - Topic.find_by_id(t.id) { |f| found = f } + Topic.find_by(id: t.id) { |f| found = f } assert_equal t, found end def test_finder_block_nothing_found bad_id = Topic.maximum(:id) + 1 - assert_nil Topic.find_by_id(bad_id) { |f| raise } + assert_nil Topic.find_by(id: bad_id) { |f| raise } end def test_find_returns_block_value t = Topic.first - x = Topic.find_by_id(t.id) { |f| "hi mom!" } + x = Topic.find_by(id:t.id) { |f| "hi mom!" } assert_equal "hi mom!", x end def test_dynamic_finder_with_invalid_params - assert_raise(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" } + assert_raise(ArgumentError) { Topic.find_by title: 'No Title', :join => "It should be `joins'" } end def test_find_by_one_attribute_with_order_option - assert_equal accounts(:signals37), Account.find_by_credit_limit(50, :order => 'id') - assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :order => 'id DESC') + assert_equal accounts(:signals37), Account.find_by(credit_limit: 50, :order => 'id') + assert_equal accounts(:rails_core_account), Account.find_by(credit_limit: 50, :order => 'id DESC') end def test_dynamic_find_by_attributes_should_yield_found_object david = authors(:david) yielded_value = nil - Author.find_by_name(david.name) do |author| + Author.find_by(name: david.name) do |author| yielded_value = author end assert_equal david, yielded_value diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index b9961a4420..4fdade2d87 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -249,21 +249,21 @@ class DirtyTest < ActiveRecord::TestCase pirate.save # check the change from 1 to '' - pirate = Pirate.find_by_catchphrase("Yarrrr, me hearties") + pirate = Pirate.find_by(catchphrase: "Yarrrr, me hearties") pirate.parrot_id = '' assert pirate.parrot_id_changed? assert_equal([1, nil], pirate.parrot_id_change) pirate.save # check the change from nil to 0 - pirate = Pirate.find_by_catchphrase("Yarrrr, me hearties") + pirate = Pirate.find_by(catchphrase: "Yarrrr, me hearties") pirate.parrot_id = 0 assert pirate.parrot_id_changed? assert_equal([nil, 0], pirate.parrot_id_change) pirate.save # check the change from 0 to '' - pirate = Pirate.find_by_catchphrase("Yarrrr, me hearties") + pirate = Pirate.find_by(catchphrase: "Yarrrr, me hearties") pirate.parrot_id = '' assert pirate.parrot_id_changed? assert_equal([0, nil], pirate.parrot_id_change) @@ -494,7 +494,7 @@ class DirtyTest < ActiveRecord::TestCase pirate.reload assert_equal Hash.new, pirate.previous_changes - pirate = Pirate.find_by_catchphrase("arrr") + pirate = Pirate.find_by(catchphrase: "arrr") pirate.catchphrase = "Me Maties!" pirate.save! @@ -505,7 +505,7 @@ class DirtyTest < ActiveRecord::TestCase assert !pirate.previous_changes.key?('parrot_id') assert !pirate.previous_changes.key?('created_on') - pirate = Pirate.find_by_catchphrase("Me Maties!") + pirate = Pirate.find_by(catchphrase: "Me Maties!") pirate.catchphrase = "Thar She Blows!" pirate.save @@ -516,7 +516,7 @@ class DirtyTest < ActiveRecord::TestCase assert !pirate.previous_changes.key?('parrot_id') assert !pirate.previous_changes.key?('created_on') - pirate = Pirate.find_by_catchphrase("Thar She Blows!") + pirate = Pirate.find_by(catchphrase: "Thar She Blows!") pirate.update(catchphrase: "Ahoy!") assert_equal 2, pirate.previous_changes.size @@ -526,7 +526,7 @@ class DirtyTest < ActiveRecord::TestCase assert !pirate.previous_changes.key?('parrot_id') assert !pirate.previous_changes.key?('created_on') - pirate = Pirate.find_by_catchphrase("Ahoy!") + pirate = Pirate.find_by_catchphrase(catchphrase: "Ahoy!") pirate.update_attribute(:catchphrase, "Ninjas suck!") assert_equal 2, pirate.previous_changes.size diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index b0b29f5f42..58f9f7e3b9 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -66,7 +66,7 @@ class FixturesTest < ActiveRecord::TestCase def test_create_fixtures fixtures = ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT, "parrots") - assert Parrot.find_by_name('Curious George'), 'George is not in the database' + assert Parrot.find_by(name: 'Curious George'), 'George is not in the database' assert fixtures.detect { |f| f.name == 'parrots' }, "no fixtures named 'parrots' in #{fixtures.map(&:name).inspect}" end @@ -80,7 +80,7 @@ class FixturesTest < ActiveRecord::TestCase def test_create_symbol_fixtures fixtures = ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT, :collections, :collections => Course) { Course.connection } - assert Course.find_by_name('Collection'), 'course is not in the database' + assert Course.find_by(name: 'Collection'), 'course is not in the database' assert fixtures.detect { |f| f.name == 'collections' }, "no fixtures named 'collections' in #{fixtures.map(&:name).inspect}" end @@ -738,7 +738,7 @@ class ActiveSupportSubclassWithFixturesTest < ActiveRecord::TestCase # This seemingly useless assertion catches a bug that caused the fixtures # setup code call nil[] def test_foo - assert_equal parrots(:louis), Parrot.find_by_name("King Louis") + assert_equal parrots(:louis), Parrot.find_by(name: "King Louis") end end diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 8e5379cb1f..b6fc7b745e 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -54,7 +54,7 @@ class PrimaryKeysTest < ActiveRecord::TestCase Keyboard.delete_all keyboard = Keyboard.new(:name => 'HHKB') assert_nothing_raised { keyboard.save! } - assert_equal keyboard.id, Keyboard.find_by_name('HHKB').id + assert_equal keyboard.id, Keyboard.find_by(name: 'HHKB').id end def test_customized_primary_key_can_be_get_before_saving diff --git a/activerecord/test/cases/reload_models_test.rb b/activerecord/test/cases/reload_models_test.rb index 0d16a3526f..6775116216 100644 --- a/activerecord/test/cases/reload_models_test.rb +++ b/activerecord/test/cases/reload_models_test.rb @@ -6,8 +6,8 @@ class ReloadModelsTest < ActiveRecord::TestCase fixtures :pets def test_has_one_with_reload - pet = Pet.find_by_name('parrot') - pet.owner = Owner.find_by_name('ashley') + pet = Pet.find_by(name: 'parrot') + pet.owner = Owner.find_by(name: 'ashley') # Reload the class Owner, simulating auto-reloading of model classes in a # development environment. Note that meanwhile the class Pet is not @@ -15,8 +15,8 @@ class ReloadModelsTest < ActiveRecord::TestCase Object.class_eval { remove_const :Owner } Kernel.load(File.expand_path(File.join(File.dirname(__FILE__), "../models/owner.rb"))) - pet = Pet.find_by_name('parrot') - pet.owner = Owner.find_by_name('ashley') - assert_equal pet.owner, Owner.find_by_name('ashley') + pet = Pet.find_by(name: 'parrot') + pet.owner = Owner.find_by(name: 'ashley') + assert_equal pet.owner, Owner.find_by(name: 'ashley') end end diff --git a/activerecord/test/cases/store_test.rb b/activerecord/test/cases/store_test.rb index 43bf285ba9..be5782c781 100644 --- a/activerecord/test/cases/store_test.rb +++ b/activerecord/test/cases/store_test.rb @@ -77,7 +77,7 @@ class StoreTest < ActiveRecord::TestCase end test "convert store attributes from Hash to HashWithIndifferentAccess saving the data and access attributes indifferently" do - user = Admin::User.find_by_name('Jamis') + user = Admin::User.find_by(name: 'Jamis') assert_equal 'symbol', user.settings[:symbol] assert_equal 'symbol', user.settings['symbol'] assert_equal 'string', user.settings[:string] |