Skip to content

Commit

Permalink
Updated all references to the old find_first and find_all to use the …
Browse files Browse the repository at this point in the history
…new style #1511 [Marcel Molina]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1520 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jun 26, 2005
1 parent ee4c834 commit 3dfa56c
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 92 deletions.
9 changes: 3 additions & 6 deletions activerecord/lib/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ def last?

def higher_item
return nil unless in_list?
self.class.find_first(
self.class.find(:first, :conditions =>
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}"
)
end

def lower_item
return nil unless in_list?
self.class.find_first(
self.class.find(:first, :conditions =>
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}"
)
end
Expand All @@ -169,10 +169,7 @@ def bottom_position_in_list
end

def bottom_item
self.class.find_first(
"#{scope_condition} ",
"#{position_column} DESC"
)
self.class.find(:first, :conditions => scope_condition, :order => "#{position_column} DESC")
end

def assume_bottom_position
Expand Down
8 changes: 4 additions & 4 deletions activerecord/lib/active_record/acts/nested_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,17 @@ def children_count

# Returns a set of itself and all of it's nested children
def full_set
self.class.find_all( "#{scope_condition} AND (#{left_col_name} BETWEEN #{self[left_col_name]} and #{self[right_col_name]})" )
self.class.find(:all, :conditions => "#{scope_condition} AND (#{left_col_name} BETWEEN #{self[left_col_name]} and #{self[right_col_name]})" )
end

# Returns a set of all of it's children and nested children
def all_children
self.class.find_all( "#{scope_condition} AND (#{left_col_name} > #{self[left_col_name]}) and (#{right_col_name} < #{self[right_col_name]})" )
self.class.find(:all, :conditions => "#{scope_condition} AND (#{left_col_name} > #{self[left_col_name]}) and (#{right_col_name} < #{self[right_col_name]})" )
end

# Returns a set of only this entries immediate children
def direct_children
self.class.find_all( "#{scope_condition} and #{parent_column} = #{self.id}")
self.class.find(:all, :conditions => "#{scope_condition} and #{parent_column} = #{self.id}")
end

# Prunes a branch off of the tree, shifting all of the elements on the right
Expand All @@ -209,4 +209,4 @@ def before_destroy
end
end
end
end
end
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def has_many(association_id, options = {})
# with +attributes+ and linked to this object through a foreign key and that has already been saved (if it passed the validation).
#
# Example: An Account class declares <tt>has_one :beneficiary</tt>, which will add:
# * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.find_first "account_id = #{id}"</tt>)
# * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.find(:first, :conditions => "account_id = #{id}")</tt>)
# * <tt>Account#beneficiary=(beneficiary)</tt> (similar to <tt>beneficiary.account_id = account.id; beneficiary.save</tt>)
# * <tt>Account#beneficiary.nil?</tt>
# * <tt>Account#build_beneficiary</tt> (similar to <tt>Beneficiary.new("account_id" => id)</tt>)
Expand Down
14 changes: 7 additions & 7 deletions activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def initialize(errors)
#
# User < ActiveRecord::Base
# def self.authenticate_unsafely(user_name, password)
# find_first("user_name = '#{user_name}' AND password = '#{password}'")
# find(:first, :conditions => "user_name = '#{user_name}' AND password = '#{password}'")
# end
#
# def self.authenticate_safely(user_name, password)
# find_first([ "user_name = ? AND password = ?", user_name, password ])
# find(:first, :conditions => [ "user_name = ? AND password = ?", user_name, password ])
# end
# end
#
Expand Down Expand Up @@ -362,7 +362,7 @@ def find(*args)
end
end

# Works like find_all, but requires a complete SQL string. Examples:
# Works like find(:all), but requires a complete SQL string. Examples:
# Post.find_by_sql "SELECT p.*, c.author FROM posts p, comments c WHERE p.id = c.post_id"
# Post.find_by_sql ["SELECT * FROM posts WHERE author = ? AND created > ?", author_id, start_date]
def find_by_sql(sql)
Expand Down Expand Up @@ -426,7 +426,7 @@ def update_all(updates, conditions = nil)
# the destroy method. Example:
# Person.destroy_all "last_login < '2004-04-04'"
def destroy_all(conditions = nil)
find_all(conditions).each { |object| object.destroy }
find(:all, :conditions => conditions).each { |object| object.destroy }
end

# Deletes all the records that matches the +condition+ without instantiating the objects first (and hence not
Expand Down Expand Up @@ -679,7 +679,7 @@ def sanitize(object) #:nodoc:
# Project.benchmark("Creating project") do
# project = Project.create("name" => "stuff")
# project.create_manager("name" => "David")
# project.milestones << Milestone.find_all
# project.milestones << Milestone.find(:all)
# end
def benchmark(title)
result = nil
Expand Down Expand Up @@ -777,8 +777,8 @@ def undecorated_table_name(class_name = class_name_of_active_record_descendant(s
end

# Enables dynamic finders like find_by_user_name(user_name) and find_by_user_name_and_password(user_name, password) that are turned into
# find_first(["user_name = ?", user_name]) and find_first(["user_name = ? AND password = ?", user_name, password]) respectively. Also works
# for find_all, but using find_all_by_amount(50) that are turned into find_all(["amount = ?", 50]).
# find(:first, :conditions => ["user_name = ?", user_name]) and find(:first, :conditions => ["user_name = ? AND password = ?", user_name, password])
# respectively. Also works for find(:all), but using find_all_by_amount(50) that are turned into find(:all, :conditions => ["amount = ?", 50]).
#
# It's even possible to use all the additional parameters to find. For example, the full interface for find_all_by_amount
# is actually find_all_by_amount(amount, options).
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ module ActiveRecord
#
# == The after_find and after_initialize exceptions
#
# Because after_find and after_initialize is called for each object instantiated found by a finder, such as Base.find_all, we've had
# Because after_find and after_initialize is called for each object instantiated found by a finder, such as Base.find(:all), we've had
# to implement a simple performance constraint (50% more speed on a simple test case). Unlike all the other callbacks, after_find and
# after_initialize will only be run if an explicit implementation is defined (<tt>def after_find</tt>). In that case, all of the
# callback types will be called.
Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@
# fixtures :foos
#
# def test_godzilla
# assert !Foo.find_all.emtpy?
# assert !Foo.find(:all).empty?
# Foo.destroy_all
# assert Foo.find_all.emtpy?
# assert Foo.find(:all).empty?
# end
#
# def test_godzilla_aftermath
# assert !Foo.find_all.emtpy?
# assert !Foo.find(:all).empty?
# end
# end
#
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,11 @@ def validates_uniqueness_of(*attr_names)

if scope = configuration[:scope]
validates_each(attr_names,configuration) do |record, attr_name, value|
record.errors.add(attr_name, configuration[:message]) if record.class.find_first(record.new_record? ? ["#{attr_name} = ? AND #{scope} = ?", record.send(attr_name), record.send(scope)] : ["#{attr_name} = ? AND #{record.class.primary_key} <> ? AND #{scope} = ?", record.send(attr_name), record.send(:id), record.send(scope)])
record.errors.add(attr_name, configuration[:message]) if record.class.find(:first, :conditions => (record.new_record? ? ["#{attr_name} = ? AND #{scope} = ?", record.send(attr_name), record.send(scope)] : ["#{attr_name} = ? AND #{record.class.primary_key} <> ? AND #{scope} = ?", record.send(attr_name), record.send(:id), record.send(scope)]))
end
else
validates_each(attr_names,configuration) do |record, attr_name, value|
record.errors.add(attr_name, configuration[:message]) if record.class.find_first(record.new_record? ? ["#{attr_name} = ?", record.send(attr_name)] : ["#{attr_name} = ? AND #{record.class.primary_key} <> ?", record.send(attr_name), record.send(:id) ] )
record.errors.add(attr_name, configuration[:message]) if record.class.find(:first, :conditions => (record.new_record? ? ["#{attr_name} = ?", record.send(attr_name)] : ["#{attr_name} = ? AND #{record.class.primary_key} <> ?", record.send(attr_name), record.send(:id) ] ))
end
end
end
Expand Down
48 changes: 24 additions & 24 deletions activerecord/test/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_dependence
firm = Firm.find(1)
assert !firm.account.nil?
firm.destroy
assert_equal 1, Account.find_all.length
assert_equal 1, Account.count
end

def test_succesful_build_association
Expand Down Expand Up @@ -259,45 +259,45 @@ def force_signal37_to_load_all_clients_of_firm
end

def test_counting
assert_equal 2, Firm.find_first.clients.count
assert_equal 2, Firm.find(:first).clients.count
end

def test_finding
assert_equal 2, Firm.find_first.clients.length
assert_equal 2, Firm.find(:first).clients.length
end

def test_finding_default_orders
assert_equal "Summit", Firm.find_first.clients.first.name
assert_equal "Summit", Firm.find(:first).clients.first.name
end

def test_finding_with_different_class_name_and_order
assert_equal "Microsoft", Firm.find_first.clients_sorted_desc.first.name
assert_equal "Microsoft", Firm.find(:first).clients_sorted_desc.first.name
end

def test_finding_with_foreign_key
assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name
assert_equal "Microsoft", Firm.find(:first).clients_of_firm.first.name
end

def test_finding_with_condition
assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name
assert_equal "Microsoft", Firm.find(:first).clients_like_ms.first.name
end

def test_finding_using_sql
firm = Firm.find_first
firm = Firm.find(:first)
first_client = firm.clients_using_sql.first
assert_not_nil first_client
assert_equal "Microsoft", first_client.name
assert_equal 1, firm.clients_using_sql.size
assert_equal 1, Firm.find_first.clients_using_sql.size
assert_equal 1, Firm.find(:first).clients_using_sql.size
end

def test_counting_using_sql
assert_equal 1, Firm.find_first.clients_using_counter_sql.size
assert_equal 0, Firm.find_first.clients_using_zero_counter_sql.size
assert_equal 1, Firm.find(:first).clients_using_counter_sql.size
assert_equal 0, Firm.find(:first).clients_using_zero_counter_sql.size
end

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

def test_belongs_to_sanity
Expand Down Expand Up @@ -547,7 +547,7 @@ def test_dependence
end

def test_destroy_dependent_when_deleted_from_association
firm = Firm.find_first
firm = Firm.find(:first)
assert_equal 2, firm.clients.size

client = firm.clients.first
Expand Down Expand Up @@ -579,17 +579,17 @@ def test_dependence_with_transaction_support_on_failure
end

def test_dependence_on_account
assert_equal 2, Account.find_all.length
assert_equal 2, Account.count
companies(:first_firm).destroy
assert_equal 1, Account.find_all.length
assert_equal 1, Account.count
end

def test_included_in_collection
assert companies(:first_firm).clients.include?(Client.find(2))
end

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

def test_find_all_without_conditions
Expand All @@ -598,15 +598,15 @@ def test_find_all_without_conditions
end

def test_replace_with_less
firm = Firm.find_first
firm = Firm.find(:first)
firm.clients = [companies(:first_client)]
assert firm.save, "Could not save firm"
firm.reload
assert_equal 1, firm.clients.length
end

def test_replace_with_new
firm = Firm.find_first
firm = Firm.find(:first)
new_client = Client.new("name" => "New Client")
firm.clients = [companies(:second_client),new_client]
firm.save
Expand Down Expand Up @@ -699,7 +699,7 @@ def test_belongs_to_counter
end

def test_assignment_before_parent_saved
client = Client.find_first
client = Client.find(:first)
apple = Firm.new("name" => "Apple")
client.firm = apple
assert_equal apple, client.firm
Expand Down Expand Up @@ -738,15 +738,15 @@ def test_assignment_before_either_saved

def test_new_record_with_foreign_key_but_no_object
c = Client.new("firm_id" => 1)
assert_equal Firm.find_first, c.firm_with_basic_id
assert_equal Firm.find(:first), c.firm_with_basic_id
end

def test_forgetting_the_load_when_foreign_key_enters_late
c = Client.new
assert_nil c.firm_with_basic_id

c.firm_id = 1
assert_equal Firm.find_first, c.firm_with_basic_id
assert_equal Firm.find(:first), c.firm_with_basic_id
end

def test_field_name_same_as_foreign_key
Expand All @@ -764,7 +764,7 @@ def xtest_counter_cache
apple.companies_count = 2
apple.save

apple = Firm.find_first("name = 'Apple'")
apple = Firm.find(:first, :conditions => "name = 'Apple'")
assert_equal 2, apple.clients.size, "Should use the new cached number"

apple.clients.to_s
Expand Down Expand Up @@ -966,7 +966,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.find(:all))
assert_equal 0, david.projects.size
assert_equal 0, david.projects(true).size
end
Expand All @@ -986,7 +986,7 @@ def test_deleting_array_with_sql
active_record.developers.reload
assert_equal 2, 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
end

Expand Down
10 changes: 5 additions & 5 deletions activerecord/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_update_array_content
end

def test_attributes_hash
assert_equal @loaded_fixtures['projects']['active_record'].to_hash, Project.find_first.attributes
assert_equal @loaded_fixtures['projects']['active_record'].to_hash, Project.find(:first).attributes
end

def test_create
Expand Down Expand Up @@ -227,13 +227,13 @@ def test_initialize_with_invalid_attribute
end

def test_load
topics = Topic.find_all nil, "id"
topics = Topic.find(:all, :order => 'id')
assert_equal(2, topics.size)
assert_equal(topics(:first).title, topics.first.title)
end

def test_load_with_condition
topics = Topic.find_all "author_name = 'Mary'"
topics = Topic.find(:all, :conditions => "author_name = 'Mary'")

assert_equal(1, topics.size)
assert_equal(topics(:second).title, topics.first.title)
Expand Down Expand Up @@ -276,10 +276,10 @@ def test_table_name_guesses
end

def test_destroy_all
assert_equal 2, Topic.find_all.size
assert_equal 2, Topic.count

Topic.destroy_all "author_name = 'Mary'"
assert_equal 1, Topic.find_all.size
assert_equal 1, Topic.count
end

def test_destroy_many
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_find_all_with_limit
def test_find_all_with_prepared_limit_and_offset
if ActiveRecord::ConnectionAdapters.const_defined? :OracleAdapter
if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OracleAdapter)
assert_raises(ArgumentError) { Entrant.find_all nil, "id ASC", [2, 1] }
assert_raises(ArgumentError) { Entrant.find(:all, :order => 'id ASC', :limit => 2, :offset => 1) }
end
else
entrants = Entrant.find(:all, :order => "id ASC", :limit => 2, :offset => 1)
Expand Down
Loading

0 comments on commit 3dfa56c

Please sign in to comment.