Skip to content
This repository
Browse code

Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql.

  • Loading branch information...
commit 3803fcce26b837c0117f7d278b83c366dc4ed370 1 parent 70a86c6
Jon Leighton authored July 20, 2012
10  activerecord/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,15 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Removed `:finder_sql` and `:counter_sql` collection association options. Please
  4
+    use scopes instead.
  5
+
  6
+    *Jon Leighton*
  7
+
  8
+*   Removed `:insert_sql` and `:delete_sql` `has_and_belongs_to_many`
  9
+    association options. Please use `has_many :through` instead.
  10
+
  11
+    *Jon Leighton*
  12
+
3 13
 *   The migration generator now creates a join table with (commented) indexes every time
4 14
     the migration name contains the word `join_table`:
5 15
 
31  activerecord/lib/active_record/associations.rb
@@ -1104,15 +1104,6 @@ module ClassMethods
1104 1104
       #   a +belongs_to+, and the records which get deleted are the join records, rather than
1105 1105
       #   the associated records.
1106 1106
       #
1107  
-      # [:finder_sql]
1108  
-      #   Specify a complete SQL statement to fetch the association. This is a good way to go for complex
1109  
-      #   associations that depend on multiple tables. May be supplied as a string or a proc where interpolation is
1110  
-      #   required. Note: When this option is used, +find_in_collection+
1111  
-      #   is _not_ added.
1112  
-      # [:counter_sql]
1113  
-      #   Specify a complete SQL statement to fetch the size of the association. If <tt>:finder_sql</tt> is
1114  
-      #   specified but not <tt>:counter_sql</tt>, <tt>:counter_sql</tt> will be generated by
1115  
-      #   replacing <tt>SELECT ... FROM</tt> with <tt>SELECT COUNT(*) FROM</tt>.
1116 1107
       # [:extend]
1117 1108
       #   Specify a named module for extending the proxy. See "Association extensions".
1118 1109
       # [:include]
@@ -1184,14 +1175,6 @@ module ClassMethods
1184 1175
       #   has_many :tags, :as => :taggable
1185 1176
       #   has_many :reports, :readonly => true
1186 1177
       #   has_many :subscribers, :through => :subscriptions, :source => :user
1187  
-      #   has_many :subscribers, :class_name => "Person", :finder_sql => Proc.new {
1188  
-      #       %Q{
1189  
-      #         SELECT DISTINCT *
1190  
-      #         FROM people p, post_subscriptions ps
1191  
-      #         WHERE ps.post_id = #{id} AND ps.person_id = p.id
1192  
-      #         ORDER BY p.first_name
1193  
-      #       }
1194  
-      #   }
1195 1178
       def has_many(name, scope = nil, options = {}, &extension)
1196 1179
         Builder::HasMany.build(self, name, scope, options, &extension)
1197 1180
       end
@@ -1555,18 +1538,6 @@ def belongs_to(name, scope = nil, options = {})
1555 1538
       #   such as <tt>last_name, first_name DESC</tt>
1556 1539
       # [:uniq]
1557 1540
       #   If true, duplicate associated objects will be ignored by accessors and query methods.
1558  
-      # [:finder_sql]
1559  
-      #   Overwrite the default generated SQL statement used to fetch the association with a manual statement
1560  
-      # [:counter_sql]
1561  
-      #   Specify a complete SQL statement to fetch the size of the association. If <tt>:finder_sql</tt> is
1562  
-      #   specified but not <tt>:counter_sql</tt>, <tt>:counter_sql</tt> will be generated by
1563  
-      #   replacing <tt>SELECT ... FROM</tt> with <tt>SELECT COUNT(*) FROM</tt>.
1564  
-      # [:delete_sql]
1565  
-      #   Overwrite the default generated SQL statement used to remove links between the associated
1566  
-      #   classes with a manual statement.
1567  
-      # [:insert_sql]
1568  
-      #   Overwrite the default generated SQL statement used to add links between the associated classes
1569  
-      #   with a manual statement.
1570 1541
       # [:extend]
1571 1542
       #   Anonymous module for extending the proxy, see "Association extensions".
1572 1543
       # [:include]
@@ -1603,8 +1574,6 @@ def belongs_to(name, scope = nil, options = {})
1603 1574
       #   has_and_belongs_to_many :nations, :class_name => "Country"
1604 1575
       #   has_and_belongs_to_many :categories, :join_table => "prods_cats"
1605 1576
       #   has_and_belongs_to_many :categories, :readonly => true
1606  
-      #   has_and_belongs_to_many :active_projects, :join_table => 'developers_projects', :delete_sql =>
1607  
-      #   proc { |record| "DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}" }
1608 1577
       def has_and_belongs_to_many(name, scope = nil, options = {}, &extension)
1609 1578
         Builder::HasAndBelongsToMany.build(self, name, scope, options, &extension)
1610 1579
       end
8  activerecord/lib/active_record/associations/association.rb
@@ -140,14 +140,6 @@ def load_target
140 140
         reset
141 141
       end
142 142
 
143  
-      def interpolate(sql, record = nil)
144  
-        if sql.respond_to?(:to_proc)
145  
-          owner.send(:instance_exec, record, &sql)
146  
-        else
147  
-          sql
148  
-        end
149  
-      end
150  
-
151 143
       # We can't dump @reflection since it contains the scope proc
152 144
       def marshal_dump
153 145
         reflection  = @reflection
2  activerecord/lib/active_record/associations/association_scope.rb
@@ -5,7 +5,7 @@ class AssociationScope #:nodoc:
5 5
 
6 6
       attr_reader :association, :alias_tracker
7 7
 
8  
-      delegate :klass, :owner, :reflection, :interpolate, :to => :association
  8
+      delegate :klass, :owner, :reflection, :to => :association
9 9
       delegate :chain, :scope_chain, :options, :source_options, :active_record, :to => :reflection
10 10
 
11 11
       def initialize(association)
2  activerecord/lib/active_record/associations/builder/collection_association.rb
@@ -3,7 +3,7 @@ class CollectionAssociation < Association #:nodoc:
3 3
     CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
4 4
 
5 5
     def valid_options
6  
-      super + [:table_name, :finder_sql, :counter_sql, :before_add, :after_add, :before_remove, :after_remove]
  6
+      super + [:table_name, :before_add, :after_add, :before_remove, :after_remove]
7 7
     end
8 8
 
9 9
     attr_reader :block_extension, :extension_module
2  activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb
@@ -5,7 +5,7 @@ def macro
5 5
     end
6 6
 
7 7
     def valid_options
8  
-      super + [:join_table, :association_foreign_key, :delete_sql, :insert_sql]
  8
+      super + [:join_table, :association_foreign_key]
9 9
     end
10 10
 
11 11
     def build
85  activerecord/lib/active_record/associations/collection_association.rb
@@ -44,7 +44,7 @@ def writer(records)
44 44
 
45 45
       # Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
46 46
       def ids_reader
47  
-        if loaded? || options[:finder_sql]
  47
+        if loaded?
48 48
           load_target.map do |record|
49 49
             record.send(reflection.association_primary_key)
50 50
           end
@@ -79,11 +79,7 @@ def find(*args)
79 79
         if block_given?
80 80
           load_target.find(*args) { |*block_args| yield(*block_args) }
81 81
         else
82  
-          if options[:finder_sql]
83  
-            find_by_scan(*args)
84  
-          else
85  
-            scoped.find(*args)
86  
-          end
  82
+          scoped.find(*args)
87 83
         end
88 84
       end
89 85
 
@@ -170,35 +166,26 @@ def sum(*args)
170 166
         end
171 167
       end
172 168
 
173  
-      # Count all records using SQL. If the +:counter_sql+ or +:finder_sql+ option is set for the
174  
-      # association, it will be used for the query. Otherwise, construct options and pass them with
  169
+      # Count all records using SQL. Construct options and pass them with
175 170
       # scope to the target class's +count+.
176 171
       def count(column_name = nil, count_options = {})
177 172
         column_name, count_options = nil, column_name if column_name.is_a?(Hash)
178 173
 
179  
-        if options[:counter_sql] || options[:finder_sql]
180  
-          unless count_options.blank?
181  
-            raise ArgumentError, "If finder_sql/counter_sql is used then options cannot be passed"
182  
-          end
183  
-
184  
-          reflection.klass.count_by_sql(custom_counter_sql)
185  
-        else
186  
-          if association_scope.uniq_value
187  
-            # This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.
188  
-            column_name ||= reflection.klass.primary_key
189  
-            count_options[:distinct] = true
190  
-          end
  174
+        if association_scope.uniq_value
  175
+          # This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.
  176
+          column_name ||= reflection.klass.primary_key
  177
+          count_options[:distinct] = true
  178
+        end
191 179
 
192  
-          value = scoped.count(column_name, count_options)
  180
+        value = scoped.count(column_name, count_options)
193 181
 
194  
-          limit  = options[:limit]
195  
-          offset = options[:offset]
  182
+        limit  = options[:limit]
  183
+        offset = options[:offset]
196 184
 
197  
-          if limit || offset
198  
-            [ [value - offset.to_i, 0].max, limit.to_i ].min
199  
-          else
200  
-            value
201  
-          end
  185
+        if limit || offset
  186
+          [ [value - offset.to_i, 0].max, limit.to_i ].min
  187
+        else
  188
+          value
202 189
         end
203 190
       end
204 191
 
@@ -323,7 +310,6 @@ def include?(record)
323 310
           if record.new_record?
324 311
             include_in_memory?(record)
325 312
           else
326  
-            load_target if options[:finder_sql]
327 313
             loaded? ? target.include?(record) : scoped.exists?(record)
328 314
           end
329 315
         else
@@ -358,31 +344,8 @@ def add_to_target(record)
358 344
 
359 345
       private
360 346
 
361  
-        def custom_counter_sql
362  
-          if options[:counter_sql]
363  
-            interpolate(options[:counter_sql])
364  
-          else
365  
-            # replace the SELECT clause with COUNT(SELECTS), preserving any hints within /* ... */
366  
-            interpolate(options[:finder_sql]).sub(/SELECT\b(\/\*.*?\*\/ )?(.*)\bFROM\b/im) do
367  
-              count_with = $2.to_s
368  
-              count_with = '*' if count_with.blank? || count_with =~ /,/
369  
-              "SELECT #{$1}COUNT(#{count_with}) FROM"
370  
-            end
371  
-          end
372  
-        end
373  
-
374  
-        def custom_finder_sql
375  
-          interpolate(options[:finder_sql])
376  
-        end
377  
-
378 347
         def find_target
379  
-          records =
380  
-            if options[:finder_sql]
381  
-              reflection.klass.find_by_sql(custom_finder_sql)
382  
-            else
383  
-              scoped.all
384  
-            end
385  
-
  348
+          records = scoped.to_a
386 349
           records.each { |record| set_inverse_instance(record) }
387 350
           records
388 351
         end
@@ -521,7 +484,6 @@ def callbacks_for(callback_name)
521 484
         # Otherwise, go to the database only if none of the following are true:
522 485
         #   * target already loaded
523 486
         #   * owner is new record
524  
-        #   * custom :finder_sql exists
525 487
         #   * target contains new or changed record(s)
526 488
         #   * the first arg is an integer (which indicates the number of records to be returned)
527 489
         def fetch_first_or_last_using_find?(args)
@@ -530,7 +492,6 @@ def fetch_first_or_last_using_find?(args)
530 492
           else
531 493
             !(loaded? ||
532 494
               owner.new_record? ||
533  
-              options[:finder_sql] ||
534 495
               target.any? { |record| record.new_record? || record.changed? } ||
535 496
               args.first.kind_of?(Integer))
536 497
           end
@@ -547,20 +508,6 @@ def include_in_memory?(record)
547 508
           end
548 509
         end
549 510
 
550  
-        # If using a custom finder_sql, #find scans the entire collection.
551  
-        def find_by_scan(*args)
552  
-          expects_array = args.first.kind_of?(Array)
553  
-          ids           = args.flatten.compact.map{ |arg| arg.to_i }.uniq
554  
-
555  
-          if ids.size == 1
556  
-            id = ids.first
557  
-            record = load_target.detect { |r| id == r.id }
558  
-            expects_array ? [ record ] : record
559  
-          else
560  
-            load_target.select { |r| ids.include?(r.id) }
561  
-          end
562  
-        end
563  
-
564 511
         # Fetches the first/last using SQL if possible, otherwise from the target array.
565 512
         def first_or_last(type, *args)
566 513
           args.shift if args.first.is_a?(Hash) && args.first.empty?
39  activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -18,16 +18,12 @@ def insert_record(record, validate = true, raise = false)
18 18
           end
19 19
         end
20 20
 
21  
-        if options[:insert_sql]
22  
-          owner.connection.insert(interpolate(options[:insert_sql], record))
23  
-        else
24  
-          stmt = join_table.compile_insert(
25  
-            join_table[reflection.foreign_key]             => owner.id,
26  
-            join_table[reflection.association_foreign_key] => record.id
27  
-          )
  21
+        stmt = join_table.compile_insert(
  22
+          join_table[reflection.foreign_key]             => owner.id,
  23
+          join_table[reflection.association_foreign_key] => record.id
  24
+        )
28 25
 
29  
-          owner.connection.insert stmt
30  
-        end
  26
+        owner.connection.insert stmt
31 27
 
32 28
         record
33 29
       end
@@ -39,22 +35,17 @@ def count_records
39 35
         end
40 36
 
41 37
         def delete_records(records, method)
42  
-          if sql = options[:delete_sql]
43  
-            records = load_target if records == :all
44  
-            records.each { |record| owner.connection.delete(interpolate(sql, record)) }
45  
-          else
46  
-            relation  = join_table
47  
-            condition = relation[reflection.foreign_key].eq(owner.id)
48  
-
49  
-            unless records == :all
50  
-              condition = condition.and(
51  
-                relation[reflection.association_foreign_key]
52  
-                  .in(records.map { |x| x.id }.compact)
53  
-              )
54  
-            end
55  
-
56  
-            owner.connection.delete(relation.where(condition).compile_delete)
  38
+          relation  = join_table
  39
+          condition = relation[reflection.foreign_key].eq(owner.id)
  40
+
  41
+          unless records == :all
  42
+            condition = condition.and(
  43
+              relation[reflection.association_foreign_key]
  44
+                .in(records.map { |x| x.id }.compact)
  45
+            )
57 46
           end
  47
+
  48
+          owner.connection.delete(relation.where(condition).compile_delete)
58 49
         end
59 50
 
60 51
         def invertible_for?(record)
8  activerecord/lib/active_record/associations/has_many_association.rb
@@ -33,13 +33,7 @@ def insert_record(record, validate = true, raise = false)
33 33
         # If the collection is empty the target is set to an empty array and
34 34
         # the loaded flag is set to true as well.
35 35
         def count_records
36  
-          count = if has_cached_counter?
37  
-            owner.send(:read_attribute, cached_counter_attribute_name)
38  
-          elsif options[:counter_sql] || options[:finder_sql]
39  
-            reflection.klass.count_by_sql(custom_counter_sql)
40  
-          else
41  
-            scoped.count
42  
-          end
  36
+          count = has_cached_counter? ? owner[cached_counter_attribute_name] : scoped.count
43 37
 
44 38
           # If there's nothing in the database and @target has no new records
45 39
           # we are certain the current target is an empty array. This is a
69  activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -65,16 +65,6 @@ class DeveloperWithSymbolsForKeys < ActiveRecord::Base
65 65
     :foreign_key => "developer_id"
66 66
 end
67 67
 
68  
-class DeveloperWithCounterSQL < ActiveRecord::Base
69  
-  self.table_name = 'developers'
70  
-  has_and_belongs_to_many :projects,
71  
-    :class_name => "DeveloperWithCounterSQL",
72  
-    :join_table => "developers_projects",
73  
-    :association_foreign_key => "project_id",
74  
-    :foreign_key => "developer_id",
75  
-    :counter_sql => proc { "SELECT COUNT(*) AS count_all FROM projects INNER JOIN developers_projects ON projects.id = developers_projects.project_id WHERE developers_projects.developer_id =#{id}" }
76  
-end
77  
-
78 68
 class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
79 69
   fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects,
80 70
            :parrots, :pirates, :parrots_pirates, :treasures, :price_estimates, :tags, :taggings
@@ -361,31 +351,6 @@ def test_deleting_array
361 351
     assert_equal 0, david.projects(true).size
362 352
   end
363 353
 
364  
-  def test_deleting_with_sql
365  
-    david = Developer.find(1)
366  
-    active_record = Project.find(1)
367  
-    active_record.developers.reload
368  
-    assert_equal 3, active_record.developers_by_sql.size
369  
-
370  
-    active_record.developers_by_sql.delete(david)
371  
-    assert_equal 2, active_record.developers_by_sql(true).size
372  
-  end
373  
-
374  
-  def test_deleting_array_with_sql
375  
-    active_record = Project.find(1)
376  
-    active_record.developers.reload
377  
-    assert_equal 3, active_record.developers_by_sql.size
378  
-
379  
-    active_record.developers_by_sql.delete(Developer.all)
380  
-    assert_equal 0, active_record.developers_by_sql(true).size
381  
-  end
382  
-
383  
-  def test_deleting_all_with_sql
384  
-    project = Project.find(1)
385  
-    project.developers_by_sql.delete_all
386  
-    assert_equal 0, project.developers_by_sql.size
387  
-  end
388  
-
389 354
   def test_deleting_all
390 355
     david = Developer.find(1)
391 356
     david.projects.reload
@@ -534,25 +499,6 @@ def test_include_returns_false_for_non_matching_record_to_verify_scoping
534 499
     assert ! project.developers.include?(developer)
535 500
   end
536 501
 
537  
-  def test_find_in_association_with_custom_finder_sql
538  
-    assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id), "SQL find"
539  
-
540  
-    active_record = projects(:active_record)
541  
-    active_record.developers_with_finder_sql.reload
542  
-    assert_equal developers(:david), active_record.developers_with_finder_sql.find(developers(:david).id), "Ruby find"
543  
-  end
544  
-
545  
-  def test_find_in_association_with_custom_finder_sql_and_multiple_interpolations
546  
-    # interpolate once:
547  
-    assert_equal [developers(:david), developers(:jamis), developers(:poor_jamis)], projects(:active_record).developers_with_finder_sql, "first interpolation"
548  
-    # interpolate again, for a different project id
549  
-    assert_equal [developers(:david)], projects(:action_controller).developers_with_finder_sql, "second interpolation"
550  
-  end
551  
-
552  
-  def test_find_in_association_with_custom_finder_sql_and_string_id
553  
-    assert_equal developers(:david), projects(:active_record).developers_with_finder_sql.find(developers(:david).id.to_s), "SQL find"
554  
-  end
555  
-
556 502
   def test_find_with_merged_options
557 503
     assert_equal 1, projects(:active_record).limited_developers.size
558 504
     assert_equal 1, projects(:active_record).limited_developers.all.size
@@ -788,21 +734,6 @@ def test_count
788 734
     assert_equal 2, david.projects.count
789 735
   end
790 736
 
791  
-  def test_count_with_counter_sql
792  
-    developer  = DeveloperWithCounterSQL.create(:name => 'tekin')
793  
-    developer.project_ids = [projects(:active_record).id]
794  
-    developer.save
795  
-    developer.reload
796  
-    assert_equal 1, developer.projects.count
797  
-  end
798  
-
799  
-  unless current_adapter?(:PostgreSQLAdapter)
800  
-    def test_count_with_finder_sql
801  
-      assert_equal 3, projects(:active_record).developers_with_finder_sql.count
802  
-      assert_equal 3, projects(:active_record).developers_with_multiline_finder_sql.count
803  
-    end
804  
-  end
805  
-
806 737
   def test_association_proxy_transaction_method_starts_transaction_in_association_class
807 738
     Post.expects(:transaction)
808 739
     Category.first.posts.transaction do
102  activerecord/test/cases/associations/has_many_associations_test.rb
@@ -20,43 +20,6 @@
20 20
 require 'models/bulb'
21 21
 require 'models/engine'
22 22
 
23  
-class HasManyAssociationsTestForCountWithFinderSql < ActiveRecord::TestCase
24  
-  class Invoice < ActiveRecord::Base
25  
-    has_many :custom_line_items, :class_name => 'LineItem', :finder_sql => "SELECT line_items.* from line_items"
26  
-  end
27  
-  def test_should_fail
28  
-    assert_raise(ArgumentError) do
29  
-      Invoice.create.custom_line_items.count(:conditions => {:amount => 0})
30  
-    end
31  
-  end
32  
-end
33  
-
34  
-class HasManyAssociationsTestForCountWithCountSql < ActiveRecord::TestCase
35  
-  class Invoice < ActiveRecord::Base
36  
-    has_many :custom_line_items, :class_name => 'LineItem', :counter_sql => "SELECT COUNT(*) line_items.* from line_items"
37  
-  end
38  
-  def test_should_fail
39  
-    assert_raise(ArgumentError) do
40  
-      Invoice.create.custom_line_items.count(:conditions => {:amount => 0})
41  
-    end
42  
-  end
43  
-end
44  
-
45  
-class HasManyAssociationsTestForCountDistinctWithFinderSql < ActiveRecord::TestCase
46  
-  class Invoice < ActiveRecord::Base
47  
-    has_many :custom_line_items, :class_name => 'LineItem', :finder_sql => "SELECT DISTINCT line_items.amount from line_items"
48  
-  end
49  
-
50  
-  def test_should_count_distinct_results
51  
-    invoice = Invoice.new
52  
-    invoice.custom_line_items << LineItem.new(:amount => 0)
53  
-    invoice.custom_line_items << LineItem.new(:amount => 0)
54  
-    invoice.save!
55  
-
56  
-    assert_equal 1, invoice.custom_line_items.count
57  
-  end
58  
-end
59  
-
60 23
 class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
61 24
   fixtures :authors, :posts, :comments
62 25
 
@@ -307,37 +270,6 @@ def test_finding_using_primary_key
307 270
     assert_equal "Summit", Firm.scoped(:order => "id").first.clients_using_primary_key.first.name
308 271
   end
309 272
 
310  
-  def test_finding_using_sql
311  
-    firm = Firm.scoped(:order => "id").first
312  
-    first_client = firm.clients_using_sql.first
313  
-    assert_not_nil first_client
314  
-    assert_equal "Microsoft", first_client.name
315  
-    assert_equal 1, firm.clients_using_sql.size
316  
-    assert_equal 1, Firm.scoped(:order => "id").first.clients_using_sql.size
317  
-  end
318  
-
319  
-  def test_finding_using_sql_take_into_account_only_uniq_ids
320  
-    firm = Firm.scoped(:order => "id").first
321  
-    client = firm.clients_using_sql.first
322  
-    assert_equal client, firm.clients_using_sql.find(client.id, client.id)
323  
-    assert_equal client, firm.clients_using_sql.find(client.id, client.id.to_s)
324  
-  end
325  
-
326  
-  def test_counting_using_sql
327  
-    assert_equal 1, Firm.scoped(:order => "id").first.clients_using_counter_sql.size
328  
-    assert Firm.scoped(:order => "id").first.clients_using_counter_sql.any?
329  
-    assert_equal 0, Firm.scoped(:order => "id").first.clients_using_zero_counter_sql.size
330  
-    assert !Firm.scoped(:order => "id").first.clients_using_zero_counter_sql.any?
331  
-  end
332  
-
333  
-  def test_counting_non_existant_items_using_sql
334  
-    assert_equal 0, Firm.scoped(:order => "id").first.no_clients_using_counter_sql.size
335  
-  end
336  
-
337  
-  def test_counting_using_finder_sql
338  
-    assert_equal 2, Firm.find(4).clients_using_sql.count
339  
-  end
340  
-
341 273
   def test_belongs_to_sanity
342 274
     c = Client.new
343 275
     assert_nil c.firm
@@ -365,22 +297,6 @@ def test_find_ids
365 297
     assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
366 298
   end
367 299
 
368  
-  def test_find_string_ids_when_using_finder_sql
369  
-    firm = Firm.scoped(:order => "id").first
370  
-
371  
-    client = firm.clients_using_finder_sql.find("2")
372  
-    assert_kind_of Client, client
373  
-
374  
-    client_ary = firm.clients_using_finder_sql.find(["2"])
375  
-    assert_kind_of Array, client_ary
376  
-    assert_equal client, client_ary.first
377  
-
378  
-    client_ary = firm.clients_using_finder_sql.find("2", "3")
379  
-    assert_kind_of Array, client_ary
380  
-    assert_equal 2, client_ary.size
381  
-    assert client_ary.include?(client)
382  
-  end
383  
-
384 300
   def test_find_all
385 301
     firm = Firm.scoped(:order => "id").first
386 302
     assert_equal 2, firm.clients.scoped(:where => "#{QUOTED_TYPE} = 'Client'").all.length
@@ -1208,13 +1124,6 @@ def test_get_ids_ignores_include_option
1208 1124
     assert_equal [readers(:michael_welcome).id], posts(:welcome).readers_with_person_ids
1209 1125
   end
1210 1126
 
1211  
-  def test_get_ids_for_unloaded_finder_sql_associations_loads_them
1212  
-    company = companies(:first_firm)
1213  
-    assert !company.clients_using_sql.loaded?
1214  
-    assert_equal [companies(:second_client).id], company.clients_using_sql_ids
1215  
-    assert company.clients_using_sql.loaded?
1216  
-  end
1217  
-
1218 1127
   def test_get_ids_for_ordered_association
1219 1128
     assert_equal [companies(:second_client).id, companies(:first_client).id], companies(:first_firm).clients_ordered_by_name_ids
1220 1129
   end
@@ -1275,17 +1184,6 @@ def test_include_checks_if_record_exists_if_target_not_loaded
1275 1184
     assert ! firm.clients.loaded?
1276 1185
   end
1277 1186
 
1278  
-  def test_include_loads_collection_if_target_uses_finder_sql
1279  
-    firm = companies(:first_firm)
1280  
-    client = firm.clients_using_sql.first
1281  
-
1282  
-    firm.reload
1283  
-    assert ! firm.clients_using_sql.loaded?
1284  
-    assert firm.clients_using_sql.include?(client)
1285  
-    assert firm.clients_using_sql.loaded?
1286  
-  end
1287  
-
1288  
-
1289 1187
   def test_include_returns_false_for_non_matching_record_to_verify_scoping
1290 1188
     firm = companies(:first_firm)
1291 1189
     client = Client.create!(:name => 'Not Associated')
4  activerecord/test/cases/reflection_test.rb
@@ -165,8 +165,8 @@ def test_association_reflection_in_modules
165 165
 
166 166
   def test_reflection_of_all_associations
167 167
     # FIXME these assertions bust a lot
168  
-    assert_equal 39, Firm.reflect_on_all_associations.size
169  
-    assert_equal 29, Firm.reflect_on_all_associations(:has_many).size
  168
+    assert_equal 34, Firm.reflect_on_all_associations.size
  169
+    assert_equal 24, Firm.reflect_on_all_associations(:has_many).size
170 170
     assert_equal 10, Firm.reflect_on_all_associations(:has_one).size
171 171
     assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
172 172
   end
15  activerecord/test/models/company.rb
@@ -36,9 +36,7 @@ class Client < ::Company
36 36
 end
37 37
 
38 38
 class Firm < Company
39  
-  has_many :clients, -> { order "id" }, :dependent => :destroy, :counter_sql =>
40  
-      "SELECT COUNT(*) FROM companies WHERE firm_id = 1 " +
41  
-      "AND (#{QUOTED_TYPE} = 'Client' OR #{QUOTED_TYPE} = 'SpecialClient' OR #{QUOTED_TYPE} = 'VerySpecialClient' )",
  39
+  has_many :clients, -> { order "id" }, :dependent => :destroy,
42 40
       :before_remove => :log_before_remove,
43 41
       :after_remove  => :log_after_remove
44 42
   has_many :unsorted_clients, :class_name => "Client"
@@ -53,17 +51,6 @@ class Firm < Company
53 51
   has_many :clients_with_interpolated_conditions, ->(firm) { where "rating > #{firm.rating}" }, :class_name => "Client"
54 52
   has_many :clients_like_ms, -> { where("name = 'Microsoft'").order("id") }, :class_name => "Client"
55 53
   has_many :clients_like_ms_with_hash_conditions, -> { where(:name => 'Microsoft').order("id") }, :class_name => "Client"
56  
-  has_many :clients_using_sql, :class_name => "Client", :finder_sql => proc { "SELECT * FROM companies WHERE client_of = #{id}" }
57  
-  has_many :clients_using_counter_sql, :class_name => "Client",
58  
-           :finder_sql  => proc { "SELECT * FROM companies WHERE client_of = #{id} " },
59  
-           :counter_sql => proc { "SELECT COUNT(*) FROM companies WHERE client_of = #{id}" }
60  
-  has_many :clients_using_zero_counter_sql, :class_name => "Client",
61  
-           :finder_sql  => proc { "SELECT * FROM companies WHERE client_of = #{id}" },
62  
-           :counter_sql => proc { "SELECT 0 FROM companies WHERE client_of = #{id}" }
63  
-  has_many :no_clients_using_counter_sql, :class_name => "Client",
64  
-           :finder_sql  => 'SELECT * FROM companies WHERE client_of = 1000',
65  
-           :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000'
66  
-  has_many :clients_using_finder_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE 1=1'
67 54
   has_many :plain_clients, :class_name => 'Client'
68 55
   has_many :readonly_clients, -> { readonly }, :class_name => 'Client'
69 56
   has_many :clients_using_primary_key, :class_name => 'Client',
1  activerecord/test/models/company_in_module.rb
@@ -11,7 +11,6 @@ class Firm < Company
11 11
       has_many :clients_sorted_desc, -> { order("id DESC") }, :class_name => "Client"
12 12
       has_many :clients_of_firm, -> { order "id" }, :foreign_key => "client_of", :class_name => "Client"
13 13
       has_many :clients_like_ms, -> { where("name = 'Microsoft'").order("id") }, :class_name => "Client"
14  
-      has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
15 14
 
16 15
       has_one :account, :class_name => 'MyApplication::Billing::Account', :dependent => :destroy
17 16
     end
9  activerecord/test/models/project.rb
@@ -7,15 +7,6 @@ class Project < ActiveRecord::Base
7 7
   has_and_belongs_to_many :developers_named_david, -> { where("name = 'David'").uniq }, :class_name => "Developer"
8 8
   has_and_belongs_to_many :developers_named_david_with_hash_conditions, -> { where(:name => 'David').uniq }, :class_name => "Developer"
9 9
   has_and_belongs_to_many :salaried_developers, -> { where "salary > 0" }, :class_name => "Developer"
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_multiline_finder_sql, :class_name => "Developer", :finder_sql => proc {
12  
-    "SELECT
13  
-       t.*, j.*
14  
-     FROM
15  
-       developers_projects j,
16  
-       developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id"
17  
-  }
18  
-  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}" }
19 10
   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>'}"},
20 11
                             :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || '<new>'}"},
21 12
                             :before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"},

0 notes on commit 3803fcc

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