Permalink
Browse files

Deprecate ActiveRecord::Base.scoped.

It doesn't serve much purpose now that ActiveRecord::Base.all returns a
Relation.

The code is moved to active_record_deprecated_finders.
  • Loading branch information...
1 parent 6a81ccd commit b658cf1198bbeb0fb702cd10c6f491cd90cedba0 @jonleighton jonleighton committed Jul 27, 2012
Showing with 497 additions and 504 deletions.
  1. +1 −1 activerecord/lib/active_record/associations/association.rb
  2. +1 −5 activerecord/lib/active_record/associations/collection_proxy.rb
  3. +1 −1 activerecord/lib/active_record/associations/through_association.rb
  4. +1 −1 activerecord/lib/active_record/relation/query_methods.rb
  5. +1 −5 activerecord/lib/active_record/scoping/named.rb
  6. +1 −1 activerecord/test/cases/adapters/mysql/reserved_word_test.rb
  7. +1 −1 activerecord/test/cases/adapters/mysql2/reserved_word_test.rb
  8. +7 −7 activerecord/test/cases/associations/belongs_to_associations_test.rb
  9. +14 −14 activerecord/test/cases/associations/cascaded_eager_loading_test.rb
  10. +2 −2 activerecord/test/cases/associations/eager_load_nested_include_test.rb
  11. +7 −7 activerecord/test/cases/associations/eager_singularization_test.rb
  12. +114 −114 activerecord/test/cases/associations/eager_test.rb
  13. +1 −1 activerecord/test/cases/associations/extension_test.rb
  14. +5 −5 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
  15. +40 −40 activerecord/test/cases/associations/has_many_associations_test.rb
  16. +4 −4 activerecord/test/cases/associations/has_one_associations_test.rb
  17. +13 −13 activerecord/test/cases/associations/has_one_through_associations_test.rb
  18. +4 −4 activerecord/test/cases/associations/inner_join_association_test.rb
  19. +9 −9 activerecord/test/cases/associations/inverse_associations_test.rb
  20. +27 −27 activerecord/test/cases/associations/join_model_test.rb
  21. +5 −5 activerecord/test/cases/associations_test.rb
  22. +4 −4 activerecord/test/cases/attribute_methods_test.rb
  23. +1 −1 activerecord/test/cases/autosave_association_test.rb
  24. +24 −24 activerecord/test/cases/base_test.rb
  25. +13 −13 activerecord/test/cases/calculations_test.rb
  26. +1 −1 activerecord/test/cases/custom_locking_test.rb
  27. +12 −12 activerecord/test/cases/deprecated_dynamic_methods_test.rb
  28. +69 −69 activerecord/test/cases/finder_test.rb
  29. +12 −12 activerecord/test/cases/inheritance_test.rb
  30. +4 −4 activerecord/test/cases/modules_test.rb
  31. +6 −6 activerecord/test/cases/named_scope_test.rb
  32. +3 −3 activerecord/test/cases/persistence_test.rb
  33. +4 −4 activerecord/test/cases/readonly_test.rb
  34. +10 −10 activerecord/test/cases/relation_scoping_test.rb
  35. +58 −58 activerecord/test/cases/relations_test.rb
  36. +1 −1 activerecord/test/cases/validations/uniqueness_validation_test.rb
  37. +1 −1 activerecord/test/models/bulb.rb
  38. +3 −3 activerecord/test/models/comment.rb
  39. +4 −4 activerecord/test/models/developer.rb
  40. +4 −3 activerecord/test/models/post.rb
  41. +1 −1 activerecord/test/models/project.rb
  42. +3 −3 activerecord/test/models/topic.rb
@@ -118,7 +118,7 @@ def klass
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
# through association's scope)
def target_scope
- klass.scoped
+ klass.all
end
# Loads the \target if needed and returns it.
@@ -896,13 +896,9 @@ def scoping
end
def spawn
- scoped
- end
-
- def scoped(options = nil)
association = @association
- super.extending! do
+ @association.scoped.extending! do
define_method(:proxy_association) { association }
end
end
@@ -15,7 +15,7 @@ def target_scope
scope = super
chain[1..-1].each do |reflection|
scope = scope.merge(
- reflection.klass.scoped.with_default_scope.
+ reflection.klass.all.with_default_scope.
except(:select, :create_with, :includes, :preload, :joins, :eager_load)
)
end
@@ -464,7 +464,7 @@ def lock!(locks = true)
# end
#
def none
- scoped.extending(NullRelation)
+ extending(NullRelation)
end
# Sets readonly attributes for the returned relation. If value is
@@ -34,10 +34,6 @@ def all
end
end
- def scoped(options = nil)
- options ? all.merge!(options) : all
- end
-
##
# Collects attributes from scopes that should be applied when creating
# an AR instance for the particular class this is called on.
@@ -186,7 +182,7 @@ def scope(name, body, &block)
singleton_class.send(:define_method, name) do |*args|
options = body.respond_to?(:call) ? unscoped { body.call(*args) } : body
- relation = scoped.merge(options)
+ relation = all.merge(options)
extension ? relation.extending(extension) : relation
end
@@ -130,7 +130,7 @@ def test_calculations_work_with_reserved_words
end
def test_associations_work_with_reserved_words
- assert_nothing_raised { Select.scoped(:includes => [:groups]).to_a }
+ assert_nothing_raised { Select.all.merge!(:includes => [:groups]).to_a }
end
#the following functions were added to DRY test cases
@@ -130,7 +130,7 @@ def test_calculations_work_with_reserved_words
end
def test_associations_work_with_reserved_words
- assert_nothing_raised { Select.scoped(:includes => [:groups]).to_a }
+ assert_nothing_raised { Select.all.merge!(:includes => [:groups]).to_a }
end
#the following functions were added to DRY test cases
@@ -73,14 +73,14 @@ def test_natural_assignment_with_primary_key
def test_eager_loading_with_primary_key
Firm.create("name" => "Apple")
Client.create("name" => "Citibank", :firm_name => "Apple")
- citibank_result = Client.scoped(:where => {:name => "Citibank"}, :includes => :firm_with_primary_key).first
+ citibank_result = Client.all.merge!(:where => {:name => "Citibank"}, :includes => :firm_with_primary_key).first
assert citibank_result.association_cache.key?(:firm_with_primary_key)
end
def test_eager_loading_with_primary_key_as_symbol
Firm.create("name" => "Apple")
Client.create("name" => "Citibank", :firm_name => "Apple")
- citibank_result = Client.scoped(:where => {:name => "Citibank"}, :includes => :firm_with_primary_key_symbols).first
+ citibank_result = Client.all.merge!(:where => {:name => "Citibank"}, :includes => :firm_with_primary_key_symbols).first
assert citibank_result.association_cache.key?(:firm_with_primary_key_symbols)
end
@@ -182,7 +182,7 @@ def test_with_polymorphic_and_condition
def test_with_select
assert_equal 1, Company.find(2).firm_with_select.attributes.size
- assert_equal 1, Company.scoped(:includes => :firm_with_select ).find(2).firm_with_select.attributes.size
+ assert_equal 1, Company.all.merge!(:includes => :firm_with_select ).find(2).firm_with_select.attributes.size
end
def test_belongs_to_counter
@@ -334,7 +334,7 @@ def test_assignment_before_child_saved_with_primary_key
def test_new_record_with_foreign_key_but_no_object
c = Client.new("firm_id" => 1)
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
- assert_equal Firm.scoped(:order => "id").first, c.firm_with_basic_id
+ assert_equal Firm.all.merge!(:order => "id").first, c.firm_with_basic_id
end
def test_setting_foreign_key_after_nil_target_loaded
@@ -396,7 +396,7 @@ def test_custom_counter_cache
def test_association_assignment_sticks
post = Post.first
- author1, author2 = Author.scoped(:limit => 2).to_a
+ author1, author2 = Author.all.merge!(:limit => 2).to_a
assert_not_nil author1
assert_not_nil author2
@@ -498,14 +498,14 @@ def test_save_of_record_with_loaded_belongs_to
assert_nothing_raised do
Account.find(@account.id).save!
- Account.scoped(:includes => :firm).find(@account.id).save!
+ Account.all.merge!(:includes => :firm).find(@account.id).save!
end
@account.firm.delete
assert_nothing_raised do
Account.find(@account.id).save!
- Account.scoped(:includes => :firm).find(@account.id).save!
+ Account.all.merge!(:includes => :firm).find(@account.id).save!
end
end
@@ -16,15 +16,15 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
:categorizations, :people, :categories, :edges, :vertices
def test_eager_association_loading_with_cascaded_two_levels
- authors = Author.scoped(:includes=>{:posts=>:comments}, :order=>"authors.id").to_a
+ authors = Author.all.merge!(:includes=>{:posts=>:comments}, :order=>"authors.id").to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
end
def test_eager_association_loading_with_cascaded_two_levels_and_one_level
- authors = Author.scoped(:includes=>[{:posts=>:comments}, :categorizations], :order=>"authors.id").to_a
+ authors = Author.all.merge!(:includes=>[{:posts=>:comments}, :categorizations], :order=>"authors.id").to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
@@ -84,37 +84,37 @@ def test_eager_association_loading_with_join_for_count
end
def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
- authors = Author.scoped(:includes=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id").to_a
+ authors = Author.all.merge!(:includes=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id").to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
end
def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
- authors = Author.scoped(:includes=>{:posts=>[:comments, :author]}, :order=>"authors.id").to_a
+ authors = Author.all.merge!(:includes=>{:posts=>[:comments, :author]}, :order=>"authors.id").to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal authors(:david).name, authors[0].name
assert_equal [authors(:david).name], authors[0].posts.collect{|post| post.author.name}.uniq
end
def test_eager_association_loading_with_cascaded_two_levels_with_condition
- authors = Author.scoped(:includes=>{:posts=>:comments}, :where=>"authors.id=1", :order=>"authors.id").to_a
+ authors = Author.all.merge!(:includes=>{:posts=>:comments}, :where=>"authors.id=1", :order=>"authors.id").to_a
assert_equal 1, authors.size
assert_equal 5, authors[0].posts.size
end
def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
- firms = Firm.scoped(:includes=>{:account=>{:firm=>:account}}, :order=>"companies.id").to_a
+ firms = Firm.all.merge!(:includes=>{:account=>{:firm=>:account}}, :order=>"companies.id").to_a
assert_equal 2, firms.size
assert_equal firms.first.account, firms.first.account.firm.account
assert_equal companies(:first_firm).account, assert_no_queries { firms.first.account.firm.account }
assert_equal companies(:first_firm).account.firm.account, assert_no_queries { firms.first.account.firm.account }
end
def test_eager_association_loading_with_has_many_sti
- topics = Topic.scoped(:includes => :replies, :order => 'topics.id').to_a
+ topics = Topic.all.merge!(:includes => :replies, :order => 'topics.id').to_a
first, second, = topics(:first).replies.size, topics(:second).replies.size
assert_no_queries do
assert_equal first, topics[0].replies.size
@@ -127,22 +127,22 @@ def test_eager_association_loading_with_has_many_sti_and_subclasses
silly.parent_id = 1
assert silly.save
- topics = Topic.scoped(:includes => :replies, :order => ['topics.id', 'replies_topics.id']).to_a
+ topics = Topic.all.merge!(:includes => :replies, :order => ['topics.id', 'replies_topics.id']).to_a
assert_no_queries do
assert_equal 2, topics[0].replies.size
assert_equal 0, topics[1].replies.size
end
end
def test_eager_association_loading_with_belongs_to_sti
- replies = Reply.scoped(:includes => :topic, :order => 'topics.id').to_a
+ replies = Reply.all.merge!(:includes => :topic, :order => 'topics.id').to_a
assert replies.include?(topics(:second))
assert !replies.include?(topics(:first))
assert_equal topics(:first), assert_no_queries { replies.first.topic }
end
def test_eager_association_loading_with_multiple_stis_and_order
- author = Author.scoped(:includes => { :posts => [ :special_comments , :very_special_comment ] }, :order => ['authors.name', 'comments.body', 'very_special_comments_posts.body'], :where => 'posts.id = 4').first
+ author = Author.all.merge!(:includes => { :posts => [ :special_comments , :very_special_comment ] }, :order => ['authors.name', 'comments.body', 'very_special_comments_posts.body'], :where => 'posts.id = 4').first
assert_equal authors(:david), author
assert_no_queries do
author.posts.first.special_comments
@@ -151,7 +151,7 @@ def test_eager_association_loading_with_multiple_stis_and_order
end
def test_eager_association_loading_of_stis_with_multiple_references
- authors = Author.scoped(:includes => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :where => 'posts.id = 4').to_a
+ authors = Author.all.merge!(:includes => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :where => 'posts.id = 4').to_a
assert_equal [authors(:david)], authors
assert_no_queries do
authors.first.posts.first.special_comments.first.post.special_comments
@@ -160,20 +160,20 @@ def test_eager_association_loading_of_stis_with_multiple_references
end
def test_eager_association_loading_where_first_level_returns_nil
- authors = Author.scoped(:includes => {:post_about_thinking => :comments}, :order => 'authors.id DESC').to_a
+ authors = Author.all.merge!(:includes => {:post_about_thinking => :comments}, :order => 'authors.id DESC').to_a
assert_equal [authors(:bob), authors(:mary), authors(:david)], authors
assert_no_queries do
authors[2].post_about_thinking.comments.first
end
end
def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
- source = Vertex.scoped(:includes=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id').first
+ source = Vertex.all.merge!(:includes=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id').first
assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first }
end
def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
- sink = Vertex.scoped(:includes=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC').first
+ sink = Vertex.all.merge!(:includes=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC').first
assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first }
end
end
@@ -92,7 +92,7 @@ def generate_test_object_graphs
end
def test_include_query
- res = ShapeExpression.scoped(:includes => [ :shape, { :paint => :non_poly } ]).to_a
+ res = ShapeExpression.all.merge!(:includes => [ :shape, { :paint => :non_poly } ]).to_a
assert_equal NUM_SHAPE_EXPRESSIONS, res.size
assert_queries(0) do
res.each do |se|
@@ -122,7 +122,7 @@ def test_missing_data_in_a_nested_include_should_not_cause_errors_when_construct
assert_nothing_raised do
# @davey_mcdave doesn't have any author_favorites
includes = {:posts => :comments, :categorizations => :category, :author_favorites => :favorite_author }
- Author.scoped(:includes => includes, :where => {:authors => {:name => @davey_mcdave.name}}, :order => 'categories.name').to_a
+ Author.all.merge!(:includes => includes, :where => {:authors => {:name => @davey_mcdave.name}}, :order => 'categories.name').to_a
end
end
end
@@ -103,43 +103,43 @@ def teardown
def test_eager_no_extra_singularization_belongs_to
return unless @have_tables
assert_nothing_raised do
- Virus.scoped(:includes => :octopus).to_a
+ Virus.all.merge!(:includes => :octopus).to_a
end
end
def test_eager_no_extra_singularization_has_one
return unless @have_tables
assert_nothing_raised do
- Octopus.scoped(:includes => :virus).to_a
+ Octopus.all.merge!(:includes => :virus).to_a
end
end
def test_eager_no_extra_singularization_has_many
return unless @have_tables
assert_nothing_raised do
- Bus.scoped(:includes => :passes).to_a
+ Bus.all.merge!(:includes => :passes).to_a
end
end
def test_eager_no_extra_singularization_has_and_belongs_to_many
return unless @have_tables
assert_nothing_raised do
- Crisis.scoped(:includes => :messes).to_a
- Mess.scoped(:includes => :crises).to_a
+ Crisis.all.merge!(:includes => :messes).to_a
+ Mess.all.merge!(:includes => :crises).to_a
end
end
def test_eager_no_extra_singularization_has_many_through_belongs_to
return unless @have_tables
assert_nothing_raised do
- Crisis.scoped(:includes => :successes).to_a
+ Crisis.all.merge!(:includes => :successes).to_a
end
end
def test_eager_no_extra_singularization_has_many_through_has_many
return unless @have_tables
assert_nothing_raised do
- Crisis.scoped(:includes => :compresses).to_a
+ Crisis.all.merge!(:includes => :compresses).to_a
end
end
end
Oops, something went wrong.

7 comments on commit b658cf1

@ jonleighton I understand the rationale of replacing #scoped for #all, but it would be nice if calling #scoped still worked in Rails 4.0 but with a deprecation warning to alert developers when it will be removed completely. As of Rails 3.2, it's a frequently-used, public API.

Member

steveklabnik replied Oct 12, 2012

@ryansobol if you put a space between your @ and your jonleighton, it doesn't do a mention. ;)

Member

steveklabnik replied Oct 12, 2012

That said, @jonleighton, @ryansobol has a point. Does this violate our "deprecate in 4.0, remove in 4.1 policy"?

Member

jonleighton replied Oct 12, 2012

@ryansobol "The code is moved to active_record_deprecated_finders." It's deprecated there.

Member

jonleighton replied Oct 12, 2012

although, that gem is now called activerecord-deprecated_finders

Member

steveklabnik replied Oct 12, 2012

Nice catch, thanks.

@jonleighton I didn't realize "active_record_deprecated_finders" is a gem. Thanks for the clarification. 😄

Please sign in to comment.