Skip to content

Commit

Permalink
User Rails 4 find_by
Browse files Browse the repository at this point in the history
  • Loading branch information
robertomiranda committed Jan 18, 2013
1 parent 4095c08 commit 7baecc4
Show file tree
Hide file tree
Showing 23 changed files with 70 additions and 70 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/abstract_controller/helpers.rb
Expand Up @@ -29,7 +29,7 @@ def inherited(klass)
# 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?
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_controller/metal/http_authentication.rb
Expand Up @@ -29,7 +29,7 @@ module HttpAuthentication
#
# 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
Expand Down Expand Up @@ -344,7 +344,7 @@ def opaque(secret_key)
#
# 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
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/test_case.rb
Expand Up @@ -275,7 +275,7 @@ def exists?
# 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
#
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/dirty.rb
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/secure_password.rb
Expand Up @@ -37,8 +37,8 @@ module ClassMethods
# 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)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations.rb
Expand Up @@ -987,7 +987,7 @@ def association_instance_set(name, association)
# 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.
#
Expand Down
8 changes: 4 additions & 4 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -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
#
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/integration.rb
Expand Up @@ -19,7 +19,7 @@ module Integration
# <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
Expand All @@ -31,7 +31,7 @@ module Integration
# 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.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -76,7 +76,7 @@ def sum(*args)
# 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
Expand Down
Expand Up @@ -24,11 +24,11 @@ def test_class_names
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
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -268,7 +268,7 @@ def test_build_by_new_record
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
Expand All @@ -293,7 +293,7 @@ def test_create_by_new_record
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
Expand Down Expand Up @@ -567,7 +567,7 @@ def test_dynamic_find_should_respect_association_order
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
Expand All @@ -581,8 +581,8 @@ def test_dynamic_find_all_should_respect_readonly_access
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!
Expand Down Expand Up @@ -751,7 +751,7 @@ def test_assign_ids_ignoring_blanks
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!
Expand Down Expand Up @@ -783,7 +783,7 @@ def test_self_referential_habtm_without_foreign_key_set_should_raise_exception
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
Expand Down
Expand Up @@ -253,7 +253,7 @@ def test_create_resets_cached_counters
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)

Expand Down Expand Up @@ -311,7 +311,7 @@ def test_find_should_prepend_to_association_order

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
Expand Down Expand Up @@ -878,7 +878,7 @@ def test_clearing_a_dependent_association_collection
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
Expand All @@ -898,7 +898,7 @@ def test_clearing_an_exclusively_dependent_association_collection
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
Expand Down Expand Up @@ -947,7 +947,7 @@ def test_delete_all_association_with_primary_key_deletes_correct_records
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
Expand All @@ -973,7 +973,7 @@ def test_clearing_without_initial_access

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
Expand Down Expand Up @@ -1299,7 +1299,7 @@ def test_modifying_a_through_a_has_many_should_raise

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
Expand Down
Expand Up @@ -524,7 +524,7 @@ def test_association_callback_ordering
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
Expand Down
Expand Up @@ -38,15 +38,15 @@ def test_with_select

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

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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -448,7 +448,7 @@ def test_has_many_through_uses_conditions_specified_on_the_has_many_association
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
Expand Down
14 changes: 7 additions & 7 deletions activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -497,7 +497,7 @@ def test_replace_on_new_object
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

Expand Down Expand Up @@ -591,11 +591,11 @@ def test_should_destroy_a_child_association_as_part_of_the_save_transaction_if_i
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
Expand Down Expand Up @@ -638,11 +638,11 @@ def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_
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
Expand Down Expand Up @@ -698,11 +698,11 @@ def test_should_destroy_has_many_as_part_of_the_save_transaction_if_they_were_ma
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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/callbacks_test.rb
Expand Up @@ -476,7 +476,7 @@ def test_before_destroy_returning_false
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
Expand Down

0 comments on commit 7baecc4

Please sign in to comment.