Skip to content

Commit b658cf1

Browse files
committed
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.
1 parent 6a81ccd commit b658cf1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+497
-504
lines changed

activerecord/lib/active_record/associations/association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def klass
118118
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
119119
# through association's scope)
120120
def target_scope
121-
klass.scoped
121+
klass.all
122122
end
123123

124124
# Loads the \target if needed and returns it.

activerecord/lib/active_record/associations/collection_proxy.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,13 +896,9 @@ def scoping
896896
end
897897

898898
def spawn
899-
scoped
900-
end
901-
902-
def scoped(options = nil)
903899
association = @association
904900

905-
super.extending! do
901+
@association.scoped.extending! do
906902
define_method(:proxy_association) { association }
907903
end
908904
end

activerecord/lib/active_record/associations/through_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def target_scope
1515
scope = super
1616
chain[1..-1].each do |reflection|
1717
scope = scope.merge(
18-
reflection.klass.scoped.with_default_scope.
18+
reflection.klass.all.with_default_scope.
1919
except(:select, :create_with, :includes, :preload, :joins, :eager_load)
2020
)
2121
end

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ def lock!(locks = true)
464464
# end
465465
#
466466
def none
467-
scoped.extending(NullRelation)
467+
extending(NullRelation)
468468
end
469469

470470
# Sets readonly attributes for the returned relation. If value is

activerecord/lib/active_record/scoping/named.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ def all
3434
end
3535
end
3636

37-
def scoped(options = nil)
38-
options ? all.merge!(options) : all
39-
end
40-
4137
##
4238
# Collects attributes from scopes that should be applied when creating
4339
# an AR instance for the particular class this is called on.
@@ -186,7 +182,7 @@ def scope(name, body, &block)
186182

187183
singleton_class.send(:define_method, name) do |*args|
188184
options = body.respond_to?(:call) ? unscoped { body.call(*args) } : body
189-
relation = scoped.merge(options)
185+
relation = all.merge(options)
190186

191187
extension ? relation.extending(extension) : relation
192188
end

activerecord/test/cases/adapters/mysql/reserved_word_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def test_calculations_work_with_reserved_words
130130
end
131131

132132
def test_associations_work_with_reserved_words
133-
assert_nothing_raised { Select.scoped(:includes => [:groups]).to_a }
133+
assert_nothing_raised { Select.all.merge!(:includes => [:groups]).to_a }
134134
end
135135

136136
#the following functions were added to DRY test cases

activerecord/test/cases/adapters/mysql2/reserved_word_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def test_calculations_work_with_reserved_words
130130
end
131131

132132
def test_associations_work_with_reserved_words
133-
assert_nothing_raised { Select.scoped(:includes => [:groups]).to_a }
133+
assert_nothing_raised { Select.all.merge!(:includes => [:groups]).to_a }
134134
end
135135

136136
#the following functions were added to DRY test cases

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ def test_natural_assignment_with_primary_key
7373
def test_eager_loading_with_primary_key
7474
Firm.create("name" => "Apple")
7575
Client.create("name" => "Citibank", :firm_name => "Apple")
76-
citibank_result = Client.scoped(:where => {:name => "Citibank"}, :includes => :firm_with_primary_key).first
76+
citibank_result = Client.all.merge!(:where => {:name => "Citibank"}, :includes => :firm_with_primary_key).first
7777
assert citibank_result.association_cache.key?(:firm_with_primary_key)
7878
end
7979

8080
def test_eager_loading_with_primary_key_as_symbol
8181
Firm.create("name" => "Apple")
8282
Client.create("name" => "Citibank", :firm_name => "Apple")
83-
citibank_result = Client.scoped(:where => {:name => "Citibank"}, :includes => :firm_with_primary_key_symbols).first
83+
citibank_result = Client.all.merge!(:where => {:name => "Citibank"}, :includes => :firm_with_primary_key_symbols).first
8484
assert citibank_result.association_cache.key?(:firm_with_primary_key_symbols)
8585
end
8686

@@ -182,7 +182,7 @@ def test_with_polymorphic_and_condition
182182

183183
def test_with_select
184184
assert_equal 1, Company.find(2).firm_with_select.attributes.size
185-
assert_equal 1, Company.scoped(:includes => :firm_with_select ).find(2).firm_with_select.attributes.size
185+
assert_equal 1, Company.all.merge!(:includes => :firm_with_select ).find(2).firm_with_select.attributes.size
186186
end
187187

188188
def test_belongs_to_counter
@@ -334,7 +334,7 @@ def test_assignment_before_child_saved_with_primary_key
334334
def test_new_record_with_foreign_key_but_no_object
335335
c = Client.new("firm_id" => 1)
336336
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
337-
assert_equal Firm.scoped(:order => "id").first, c.firm_with_basic_id
337+
assert_equal Firm.all.merge!(:order => "id").first, c.firm_with_basic_id
338338
end
339339

340340
def test_setting_foreign_key_after_nil_target_loaded
@@ -396,7 +396,7 @@ def test_custom_counter_cache
396396
def test_association_assignment_sticks
397397
post = Post.first
398398

399-
author1, author2 = Author.scoped(:limit => 2).to_a
399+
author1, author2 = Author.all.merge!(:limit => 2).to_a
400400
assert_not_nil author1
401401
assert_not_nil author2
402402

@@ -498,14 +498,14 @@ def test_save_of_record_with_loaded_belongs_to
498498

499499
assert_nothing_raised do
500500
Account.find(@account.id).save!
501-
Account.scoped(:includes => :firm).find(@account.id).save!
501+
Account.all.merge!(:includes => :firm).find(@account.id).save!
502502
end
503503

504504
@account.firm.delete
505505

506506
assert_nothing_raised do
507507
Account.find(@account.id).save!
508-
Account.scoped(:includes => :firm).find(@account.id).save!
508+
Account.all.merge!(:includes => :firm).find(@account.id).save!
509509
end
510510
end
511511

activerecord/test/cases/associations/cascaded_eager_loading_test.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
1616
:categorizations, :people, :categories, :edges, :vertices
1717

1818
def test_eager_association_loading_with_cascaded_two_levels
19-
authors = Author.scoped(:includes=>{:posts=>:comments}, :order=>"authors.id").to_a
19+
authors = Author.all.merge!(:includes=>{:posts=>:comments}, :order=>"authors.id").to_a
2020
assert_equal 3, authors.size
2121
assert_equal 5, authors[0].posts.size
2222
assert_equal 3, authors[1].posts.size
2323
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
2424
end
2525

2626
def test_eager_association_loading_with_cascaded_two_levels_and_one_level
27-
authors = Author.scoped(:includes=>[{:posts=>:comments}, :categorizations], :order=>"authors.id").to_a
27+
authors = Author.all.merge!(:includes=>[{:posts=>:comments}, :categorizations], :order=>"authors.id").to_a
2828
assert_equal 3, authors.size
2929
assert_equal 5, authors[0].posts.size
3030
assert_equal 3, authors[1].posts.size
@@ -84,37 +84,37 @@ def test_eager_association_loading_with_join_for_count
8484
end
8585

8686
def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
87-
authors = Author.scoped(:includes=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id").to_a
87+
authors = Author.all.merge!(:includes=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id").to_a
8888
assert_equal 3, authors.size
8989
assert_equal 5, authors[0].posts.size
9090
assert_equal 3, authors[1].posts.size
9191
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
9292
end
9393

9494
def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
95-
authors = Author.scoped(:includes=>{:posts=>[:comments, :author]}, :order=>"authors.id").to_a
95+
authors = Author.all.merge!(:includes=>{:posts=>[:comments, :author]}, :order=>"authors.id").to_a
9696
assert_equal 3, authors.size
9797
assert_equal 5, authors[0].posts.size
9898
assert_equal authors(:david).name, authors[0].name
9999
assert_equal [authors(:david).name], authors[0].posts.collect{|post| post.author.name}.uniq
100100
end
101101

102102
def test_eager_association_loading_with_cascaded_two_levels_with_condition
103-
authors = Author.scoped(:includes=>{:posts=>:comments}, :where=>"authors.id=1", :order=>"authors.id").to_a
103+
authors = Author.all.merge!(:includes=>{:posts=>:comments}, :where=>"authors.id=1", :order=>"authors.id").to_a
104104
assert_equal 1, authors.size
105105
assert_equal 5, authors[0].posts.size
106106
end
107107

108108
def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
109-
firms = Firm.scoped(:includes=>{:account=>{:firm=>:account}}, :order=>"companies.id").to_a
109+
firms = Firm.all.merge!(:includes=>{:account=>{:firm=>:account}}, :order=>"companies.id").to_a
110110
assert_equal 2, firms.size
111111
assert_equal firms.first.account, firms.first.account.firm.account
112112
assert_equal companies(:first_firm).account, assert_no_queries { firms.first.account.firm.account }
113113
assert_equal companies(:first_firm).account.firm.account, assert_no_queries { firms.first.account.firm.account }
114114
end
115115

116116
def test_eager_association_loading_with_has_many_sti
117-
topics = Topic.scoped(:includes => :replies, :order => 'topics.id').to_a
117+
topics = Topic.all.merge!(:includes => :replies, :order => 'topics.id').to_a
118118
first, second, = topics(:first).replies.size, topics(:second).replies.size
119119
assert_no_queries do
120120
assert_equal first, topics[0].replies.size
@@ -127,22 +127,22 @@ def test_eager_association_loading_with_has_many_sti_and_subclasses
127127
silly.parent_id = 1
128128
assert silly.save
129129

130-
topics = Topic.scoped(:includes => :replies, :order => ['topics.id', 'replies_topics.id']).to_a
130+
topics = Topic.all.merge!(:includes => :replies, :order => ['topics.id', 'replies_topics.id']).to_a
131131
assert_no_queries do
132132
assert_equal 2, topics[0].replies.size
133133
assert_equal 0, topics[1].replies.size
134134
end
135135
end
136136

137137
def test_eager_association_loading_with_belongs_to_sti
138-
replies = Reply.scoped(:includes => :topic, :order => 'topics.id').to_a
138+
replies = Reply.all.merge!(:includes => :topic, :order => 'topics.id').to_a
139139
assert replies.include?(topics(:second))
140140
assert !replies.include?(topics(:first))
141141
assert_equal topics(:first), assert_no_queries { replies.first.topic }
142142
end
143143

144144
def test_eager_association_loading_with_multiple_stis_and_order
145-
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
145+
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
146146
assert_equal authors(:david), author
147147
assert_no_queries do
148148
author.posts.first.special_comments
@@ -151,7 +151,7 @@ def test_eager_association_loading_with_multiple_stis_and_order
151151
end
152152

153153
def test_eager_association_loading_of_stis_with_multiple_references
154-
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
154+
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
155155
assert_equal [authors(:david)], authors
156156
assert_no_queries do
157157
authors.first.posts.first.special_comments.first.post.special_comments
@@ -160,20 +160,20 @@ def test_eager_association_loading_of_stis_with_multiple_references
160160
end
161161

162162
def test_eager_association_loading_where_first_level_returns_nil
163-
authors = Author.scoped(:includes => {:post_about_thinking => :comments}, :order => 'authors.id DESC').to_a
163+
authors = Author.all.merge!(:includes => {:post_about_thinking => :comments}, :order => 'authors.id DESC').to_a
164164
assert_equal [authors(:bob), authors(:mary), authors(:david)], authors
165165
assert_no_queries do
166166
authors[2].post_about_thinking.comments.first
167167
end
168168
end
169169

170170
def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
171-
source = Vertex.scoped(:includes=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id').first
171+
source = Vertex.all.merge!(:includes=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id').first
172172
assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first }
173173
end
174174

175175
def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
176-
sink = Vertex.scoped(:includes=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC').first
176+
sink = Vertex.all.merge!(:includes=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC').first
177177
assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first }
178178
end
179179
end

activerecord/test/cases/associations/eager_load_nested_include_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def generate_test_object_graphs
9292
end
9393

9494
def test_include_query
95-
res = ShapeExpression.scoped(:includes => [ :shape, { :paint => :non_poly } ]).to_a
95+
res = ShapeExpression.all.merge!(:includes => [ :shape, { :paint => :non_poly } ]).to_a
9696
assert_equal NUM_SHAPE_EXPRESSIONS, res.size
9797
assert_queries(0) do
9898
res.each do |se|
@@ -122,7 +122,7 @@ def test_missing_data_in_a_nested_include_should_not_cause_errors_when_construct
122122
assert_nothing_raised do
123123
# @davey_mcdave doesn't have any author_favorites
124124
includes = {:posts => :comments, :categorizations => :category, :author_favorites => :favorite_author }
125-
Author.scoped(:includes => includes, :where => {:authors => {:name => @davey_mcdave.name}}, :order => 'categories.name').to_a
125+
Author.all.merge!(:includes => includes, :where => {:authors => {:name => @davey_mcdave.name}}, :order => 'categories.name').to_a
126126
end
127127
end
128128
end

0 commit comments

Comments
 (0)