public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Make NamedScope#size behave identically to AssociationCollection#size. [#933 
state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
tomstuart (author)
Fri Aug 29 07:11:25 -0700 2008
lifo (committer)
Fri Aug 29 14:18:49 -0700 2008
commit  7f179f8540ab92dbd9d3e650b465de5b694d93d4
tree    913eb8d2984c33f770c315c533cb339e7058d1d9
parent  c0361344d95162cd8573592d60e52986eaca0377
...
101
102
103
104
 
105
106
107
...
136
137
138
 
 
 
 
139
140
141
...
101
102
103
 
104
105
106
107
...
136
137
138
139
140
141
142
143
144
145
0
@@ -101,7 +101,7 @@ module ActiveRecord
0
     
0
     class Scope
0
       attr_reader :proxy_scope, :proxy_options
0
-      NON_DELEGATE_METHODS = %w(nil? send object_id class extend find count sum average maximum minimum paginate first last empty? any? respond_to?).to_set
0
+      NON_DELEGATE_METHODS = %w(nil? send object_id class extend find size count sum average maximum minimum paginate first last empty? any? respond_to?).to_set
0
       [].methods.each do |m|
0
         unless m =~ /^__/ || NON_DELEGATE_METHODS.include?(m.to_s)
0
           delegate m, :to => :proxy_found
0
@@ -136,6 +136,10 @@ module ActiveRecord
0
         end
0
       end
0
 
0
+      def size
0
+        @found ? @found.length : count
0
+      end
0
+
0
       def empty?
0
         @found ? @found.empty? : count.zero?
0
       end
...
256
257
258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
0
@@ -256,4 +256,19 @@ class NamedScopeTest < ActiveRecord::TestCase
0
   def test_should_use_where_in_query_for_named_scope
0
     assert_equal Developer.find_all_by_name('Jamis'), Developer.find_all_by_id(Developer.jamises)
0
   end
0
+
0
+  def test_size_should_use_count_when_results_are_not_loaded
0
+    topics = Topic.base
0
+    assert_queries(1) do
0
+      assert_sql(/COUNT/i) { topics.size }
0
+    end
0
+  end
0
+
0
+  def test_size_should_use_length_when_results_are_loaded
0
+    topics = Topic.base
0
+    topics.reload # force load
0
+    assert_no_queries do
0
+      topics.size # use loaded (no query)
0
+    end
0
+  end
0
 end

Comments