public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Ensure NamedScope#any? uses COUNT query wherever possible. [#680 
state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
DefV (author)
Wed Jul 23 03:50:16 -0700 2008
lifo (committer)
Wed Jul 23 04:02:17 -0700 2008
commit  93e10f9911fb2a096681ee0a0bc82487a9a06c44
tree    92bcb41503ea63aeabcd0354da430630cbf3a683
parent  2681685450631238511cfc3c2f0fa044c1f8033a
...
103
104
105
106
 
107
108
109
...
140
141
142
 
 
 
 
 
 
 
 
143
144
145
...
103
104
105
 
106
107
108
109
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
0
@@ -103,7 +103,7 @@ module ActiveRecord
0
       attr_reader :proxy_scope, :proxy_options
0
 
0
       [].methods.each do |m|
0
- unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|find|count|sum|average|maximum|minimum|paginate|first|last|empty?)/
0
+ unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|find|count|sum|average|maximum|minimum|paginate|first|last|empty?|any?)/
0
           delegate m, :to => :proxy_found
0
         end
0
       end
0
@@ -140,6 +140,14 @@ module ActiveRecord
0
         @found ? @found.empty? : count.zero?
0
       end
0
 
0
+ def any?
0
+ if block_given?
0
+ proxy_found.any? { |*block_args| yield(*block_args) }
0
+ else
0
+ !empty?
0
+ end
0
+ end
0
+
0
       protected
0
       def proxy_found
0
         @found || load_found
...
184
185
186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
188
189
...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
0
@@ -184,6 +184,28 @@ class NamedScopeTest < ActiveRecord::TestCase
0
     end
0
   end
0
 
0
+ def test_any_should_not_load_results
0
+ topics = Topic.base
0
+ assert_queries(1) do
0
+ topics.expects(:empty?).returns(true)
0
+ assert !topics.any?
0
+ end
0
+ end
0
+
0
+ def test_any_should_call_proxy_found_if_using_a_block
0
+ topics = Topic.base
0
+ assert_queries(1) do
0
+ topics.expects(:empty?).never
0
+ topics.any? { true }
0
+ end
0
+ end
0
+
0
+ def test_any_should_not_fire_query_if_named_scope_loaded
0
+ topics = Topic.base
0
+ topics.collect # force load
0
+ assert_no_queries { assert topics.any? }
0
+ end
0
+
0
   def test_should_build_with_proxy_options
0
     topic = Topic.approved.build({})
0
     assert topic.approved

Comments

  • nicksieger Thu Aug 07 18:48:15 -0700 2008

    This commit broke an AR test that is still failing in edge as of this comment.

    See http://gist.github.com/4339

  • ryanb Thu Aug 07 19:11:56 -0700 2008

    I ran into this before too. You need to update mocha (gem install mocha). See this thread for details.

  • ryanb Thu Aug 07 19:44:56 -0700 2008

    I have added a ticket about this issue along with a patch for a fix.

  • nicksieger Thu Aug 07 20:00:40 -0700 2008

    Thanks ryanb!