public
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/NZKoz/koz-rails.git
Dynamic finders on association collections respect association :limit. 
Closes #10227 [Jack Danger Canty]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8178 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
bitsweat (author)
Tue Nov 20 23:32:44 -0800 2007
commit  6d391229906b5430d58aec9035ef08d028fdfe99
tree    f75574e57ea544ba68e9282a680367a16ec6e4ba
parent  a66b850b3f4015c9e37a02e74832f3dcc027a53d
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Dynamic finders on association collections respect association :order and :limit. #10211, #10227 [Patrick Joyce, Rick Olson, Jack Danger Canty]
0
+
0
 * Add 'foxy' support for fixtures of polymorphic associations. #10183 [jbarnette, David Lowenfels]
0
 
0
 * validates_inclusion_of and validates_exclusion_of allow formatted :message strings. #8132 [devrieda, Mike Naberezny]
...
133
134
135
136
 
 
 
 
 
137
138
139
...
133
134
135
 
136
137
138
139
140
141
142
143
0
@@ -133,7 +133,11 @@ module ActiveRecord
0
         end
0
 
0
         def construct_scope
0
- { :find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false, :order => @reflection.options[:order] } }
0
+ { :find => { :conditions => @finder_sql,
0
+ :joins => @join_sql,
0
+ :readonly => false,
0
+ :order => @reflection.options[:order],
0
+ :limit => @reflection.options[:limit] } }
0
         end
0
 
0
         # Join tables with additional columns on top of the two foreign keys must be considered ambiguous unless a select
...
167
168
169
170
 
171
172
173
...
167
168
169
 
170
171
172
173
0
@@ -167,7 +167,7 @@ module ActiveRecord
0
         def construct_scope
0
           create_scoping = {}
0
           set_belongs_to_association_for(create_scoping)
0
- { :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order] }, :create => create_scoping }
0
+ { :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] }, :create => create_scoping }
0
         end
0
     end
0
   end
...
236
237
238
239
 
 
240
241
242
...
236
237
238
 
239
240
241
242
243
0
@@ -236,7 +236,8 @@ module ActiveRecord
0
                          :conditions => construct_conditions,
0
                          :joins => construct_joins,
0
                          :select => construct_select,
0
- :order => @reflection.options[:order] } }
0
+ :order => @reflection.options[:order],
0
+ :limit => @reflection.options[:limit] } }
0
         end
0
 
0
         def construct_sql
...
483
484
485
 
 
 
 
 
 
 
 
 
 
486
487
488
...
1120
1121
1122
 
 
 
 
 
 
 
 
 
 
1123
1124
1125
...
1864
1865
1866
 
 
 
 
 
 
 
 
 
 
1867
1868
1869
...
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
...
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
...
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
0
@@ -483,6 +483,16 @@ class HasManyAssociationsTest < Test::Unit::TestCase
0
     assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client', :order => 'id')
0
   end
0
 
0
+ def test_dynamic_find_all_should_respect_association_limit
0
+ assert_equal 1, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'").length
0
+ assert_equal 1, companies(:first_firm).limited_clients.find_all_by_type('Client').length
0
+ end
0
+
0
+ def test_dynamic_find_all_limit_should_override_association_limit
0
+ assert_equal 2, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'", :limit => 9_000).length
0
+ assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length
0
+ end
0
+
0
   def test_triple_equality
0
     assert !(Array === Firm.find(:first).clients)
0
     assert Firm.find(:first).clients === Array
0
@@ -1120,6 +1130,16 @@ class HasManyAssociationsTest < Test::Unit::TestCase
0
     assert_equal [Comment.find(3), Comment.find(6), Comment.find(7), Comment.find(10)], authors(:david).comments_desc.find_all_by_type('SpecialComment', :order => 'comments.id')
0
   end
0
 
0
+ def test_dynamic_find_all_should_respect_association_limit_for_through
0
+ assert_equal 1, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'").length
0
+ assert_equal 1, authors(:david).limited_comments.find_all_by_type('SpecialComment').length
0
+ end
0
+
0
+ def test_dynamic_find_all_order_should_override_association_limit_for_through
0
+ assert_equal 4, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'", :limit => 9_000).length
0
+ assert_equal 4, authors(:david).limited_comments.find_all_by_type('SpecialComment', :limit => 9_000).length
0
+ end
0
+
0
 end
0
 
0
 class BelongsToAssociationsTest < Test::Unit::TestCase
0
@@ -1864,6 +1884,16 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
0
     assert_equal [low_id_jamis, middle_id_jamis, high_id_jamis], projects(:active_record).developers.find_all_by_name('Jamis', :order => 'id')
0
   end
0
 
0
+ def test_dynamic_find_all_should_respect_association_limit
0
+ assert_equal 1, projects(:active_record).limited_developers.find(:all, :conditions => "name = 'Jamis'").length
0
+ assert_equal 1, projects(:active_record).limited_developers.find_all_by_name('Jamis').length
0
+ end
0
+
0
+ def test_dynamic_find_all_order_should_override_association_limit
0
+ assert_equal 2, projects(:active_record).limited_developers.find(:all, :conditions => "name = 'Jamis'", :limit => 9_000).length
0
+ assert_equal 2, projects(:active_record).limited_developers.find_all_by_name('Jamis', :limit => 9_000).length
0
+ end
0
+
0
   def test_new_with_values_in_collection
0
     jamis = DeveloperForProjectWithAfterCreateHook.find_by_name('Jamis')
0
     david = DeveloperForProjectWithAfterCreateHook.find_by_name('David')
...
16
17
18
 
19
20
21
...
16
17
18
19
20
21
22
0
@@ -16,6 +16,7 @@ class Author < ActiveRecord::Base
0
   end
0
   has_many :comments, :through => :posts
0
   has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC'
0
+ has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1
0
   has_many :funky_comments, :through => :posts, :source => :comments
0
 
0
   has_many :special_posts

Comments

    No one has commented yet.