Remove deprecated find_first and find_all.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6998 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2007-06-11 07:45:56 +00:00
parent 800b899f96
commit 7858a32e1b
9 changed files with 36 additions and 248 deletions

View File

@@ -1205,7 +1205,6 @@ module ActiveRecord
deprecated_remove_association_relation(association_name)
deprecated_has_collection_method(association_name)
deprecated_find_in_collection_method(association_name)
deprecated_find_all_in_collection_method(association_name)
deprecated_collection_create_method(association_name)
deprecated_collection_build_method(association_name)
end

View File

@@ -20,25 +20,6 @@ module ActiveRecord
end
end
# DEPRECATED.
def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil)
if @reflection.options[:finder_sql]
@reflection.klass.find_by_sql(@finder_sql)
else
conditions = @finder_sql
conditions += " AND (#{sanitize_sql(runtime_conditions)})" if runtime_conditions
orderings ||= @reflection.options[:order]
@reflection.klass.find_all(conditions, orderings, limit, joins)
end
end
deprecate :find_all => "use find(:all, ...) instead"
# DEPRECATED. Find the first associated record. All arguments are optional.
def find_first(conditions = nil, orderings = nil)
find_all(conditions, orderings, 1).first
end
deprecate :find_first => "use find(:first, ...) instead"
# Count the number of associated records. All arguments are optional.
def count(*args)
if @reflection.options[:counter_sql]

View File

@@ -1,7 +1,6 @@
require 'base64'
require 'yaml'
require 'set'
require 'active_record/deprecated_finders'
module ActiveRecord #:nodoc:
class ActiveRecordError < StandardError #:nodoc:
@@ -1207,7 +1206,7 @@ module ActiveRecord #:nodoc:
# or find_or_create_by_user_and_password(user, password).
def method_missing(method_id, *arguments)
if match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
finder, deprecated_finder = determine_finder(match), determine_deprecated_finder(match)
finder = determine_finder(match)
attribute_names = extract_attribute_names_from_match(match)
super unless all_attributes_exists?(attribute_names)
@@ -1234,9 +1233,7 @@ module ActiveRecord #:nodoc:
end
else
ActiveSupport::Deprecation.silence do
send(deprecated_finder, sanitize_sql(attributes), *arguments[attribute_names.length..-1])
end
raise ArgumentError, "Unrecognized arguments for #{method_id}: #{extra_options.inspect}"
end
elsif match = /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/.match(method_id.to_s)
instantiator = determine_instantiator(match)
@@ -1262,10 +1259,6 @@ module ActiveRecord #:nodoc:
match.captures.first == 'all_by' ? :find_every : :find_initial
end
def determine_deprecated_finder(match)
match.captures.first == 'all_by' ? :find_all : :find_first
end
def determine_instantiator(match)
match.captures.first == 'initialize' ? :new : :create
end

View File

@@ -49,17 +49,6 @@ module ActiveRecord
end_eval
end
def deprecated_find_all_in_collection_method(collection_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def find_all_in_#{collection_name}(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil)
ActiveSupport::Deprecation.silence do
#{collection_name}.find_all(runtime_conditions, orderings, limit, joins)
end
end
deprecate :find_all_in_#{collection_name} => "use #{collection_name}.find(:all, ...) instead"
end_eval
end
def deprecated_collection_create_method(collection_name)# :nodoc:
module_eval <<-"end_eval", __FILE__, __LINE__
def create_in_#{collection_name}(attributes = {})

View File

@@ -1,44 +0,0 @@
module ActiveRecord
class Base
class << self
# DEPRECATION NOTICE: This method is deprecated in favor of find with the :conditions option.
#
# Works like find, but the record matching +id+ must also meet the +conditions+.
# +RecordNotFound+ is raised if no record can be found matching the +id+ or meeting the condition.
# Example:
# Person.find_on_conditions 5, "first_name LIKE '%dav%' AND last_name = 'heinemeier'"
def find_on_conditions(ids, conditions) # :nodoc:
find(ids, :conditions => conditions)
end
deprecate :find_on_conditions => "use find(ids, :conditions => conditions)"
# DEPRECATION NOTICE: This method is deprecated in favor of find(:first, options).
#
# Returns the object for the first record responding to the conditions in +conditions+,
# such as "group = 'master'". If more than one record is returned from the query, it's the first that'll
# be used to create the object. In such cases, it might be beneficial to also specify
# +orderings+, like "income DESC, name", to control exactly which record is to be used. Example:
# Employee.find_first "income > 50000", "income DESC, name"
def find_first(conditions = nil, orderings = nil, joins = nil) # :nodoc:
find(:first, :conditions => conditions, :order => orderings, :joins => joins)
end
deprecate :find_first => "use find(:first, ...)"
# DEPRECATION NOTICE: This method is deprecated in favor of find(:all, options).
#
# Returns an array of all the objects that could be instantiated from the associated
# table in the database. The +conditions+ can be used to narrow the selection of objects (WHERE-part),
# such as by "color = 'red'", and arrangement of the selection can be done through +orderings+ (ORDER BY-part),
# such as by "last_name, first_name DESC". A maximum of returned objects and their offset can be specified in
# +limit+ with either just a single integer as the limit or as an array with the first element as the limit,
# the second as the offset. Examples:
# Project.find_all "category = 'accounts'", "last_accessed DESC", 15
# Project.find_all ["category = ?", category_name], "created ASC", [15, 20]
def find_all(conditions = nil, orderings = nil, limit = nil, joins = nil) # :nodoc:
limit, offset = limit.is_a?(Array) ? limit : [ limit, nil ]
find(:all, :conditions => conditions, :order => orderings, :joins => joins, :limit => limit, :offset => offset)
end
deprecate :find_all => "use find(:all, ...)"
end
end
end

View File

@@ -534,44 +534,30 @@ class HasManyAssociationsTest < Test::Unit::TestCase
end
def test_find_all
assert_deprecated 'find_all' do
firm = Firm.find_first
assert_equal firm.clients, firm.clients.find_all
assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length
assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
end
firm = Firm.find(:first)
assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length
assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
end
def test_find_all_sanitized
assert_deprecated 'find_all' do
firm = Firm.find_first
assert_equal firm.clients.find_all("name = 'Summit'"), firm.clients.find_all(["name = '%s'", "Summit"])
summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"])
assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }])
end
firm = Firm.find(:first)
summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"])
assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }])
end
def test_find_first
assert_deprecated 'find_first' do
firm = Firm.find_first
client2 = Client.find(2)
assert_equal firm.clients.first, firm.clients.find_first
assert_equal client2, firm.clients.find_first("#{QUOTED_TYPE} = 'Client'")
assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'")
end
firm = Firm.find(:first)
client2 = Client.find(2)
assert_equal firm.clients.first, firm.clients.find(:first)
assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'")
end
def test_find_first_sanitized
assert_deprecated 'find_first' do
firm = Firm.find_first
client2 = Client.find(2)
assert_deprecated(/find_first/) do
assert_equal client2, firm.clients.find_first(["#{QUOTED_TYPE} = ?", "Client"])
end
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'])
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }])
end
firm = Firm.find(:first)
client2 = Client.find(2)
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'])
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }])
end
def test_find_in_collection
@@ -1647,7 +1633,7 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
assert david.projects(true).empty?
end
def test_rich_association
def test_deprecated_push_with_attributes_was_removed
jamis = developers(:jamis)
assert_raise(NoMethodError) do
jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today)

View File

@@ -17,8 +17,6 @@ raise "ActiveRecord should have barked on bad collection keys" unless bad_collec
class DeprecatedAssociationWarningsTest < Test::Unit::TestCase
def test_deprecation_warnings
assert_deprecated('find_first') { Firm.find_first }
assert_deprecated('find_all') { Firm.find_all }
assert_deprecated('has_account?') { Firm.find(:first).has_account? }
assert_deprecated('has_clients?') { Firm.find(:first).has_clients? }
end
@@ -239,7 +237,7 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
def test_has_and_belongs_to_many_zero
david = Developer.find(1)
assert_deprecated do
david.remove_projects Project.find_all
david.remove_projects Project.find(:all)
assert_equal 0, david.projects_count
assert !david.has_projects?
end
@@ -358,9 +356,8 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
end
def test_has_many_find_all
assert_deprecated 'find_all_in_clients' do
assert_equal 2, @firm.find_all_in_clients("#{QUOTED_TYPE} = 'Client'").length
assert_equal 1, @firm.find_all_in_clients("name = 'Summit'").length
assert_raise(NoMethodError) do
@firm.find_all_in_clients("#{QUOTED_TYPE} = 'Client'")
end
end

View File

@@ -1,90 +1,19 @@
require 'abstract_unit'
require 'fixtures/company'
require 'fixtures/topic'
require 'fixtures/reply'
require 'fixtures/entrant'
require 'fixtures/developer'
class DeprecatedFinderTest < Test::Unit::TestCase
fixtures :companies, :topics, :entrants, :developers
fixtures :entrants
def test_find_all_with_limit
entrants = assert_deprecated { Entrant.find_all nil, "id ASC", 2 }
assert_equal 2, entrants.size
assert_equal entrants(:first), entrants.first
def test_deprecated_find_all_was_removed
assert_raise(NoMethodError) { Entrant.find_all }
end
def test_find_all_with_prepared_limit_and_offset
entrants = assert_deprecated { Entrant.find_all nil, "id ASC", [2, 1] }
assert_equal 2, entrants.size
assert_equal entrants(:second), entrants.first
def test_deprecated_find_first_was_removed
assert_raise(NoMethodError) { Entrant.find_first }
end
def test_find_first
first = assert_deprecated { Topic.find_first "title = 'The First Topic'" }
assert_equal topics(:first), first
end
def test_find_first_failing
first = assert_deprecated { Topic.find_first "title = 'The First Topic!'" }
assert_nil first
end
def test_deprecated_find_on_conditions
assert_deprecated 'find_on_conditions' do
assert Topic.find_on_conditions(1, ["approved = ?", false])
assert_raises(ActiveRecord::RecordNotFound) { Topic.find_on_conditions(1, ["approved = ?", true]) }
end
end
def test_condition_interpolation
assert_deprecated do
assert_kind_of Firm, Company.find_first(["name = '%s'", "37signals"])
assert_nil Company.find_first(["name = '%s'", "37signals!"])
assert_nil Company.find_first(["name = '%s'", "37signals!' OR 1=1"])
assert_kind_of Time, Topic.find_first(["id = %d", 1]).written_on
end
end
def test_bind_variables
assert_deprecated do
assert_kind_of Firm, Company.find_first(["name = ?", "37signals"])
assert_nil Company.find_first(["name = ?", "37signals!"])
assert_nil Company.find_first(["name = ?", "37signals!' OR 1=1"])
assert_kind_of Time, Topic.find_first(["id = ?", 1]).written_on
assert_raises(ActiveRecord::PreparedStatementInvalid) {
Company.find_first(["id=? AND name = ?", 2])
}
assert_raises(ActiveRecord::PreparedStatementInvalid) {
Company.find_first(["id=?", 2, 3, 4])
}
end
end
def test_bind_variables_with_quotes
Company.create("name" => "37signals' go'es agains")
assert_deprecated do
assert_not_nil Company.find_first(["name = ?", "37signals' go'es agains"])
end
end
def test_named_bind_variables_with_quotes
Company.create("name" => "37signals' go'es agains")
assert_deprecated do
assert_not_nil Company.find_first(["name = :name", {:name => "37signals' go'es agains"}])
end
end
def test_named_bind_variables
assert_equal '1', bind(':a', :a => 1) # ' ruby-mode
assert_equal '1 1', bind(':a :a', :a => 1) # ' ruby-mode
assert_deprecated do
assert_kind_of Firm, Company.find_first(["name = :name", { :name => "37signals" }])
assert_nil Company.find_first(["name = :name", { :name => "37signals!" }])
assert_nil Company.find_first(["name = :name", { :name => "37signals!' OR 1=1" }])
assert_kind_of Time, Topic.find_first(["id = :id", { :id => 1 }]).written_on
end
def test_deprecated_find_on_conditions_was_removed
assert_raise(NoMethodError) { Entrant.find_on_conditions }
end
def test_count
@@ -98,54 +27,4 @@ class DeprecatedFinderTest < Test::Unit::TestCase
assert_equal(1, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 2]))
assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
end
def test_find_all_with_limit
assert_deprecated do
first_five_developers = Developer.find_all nil, 'id ASC', 5
assert_equal 5, first_five_developers.length
assert_equal 'David', first_five_developers.first.name
assert_equal 'fixture_5', first_five_developers.last.name
no_developers = Developer.find_all nil, 'id ASC', 0
assert_equal 0, no_developers.length
assert_equal first_five_developers, Developer.find_all(nil, 'id ASC', [5])
assert_equal no_developers, Developer.find_all(nil, 'id ASC', [0])
end
end
def test_find_all_with_limit_and_offset
assert_deprecated do
first_three_developers = Developer.find_all nil, 'id ASC', [3, 0]
second_three_developers = Developer.find_all nil, 'id ASC', [3, 3]
last_two_developers = Developer.find_all nil, 'id ASC', [2, 8]
assert_equal 3, first_three_developers.length
assert_equal 3, second_three_developers.length
assert_equal 2, last_two_developers.length
assert_equal 'David', first_three_developers.first.name
assert_equal 'fixture_4', second_three_developers.first.name
assert_equal 'fixture_9', last_two_developers.first.name
end
end
def test_find_all_by_one_attribute_with_options
assert_not_deprecated do
topics = Topic.find_all_by_content("Have a nice day", "id DESC")
assert topics(:first), topics.last
topics = Topic.find_all_by_content("Have a nice day", "id DESC")
assert topics(:first), topics.first
end
end
protected
def bind(statement, *vars)
if vars.first.is_a?(Hash)
ActiveRecord::Base.send(:replace_named_bind_variables, statement, vars.first)
else
ActiveRecord::Base.send(:replace_bind_variables, statement, vars)
end
end
end

View File

@@ -176,6 +176,14 @@ class FinderTest < Test::Unit::TestCase
assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
end
def test_condition_interpolation
assert_kind_of Firm, Company.find(:first, :conditions => ["name = '%s'", "37signals"])
assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!"])
assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!' OR 1=1"])
assert_kind_of Time, Topic.find(:first, :conditions => ["id = %d", 1]).written_on
end
def test_condition_array_interpolation
assert_kind_of Firm, Company.find(:first, :conditions => ["name = '%s'", "37signals"])
assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!"])