Skip to content
This repository
Browse code

Deprecated support for interpolated association conditions with the :…

…conditions => 'foo = #{bar}' syntax, and added the new interpolation syntax which is :conditions => proc { "foo = #{bar}" }.
  • Loading branch information...
commit 756e70cb4492ded56b72b1601da7d198eaf7b840 1 parent 66003f5
Jon Leighton authored

Showing 23 changed files with 186 additions and 59 deletions. Show diff stats Hide diff stats

  1. 29  activerecord/CHANGELOG
  2. 22  activerecord/lib/active_record/association_preload.rb
  3. 19  activerecord/lib/active_record/associations.rb
  4. 13  activerecord/lib/active_record/associations/association_collection.rb
  5. 6  activerecord/lib/active_record/associations/association_proxy.rb
  6. 2  activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
  7. 6  activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
  8. 4  activerecord/lib/active_record/associations/has_many_association.rb
  9. 2  activerecord/lib/active_record/associations/has_many_through_association.rb
  10. 6  activerecord/lib/active_record/associations/through_association_scope.rb
  11. 25  activerecord/lib/active_record/base.rb
  12. 2  activerecord/test/cases/associations/belongs_to_associations_test.rb
  13. 18  activerecord/test/cases/associations/eager_test.rb
  14. 12  activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
  15. 4  activerecord/test/cases/associations/has_many_associations_test.rb
  16. 20  activerecord/test/cases/associations/has_many_through_associations_test.rb
  17. 10  activerecord/test/cases/associations/has_one_associations_test.rb
  18. 6  activerecord/test/cases/base_test.rb
  19. 4  activerecord/test/cases/reflection_test.rb
  20. 15  activerecord/test/models/company.rb
  21. 9  activerecord/test/models/post.rb
  22. 7  activerecord/test/models/project.rb
  23. 4  activerecord/test/models/tagging.rb
29  activerecord/CHANGELOG
... ...
@@ -1,4 +1,31 @@
1  
-*Rails 3.0.4 (unreleased)*
  1
+*Rails 3.0.5 (unreleased)*
  2
+
  3
+* Deprecated support for interpolated association conditions in the form of :conditions => 'foo = #{bar}'.
  4
+
  5
+  Instead, you should use a proc, like so:
  6
+
  7
+  Before:
  8
+
  9
+    has_many :things, :conditions => 'foo = #{bar}'
  10
+
  11
+  After:
  12
+
  13
+    has_many :things, :conditions => proc { "foo = #{bar}" }
  14
+
  15
+  Inside the proc, 'self' is the object which is the owner of the association, unless you are
  16
+  eager loading the association, in which case 'self' is the class which the association is within.
  17
+
  18
+  You can have any "normal" conditions inside the proc, so the following will work too:
  19
+
  20
+    has_many :things, :conditions => proc { ["foo = ?", bar] }
  21
+
  22
+  Previously :insert_sql and :delete_sql on has_and_belongs_to_many association allowed you to call
  23
+  'record' to get the record being inserted or deleted. This is now passed as an argument to
  24
+  the proc.
  25
+
  26
+  [Jon Leighton]
  27
+
  28
+*Rails 3.0.4*
2 29
 
3 30
 * Added deprecation warning for has_and_belongs_to_many associations where the join table has
4 31
   additional attributes other than the keys. Access to these attributes is removed in 3.1.
22  activerecord/lib/active_record/association_preload.rb
@@ -387,15 +387,27 @@ def find_associated_records(ids, reflection, preload_options)
387 387
         end
388 388
       end
389 389
 
390  
-
391  
-      def interpolate_sql_for_preload(sql)
392  
-        instance_eval("%@#{sql.gsub('@', '\@')}@", __FILE__, __LINE__)
  390
+      def process_conditions_for_preload(conditions, klass = self)
  391
+        sanitized = klass.send(:sanitize_sql, conditions)
  392
+
  393
+        if sanitized =~ /\#\{.*\}/
  394
+          ActiveSupport::Deprecation.warn(
  395
+            'String-based interpolation of association conditions is deprecated. Please use a ' \
  396
+            'proc instead. So, for example, has_many :older_friends, :conditions => \'age > #{age}\' ' \
  397
+            'should be changed to has_many :older_friends, :conditions => proc { "age > #{age}" }.'
  398
+          )
  399
+          instance_eval("%@#{sanitized.gsub('@', '\@')}@", __FILE__, __LINE__)
  400
+        elsif conditions.respond_to?(:to_proc)
  401
+          klass.send(:sanitize_sql, instance_eval(&conditions))
  402
+        else
  403
+          sanitized
  404
+        end
393 405
       end
394 406
 
395 407
       def append_conditions(reflection, preload_options)
396 408
         sql = ""
397  
-        sql << " AND (#{interpolate_sql_for_preload(reflection.sanitized_conditions)})" if reflection.sanitized_conditions
398  
-        sql << " AND (#{sanitize_sql preload_options[:conditions]})" if preload_options[:conditions]
  409
+        sql << " AND (#{process_conditions_for_preload(reflection.options[:conditions], reflection.klass)})" if reflection.options[:conditions]
  410
+        sql << " AND (#{process_conditions_for_preload(preload_options[:conditions])})" if preload_options[:conditions]
399 411
         sql
400 412
       end
401 413
 
19  activerecord/lib/active_record/associations.rb
@@ -2221,7 +2221,7 @@ def association_join
2221 2221
 
2222 2222
               [through_reflection, reflection].each do |ref|
2223 2223
                 if ref && ref.options[:conditions]
2224  
-                  @join << interpolate_sql(sanitize_sql(ref.options[:conditions], aliased_table_name))
  2224
+                  @join << process_conditions(ref.options[:conditions], aliased_table_name)
2225 2225
                 end
2226 2226
               end
2227 2227
 
@@ -2282,8 +2282,21 @@ def table_name_and_alias
2282 2282
                 table_alias_for table_name, @aliased_table_name
2283 2283
               end
2284 2284
 
2285  
-              def interpolate_sql(sql)
2286  
-                instance_eval("%@#{sql.gsub('@', '\@')}@", __FILE__, __LINE__)
  2285
+              def process_conditions(conditions, table_name)
  2286
+                sanitized = sanitize_sql(conditions, table_name)
  2287
+
  2288
+                if sanitized =~ /\#\{.*\}/
  2289
+                  ActiveSupport::Deprecation.warn(
  2290
+                    'String-based interpolation of association conditions is deprecated. Please use a ' \
  2291
+                    'proc instead. So, for example, has_many :older_friends, :conditions => \'age > #{age}\' ' \
  2292
+                    'should be changed to has_many :older_friends, :conditions => proc { "age > #{age}" }.'
  2293
+                  )
  2294
+                  instance_eval("%@#{sanitized.gsub('@', '\@')}@", __FILE__, __LINE__)
  2295
+                elsif conditions.respond_to?(:to_proc)
  2296
+                  conditions = sanitize_sql(instance_eval(&conditions), table_name)
  2297
+                else
  2298
+                  sanitized
  2299
+                end
2287 2300
               end
2288 2301
           end
2289 2302
         end
13  activerecord/lib/active_record/associations/association_collection.rb
@@ -185,9 +185,11 @@ def sum(*args)
185 185
       def count(column_name = nil, options = {})
186 186
         column_name, options = nil, column_name if column_name.is_a?(Hash)
187 187
 
188  
-        if @reflection.options[:counter_sql] && !options.blank?
189  
-          raise ArgumentError, "If finder_sql/counter_sql is used then options cannot be passed"
190  
-        elsif @reflection.options[:counter_sql]
  188
+        if @reflection.options[:finder_sql] || @reflection.options[:counter_sql]
  189
+          unless options.blank?
  190
+            raise ArgumentError, "If finder_sql/counter_sql is used then options cannot be passed"
  191
+          end
  192
+
191 193
           @reflection.klass.count_by_sql(@counter_sql)
192 194
         else
193 195
 
@@ -379,11 +381,10 @@ def construct_find_options!(options)
379 381
 
380 382
         def construct_counter_sql
381 383
           if @reflection.options[:counter_sql]
382  
-            @counter_sql = interpolate_sql(@reflection.options[:counter_sql])
  384
+            @counter_sql = interpolate_and_sanitize_sql(@reflection.options[:counter_sql])
383 385
           elsif @reflection.options[:finder_sql]
384 386
             # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */
385  
-            @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" }
386  
-            @counter_sql = interpolate_sql(@reflection.options[:counter_sql])
  387
+            @counter_sql = interpolate_and_sanitize_sql(@reflection.options[:finder_sql]).sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" }
387 388
           else
388 389
             @counter_sql = @finder_sql
389 390
           end
6  activerecord/lib/active_record/associations/association_proxy.rb
@@ -102,7 +102,7 @@ def aliased_table_name
102 102
       # Returns the SQL string that corresponds to the <tt>:conditions</tt>
103 103
       # option of the macro, if given, or +nil+ otherwise.
104 104
       def conditions
105  
-        @conditions ||= interpolate_sql(@reflection.sanitized_conditions) if @reflection.sanitized_conditions
  105
+        @conditions ||= @reflection.options[:conditions] && interpolate_and_sanitize_sql(@reflection.options[:conditions])
106 106
       end
107 107
       alias :sql_conditions :conditions
108 108
 
@@ -161,8 +161,8 @@ def dependent?
161 161
           @reflection.options[:dependent]
162 162
         end
163 163
 
164  
-        def interpolate_sql(sql, record = nil)
165  
-          @owner.send(:interpolate_sql, sql, record)
  164
+        def interpolate_and_sanitize_sql(sql, record = nil, sanitize_klass = @reflection.klass)
  165
+          @owner.send(:interpolate_and_sanitize_sql, sql, record, sanitize_klass)
166 166
         end
167 167
 
168 168
         # Forwards the call to the reflection class.
2  activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
@@ -24,7 +24,7 @@ def updated?
24 24
       end
25 25
 
26 26
       def conditions
27  
-       	@conditions ||= interpolate_sql(association_class.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]
  27
+        @conditions ||= @reflection.options[:conditions] && interpolate_and_sanitize_sql(@reflection.options[:conditions], nil, association_class)
28 28
       end
29 29
 
30 30
       private
6  activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -52,7 +52,7 @@ def insert_record(record, force = true, validate = true)
52 52
           end
53 53
 
54 54
           if @reflection.options[:insert_sql]
55  
-            @owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record))
  55
+            @owner.connection.insert(interpolate_and_sanitize_sql(@reflection.options[:insert_sql], record))
56 56
           else
57 57
             relation   = Arel::Table.new(@reflection.options[:join_table])
58 58
             timestamps = record_timestamp_columns(record)
@@ -81,7 +81,7 @@ def insert_record(record, force = true, validate = true)
81 81
 
82 82
         def delete_records(records)
83 83
           if sql = @reflection.options[:delete_sql]
84  
-            records.each { |record| @owner.connection.delete(interpolate_sql(sql, record)) }
  84
+            records.each { |record| @owner.connection.delete(interpolate_and_sanitize_sql(sql, record)) }
85 85
           else
86 86
             relation = Arel::Table.new(@reflection.options[:join_table])
87 87
             relation.where(relation[@reflection.primary_key_name].eq(@owner.id).
@@ -92,7 +92,7 @@ def delete_records(records)
92 92
 
93 93
         def construct_sql
94 94
           if @reflection.options[:finder_sql]
95  
-            @finder_sql = interpolate_sql(@reflection.options[:finder_sql])
  95
+            @finder_sql = interpolate_and_sanitize_sql(@reflection.options[:finder_sql])
96 96
           else
97 97
             @finder_sql = "#{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.primary_key_name} = #{owner_quoted_id} "
98 98
             @finder_sql << " AND (#{conditions})" if conditions
4  activerecord/lib/active_record/associations/has_many_association.rb
@@ -35,7 +35,7 @@ def owner_quoted_id
35 35
         def count_records
36 36
           count = if has_cached_counter?
37 37
             @owner.send(:read_attribute, cached_counter_attribute_name)
38  
-          elsif @reflection.options[:counter_sql]
  38
+          elsif @reflection.options[:finder_sql] || @reflection.options[:counter_sql]
39 39
             @reflection.klass.count_by_sql(@counter_sql)
40 40
           else
41 41
             @reflection.klass.count(:conditions => @counter_sql, :include => @reflection.options[:include])
@@ -90,7 +90,7 @@ def target_obsolete?
90 90
         def construct_sql
91 91
           case
92 92
             when @reflection.options[:finder_sql]
93  
-              @finder_sql = interpolate_sql(@reflection.options[:finder_sql])
  93
+              @finder_sql = interpolate_and_sanitize_sql(@reflection.options[:finder_sql])
94 94
 
95 95
             when @reflection.options[:as]
96 96
               @finder_sql =
2  activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -87,7 +87,7 @@ def find_target
87 87
         def construct_sql
88 88
           case
89 89
             when @reflection.options[:finder_sql]
90  
-              @finder_sql = interpolate_sql(@reflection.options[:finder_sql])
  90
+              @finder_sql = interpolate_and_sanitize_sql(@reflection.options[:finder_sql])
91 91
 
92 92
               @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}"
93 93
               @finder_sql << " AND (#{conditions})" if conditions
6  activerecord/lib/active_record/associations/through_association_scope.rb
@@ -123,7 +123,7 @@ def build_conditions
123 123
           all = []
124 124
 
125 125
           [association_conditions, source_conditions].each do |conditions|
126  
-            all << interpolate_sql(sanitize_sql(conditions)) if conditions
  126
+            all << interpolate_and_sanitize_sql(conditions) if conditions
127 127
           end
128 128
 
129 129
           all << through_conditions  if through_conditions
@@ -136,11 +136,11 @@ def build_conditions
136 136
       def build_through_conditions
137 137
         conditions = @reflection.through_reflection.options[:conditions]
138 138
         if conditions.is_a?(Hash)
139  
-          interpolate_sql(@reflection.through_reflection.klass.send(:sanitize_sql, conditions)).gsub(
  139
+          interpolate_and_sanitize_sql(conditions, nil, @reflection.through_reflection.klass).gsub(
140 140
             @reflection.quoted_table_name,
141 141
             @reflection.through_reflection.quoted_table_name)
142 142
         elsif conditions
143  
-          interpolate_sql(sanitize_sql(conditions))
  143
+          interpolate_and_sanitize_sql(conditions)
144 144
         end
145 145
       end
146 146
 
25  activerecord/lib/active_record/base.rb
@@ -879,8 +879,8 @@ def arel_engine
879 879
       # It is recommended to use block form of unscoped because chaining unscoped with <tt>named_scope</tt>
880 880
       # does not work. Assuming that <tt>published</tt> is a <tt>named_scope</tt> following two statements are same.
881 881
       #
882  
-      # Post.unscoped.published 
883  
-      # Post.published 
  882
+      # Post.unscoped.published
  883
+      # Post.published
884 884
       def unscoped #:nodoc:
885 885
         block_given? ? relation.scoping { yield } : relation
886 886
       end
@@ -1606,7 +1606,7 @@ def column_for_attribute(name)
1606 1606
         self.class.columns_hash[name.to_s]
1607 1607
       end
1608 1608
 
1609  
-      # Returns true if +comparison_object+ is the same exact object, or +comparison_object+ 
  1609
+      # Returns true if +comparison_object+ is the same exact object, or +comparison_object+
1610 1610
       # is of the same type and +self+ has an ID and it is equal to +comparison_object.id+.
1611 1611
       #
1612 1612
       # Note that new records are different from any other record by definition, unless the
@@ -1730,10 +1730,21 @@ def quote_value(value, column = nil)
1730 1730
         self.class.connection.quote(value, column)
1731 1731
       end
1732 1732
 
1733  
-      # Interpolate custom SQL string in instance context.
1734  
-      # Optional record argument is meant for custom insert_sql.
1735  
-      def interpolate_sql(sql, record = nil)
1736  
-        instance_eval("%@#{sql.gsub('@', '\@')}@", __FILE__, __LINE__)
  1733
+      def interpolate_and_sanitize_sql(sql, record = nil, sanitize_klass = self.class)
  1734
+        sanitized = sanitize_klass.send(:sanitize_sql, sql)
  1735
+
  1736
+        if sanitized =~ /\#\{.*\}/
  1737
+          ActiveSupport::Deprecation.warn(
  1738
+            'String-based interpolation of association conditions is deprecated. Please use a ' \
  1739
+            'proc instead. So, for example, has_many :older_friends, :conditions => \'age > #{age}\' ' \
  1740
+            'should be changed to has_many :older_friends, :conditions => proc { "age > #{age}" }.'
  1741
+          )
  1742
+          instance_eval("%@#{sanitized.gsub('@', '\@')}@", __FILE__, __LINE__)
  1743
+        elsif sql.respond_to?(:to_proc)
  1744
+          sanitize_klass.send(:sanitize_sql, instance_exec(record, &sql))
  1745
+        else
  1746
+          sanitized
  1747
+        end
1737 1748
       end
1738 1749
 
1739 1750
       # Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done
2  activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -151,7 +151,7 @@ def test_with_condition
151 151
     assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
152 152
     assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
153 153
   end
154  
-  
  154
+
155 155
   def test_with_polymorphic_and_condition
156 156
     sponsor = Sponsor.create
157 157
     member = Member.create :name => "Bert"
18  activerecord/test/cases/associations/eager_test.rb
@@ -651,7 +651,23 @@ def test_limited_eager_with_numeric_in_association
651 651
   end
652 652
 
653 653
   def test_preload_with_interpolation
654  
-    assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
  654
+    post = Post.includes(:comments_with_interpolated_conditions).find(posts(:welcome).id)
  655
+    assert_equal [comments(:greetings)], post.comments_with_interpolated_conditions
  656
+
  657
+    post = Post.joins(:comments_with_interpolated_conditions).find(posts(:welcome).id)
  658
+    assert_equal [comments(:greetings)], post.comments_with_interpolated_conditions
  659
+  end
  660
+
  661
+  def test_preload_with_deprecated_interpolation
  662
+    post = assert_deprecated do
  663
+      Post.includes(:comments_with_deprecated_interpolated_conditions).find(posts(:welcome).id)
  664
+    end
  665
+    assert_equal [comments(:greetings)], post.comments_with_interpolated_conditions
  666
+
  667
+    post = assert_deprecated do
  668
+      Post.joins(:comments_with_deprecated_interpolated_conditions).find(posts(:welcome).id)
  669
+    end
  670
+    assert_equal [comments(:greetings)], post.comments_with_interpolated_conditions
655 671
   end
656 672
 
657 673
   def test_polymorphic_type_condition
12  activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -415,6 +415,16 @@ def test_deleting_with_sql
415 415
     assert_equal 2, active_record.developers_by_sql(true).size
416 416
   end
417 417
 
  418
+  def test_deleting_with_sql_with_deprecated_interpolation
  419
+    david = Developer.find(1)
  420
+    active_record = Project.find(1)
  421
+    active_record.developers.reload
  422
+    assert_equal 3, active_record.developers_by_sql_deprecated.size
  423
+
  424
+    active_record.developers_by_sql_deprecated.delete(david)
  425
+    assert_equal 2, active_record.developers_by_sql_deprecated(true).size
  426
+  end
  427
+
418 428
   def test_deleting_array_with_sql
419 429
     active_record = Project.find(1)
420 430
     active_record.developers.reload
@@ -837,7 +847,7 @@ def test_count_with_counter_sql
837 847
   unless current_adapter?(:PostgreSQLAdapter)
838 848
     def test_count_with_finder_sql
839 849
       assert_equal 3, projects(:active_record).developers_with_finder_sql.count
840  
-      assert_equal 3, projects(:active_record).developers_with_multiline_finder_sql.count
  850
+      assert_equal 3, projects(:active_record).developers_with_deprecated_multiline_finder_sql.count
841 851
     end
842 852
   end
843 853
 
4  activerecord/test/cases/associations/has_many_associations_test.rb
@@ -247,7 +247,9 @@ def test_counting_non_existant_items_using_sql
247 247
 
248 248
   def test_counting_using_finder_sql
249 249
     assert_equal 2, Firm.find(4).clients_using_sql.count
250  
-    assert_equal 2, Firm.find(4).clients_using_multiline_sql.count
  250
+    assert_deprecated do
  251
+      assert_equal 2, Firm.find(4).clients_using_deprecated_multiline_sql.count
  252
+    end
251 253
   end
252 254
 
253 255
   def test_belongs_to_sanity
20  activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -21,7 +21,7 @@
21 21
 require 'models/categorization'
22 22
 
23 23
 class HasManyThroughAssociationsTest < ActiveRecord::TestCase
24  
-  fixtures :posts, :readers, :people, :comments, :authors, :categories,
  24
+  fixtures :posts, :readers, :people, :comments, :authors, :categories, :tags, :taggings,
25 25
            :owners, :pets, :toys, :jobs, :references, :companies,
26 26
            :subscribers, :books, :subscriptions, :developers, :categorizations
27 27
 
@@ -477,4 +477,22 @@ def test_joining_has_many_through_belongs_to
477 477
 
478 478
     assert_equal [posts(:eager_other)], posts
479 479
   end
  480
+
  481
+  def test_interpolated_conditions
  482
+    post = posts(:welcome)
  483
+    assert !post.tags.empty?
  484
+    assert_equal post.tags, post.interpolated_tags
  485
+    assert_equal post.tags, post.interpolated_tags_2
  486
+  end
  487
+
  488
+  def test_deprecated_interpolated_conditions
  489
+    post = posts(:welcome)
  490
+    assert !post.tags.empty?
  491
+    assert_deprecated do
  492
+      assert_equal post.tags, post.deprecated_interpolated_tags
  493
+    end
  494
+    assert_deprecated do
  495
+      assert_equal post.tags, post.deprecated_interpolated_tags_2
  496
+    end
  497
+  end
480 498
 end
10  activerecord/test/cases/associations/has_one_associations_test.rb
@@ -265,6 +265,16 @@ def test_finding_with_interpolated_condition
265 265
     assert_equal 10, firm.clients_with_interpolated_conditions.first.rating
266 266
   end
267 267
 
  268
+  def test_finding_with_deprecated_interpolated_condition
  269
+    firm = Firm.find(:first)
  270
+    superior = firm.clients.create(:name => 'SuperiorCo')
  271
+    superior.rating = 10
  272
+    superior.save
  273
+    assert_deprecated do
  274
+      assert_equal 10, firm.clients_with_deprecated_interpolated_conditions.first.rating
  275
+    end
  276
+  end
  277
+
268 278
   def test_assignment_before_child_saved
269 279
     firm = Firm.find(1)
270 280
     firm.account = a = Account.new("credit_limit" => 1000)
6  activerecord/test/cases/base_test.rb
@@ -1195,12 +1195,6 @@ def test_count_with_join
1195 1195
     assert_equal res6, res7
1196 1196
   end
1197 1197
 
1198  
-  def test_interpolate_sql
1199  
-    assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo@bar') }
1200  
-    assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar) baz') }
1201  
-    assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar} baz') }
1202  
-  end
1203  
-
1204 1198
   def test_scoped_find_conditions
1205 1199
     scoped_developers = Developer.send(:with_scope, :find => { :conditions => 'salary > 90000' }) do
1206 1200
       Developer.find(:all, :conditions => 'id < 5')
4  activerecord/test/cases/reflection_test.rb
@@ -179,8 +179,8 @@ def test_association_reflection_in_modules
179 179
 
180 180
   def test_reflection_of_all_associations
181 181
     # FIXME these assertions bust a lot
182  
-    assert_equal 37, Firm.reflect_on_all_associations.size
183  
-    assert_equal 27, Firm.reflect_on_all_associations(:has_many).size
  182
+    assert_equal 38, Firm.reflect_on_all_associations.size
  183
+    assert_equal 28, Firm.reflect_on_all_associations(:has_many).size
184 184
     assert_equal 10, Firm.reflect_on_all_associations(:has_one).size
185 185
     assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
186 186
   end
15  activerecord/test/models/company.rb
@@ -47,19 +47,20 @@ class Firm < Company
47 47
   has_many :exclusively_dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all
48 48
   has_many :limited_clients, :class_name => "Client", :limit => 1
49 49
   has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
50  
-  has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => 'rating > #{rating}'
  50
+  has_many :clients_with_deprecated_interpolated_conditions, :class_name => "Client", :conditions => 'rating > #{rating}'
  51
+  has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => proc { "rating > #{rating}" }
51 52
   has_many :clients_like_ms_with_hash_conditions, :conditions => { :name => 'Microsoft' }, :class_name => "Client", :order => "id"
52  
-  has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
53  
-  has_many :clients_using_multiline_sql, :class_name => "Client", :finder_sql => '
  53
+  has_many :clients_using_sql, :class_name => "Client", :finder_sql => proc { "SELECT * FROM companies WHERE client_of = #{id}" }
  54
+  has_many :clients_using_deprecated_multiline_sql, :class_name => "Client", :finder_sql => '
54 55
   SELECT
55 56
   companies.*
56 57
   FROM companies WHERE companies.client_of = #{id}'
57 58
   has_many :clients_using_counter_sql, :class_name => "Client",
58  
-           :finder_sql  => 'SELECT * FROM companies WHERE client_of = #{id}',
59  
-           :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = #{id}'
  59
+           :finder_sql  => proc { "SELECT * FROM companies WHERE client_of = #{id}" },
  60
+           :counter_sql => proc { "SELECT COUNT(*) FROM companies WHERE client_of = #{id}" }
60 61
   has_many :clients_using_zero_counter_sql, :class_name => "Client",
61  
-           :finder_sql  => 'SELECT * FROM companies WHERE client_of = #{id}',
62  
-           :counter_sql => 'SELECT 0 FROM companies WHERE client_of = #{id}'
  62
+           :finder_sql  => proc { "SELECT * FROM companies WHERE client_of = #{id}" },
  63
+           :counter_sql => proc { "SELECT 0 FROM companies WHERE client_of = #{id}" }
63 64
   has_many :no_clients_using_counter_sql, :class_name => "Client",
64 65
            :finder_sql  => 'SELECT * FROM companies WHERE client_of = 1000',
65 66
            :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000'
9  activerecord/test/models/post.rb
@@ -41,6 +41,8 @@ def find_most_recent
41 41
   has_many :author_categorizations, :through => :author, :source => :categorizations
42 42
 
43 43
   has_many :comments_with_interpolated_conditions, :class_name => 'Comment',
  44
+      :conditions => proc { ["#{"#{aliased_table_name}." rescue ""}body = ?", 'Thank you for the welcome'] }
  45
+  has_many :comments_with_deprecated_interpolated_conditions, :class_name => 'Comment',
44 46
       :conditions => ['#{"#{aliased_table_name}." rescue ""}body = ?', 'Thank you for the welcome']
45 47
 
46 48
   has_one  :very_special_comment
@@ -67,6 +69,13 @@ def add_joins_and_select
67 69
   has_many :invalid_taggings, :as => :taggable, :class_name => "Tagging", :conditions => 'taggings.id < 0'
68 70
   has_many :invalid_tags, :through => :invalid_taggings, :source => :tag
69 71
 
  72
+  has_many :interpolated_taggings, :class_name => 'Tagging', :as => :taggable, :conditions => proc { "1 = #{1}" }
  73
+  has_many :deprecated_interpolated_taggings, :class_name => 'Tagging', :as => :taggable, :conditions => '1 = #{1}'
  74
+  has_many :interpolated_tags, :through => :taggings
  75
+  has_many :deprecated_interpolated_tags, :through => :taggings
  76
+  has_many :interpolated_tags_2, :through => :interpolated_taggings, :source => :tag
  77
+  has_many :deprecated_interpolated_tags_2, :through => :deprecated_interpolated_taggings, :source => :tag
  78
+
70 79
   has_many :categorizations, :foreign_key => :category_id
71 80
   has_many :authors, :through => :categorizations
72 81
 
7  activerecord/test/models/project.rb
@@ -7,14 +7,15 @@ class Project < ActiveRecord::Base
7 7
   has_and_belongs_to_many :developers_named_david, :class_name => "Developer", :conditions => "name = 'David'", :uniq => true
8 8
   has_and_belongs_to_many :developers_named_david_with_hash_conditions, :class_name => "Developer", :conditions => { :name => 'David' }, :uniq => true
9 9
   has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0"
10  
-  has_and_belongs_to_many :developers_with_finder_sql, :class_name => "Developer", :finder_sql => 'SELECT t.*, j.* FROM developers_projects j, developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id'
11  
-  has_and_belongs_to_many :developers_with_multiline_finder_sql, :class_name => "Developer", :finder_sql => '
  10
+  has_and_belongs_to_many :developers_with_finder_sql, :class_name => "Developer", :finder_sql => proc { "SELECT t.*, j.* FROM developers_projects j, developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id" }
  11
+  has_and_belongs_to_many :developers_with_deprecated_multiline_finder_sql, :class_name => "Developer", :finder_sql => '
12 12
     SELECT
13 13
       t.*, j.*
14 14
     FROM
15 15
       developers_projects j,
16 16
       developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id'
17  
-  has_and_belongs_to_many :developers_by_sql, :class_name => "Developer", :delete_sql => "DELETE FROM developers_projects WHERE project_id = \#{id} AND developer_id = \#{record.id}"
  17
+  has_and_belongs_to_many :developers_by_sql, :class_name => "Developer", :delete_sql => proc { |record| "DELETE FROM developers_projects WHERE project_id = #{id} AND developer_id = #{record.id}" }
  18
+  has_and_belongs_to_many :developers_by_sql_deprecated, :class_name => "Developer", :delete_sql => "DELETE FROM developers_projects WHERE project_id = \#{id} AND developer_id = \#{record.id}"
18 19
   has_and_belongs_to_many :developers_with_callbacks, :class_name => "Developer", :before_add => Proc.new {|o, r| o.developers_log << "before_adding#{r.id || '<new>'}"},
19 20
                             :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || '<new>'}"},
20 21
                             :before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"},
4  activerecord/test/models/tagging.rb
@@ -6,5 +6,7 @@ class Tagging < ActiveRecord::Base
6 6
   belongs_to :tag, :include => :tagging
7 7
   belongs_to :super_tag,   :class_name => 'Tag', :foreign_key => 'super_tag_id'
8 8
   belongs_to :invalid_tag, :class_name => 'Tag', :foreign_key => 'tag_id'
  9
+  belongs_to :interpolated_tag, :class_name => 'Tag', :foreign_key => :tag_id, :conditions => proc { "1 = #{1}" }
  10
+  belongs_to :deprecated_interpolated_tag, :class_name => 'Tag', :foreign_key => :tag_id, :conditions => '1 = #{1}'
9 11
   belongs_to :taggable, :polymorphic => true, :counter_cache => true
10  
-end
  12
+end

0 notes on commit 756e70c

Please sign in to comment.
Something went wrong with that request. Please try again.