public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Refactor configure_dependency_for_has_many to use a few more methods.

Add an additional conditions option to make it slightly easier for certain 
plugins.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1087 state:committed]
Hongli Lai (Phusion) (author)
Sun Sep 21 14:51:02 -0700 2008
NZKoz (committer)
Wed Sep 24 04:27:39 -0700 2008
commit  72b772ae9b692add0359574b0da7038bd1420a5a
tree    c02e495394b826cdb1c700c0c58dcc3bee4e671f
parent  487758b3b88a38da3a75900839aea03774904fe1
...
1428
1429
1430
 
 
 
1431
1432
1433
 
 
 
 
 
1434
1435
1436
1437
1438
1439
 
1440
1441
1442
...
1447
1448
1449
1450
 
 
 
 
 
 
 
 
1451
1452
 
 
 
 
 
 
 
 
 
1453
1454
1455
...
1509
1510
1511
 
 
 
 
 
 
 
 
1512
1513
1514
...
1428
1429
1430
1431
1432
1433
1434
1435
 
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
...
1455
1456
1457
 
1458
1459
1460
1461
1462
1463
1464
1465
1466
 
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
...
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
0
@@ -1428,15 +1428,23 @@ module ActiveRecord
0
           []
0
         end
0
 
0
+        # Creates before_destroy callback methods that nullify, delete or destroy
0
+        # has_many associated objects, according to the defined :dependent rule.
0
+        #
0
         # See HasManyAssociation#delete_records.  Dependent associations
0
         # delete children, otherwise foreign key is set to NULL.
0
-        def configure_dependency_for_has_many(reflection)
0
+        #
0
+        # The +extra_conditions+ parameter, which is not used within the main
0
+        # Active Record codebase, is meant to allow plugins to define extra
0
+        # finder conditions.
0
+        def configure_dependency_for_has_many(reflection, extra_conditions = nil)
0
           if reflection.options.include?(:dependent)
0
             # Add polymorphic type if the :as option is present
0
             dependent_conditions = []
0
             dependent_conditions << "#{reflection.primary_key_name} = \#{record.quoted_id}"
0
             dependent_conditions << "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as]
0
             dependent_conditions << sanitize_sql(reflection.options[:conditions]) if reflection.options[:conditions]
0
+            dependent_conditions << extra_conditions if extra_conditions
0
             dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
0
 
0
             case reflection.options[:dependent]
0
@@ -1447,9 +1455,24 @@ module ActiveRecord
0
                 end
0
                 before_destroy method_name
0
               when :delete_all
0
-                module_eval "before_destroy { |record| #{reflection.class_name}.delete_all(%(#{dependent_conditions})) }"
0
+                module_eval %Q{
0
+                  before_destroy do |record|
0
+                    delete_all_has_many_dependencies(record,
0
+                      "#{reflection.name}",
0
+                      #{reflection.class_name},
0
+                      "#{dependent_conditions}")
0
+                  end
0
+                }
0
               when :nullify
0
-                module_eval "before_destroy { |record| #{reflection.class_name}.update_all(%(#{reflection.primary_key_name} = NULL),  %(#{dependent_conditions})) }"
0
+                module_eval %Q{
0
+                  before_destroy do |record|
0
+                    nullify_has_many_dependencies(record,
0
+                      "#{reflection.name}",
0
+                      #{reflection.class_name},
0
+                      "#{reflection.primary_key_name}",
0
+                      "#{dependent_conditions}")
0
+                  end
0
+                }
0
               else
0
                 raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, or :nullify (#{reflection.options[:dependent].inspect})"
0
             end
0
@@ -1509,6 +1532,14 @@ module ActiveRecord
0
           end
0
         end
0
 
0
+        def delete_all_has_many_dependencies(record, reflection_name, association_class, dependent_conditions)
0
+          association_class.delete_all(dependent_conditions)
0
+        end
0
+
0
+        def nullify_has_many_dependencies(record, reflection_name, association_class, primary_key_name, dependent_conditions)
0
+          association_class.update_all("#{primary_key_name} = NULL", dependent_conditions)
0
+        end
0
+
0
         mattr_accessor :valid_keys_for_has_many_association
0
         @@valid_keys_for_has_many_association = [
0
           :class_name, :table_name, :foreign_key, :primary_key,

Comments