Skip to content
This repository
Browse code

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]
  • Loading branch information...
commit 72b772ae9b692add0359574b0da7038bd1420a5a 1 parent 487758b
Hongli Lai FooBarWidget authored NZKoz committed

Showing 1 changed file with 34 additions and 3 deletions. Show diff stats Hide diff stats

  1. +34 3 activerecord/lib/active_record/associations.rb
37 activerecord/lib/active_record/associations.rb
@@ -1428,15 +1428,23 @@ def find_with_associations(options = {})
1428 1428 []
1429 1429 end
1430 1430
  1431 + # Creates before_destroy callback methods that nullify, delete or destroy
  1432 + # has_many associated objects, according to the defined :dependent rule.
  1433 + #
1431 1434 # See HasManyAssociation#delete_records. Dependent associations
1432 1435 # delete children, otherwise foreign key is set to NULL.
1433   - def configure_dependency_for_has_many(reflection)
  1436 + #
  1437 + # The +extra_conditions+ parameter, which is not used within the main
  1438 + # Active Record codebase, is meant to allow plugins to define extra
  1439 + # finder conditions.
  1440 + def configure_dependency_for_has_many(reflection, extra_conditions = nil)
1434 1441 if reflection.options.include?(:dependent)
1435 1442 # Add polymorphic type if the :as option is present
1436 1443 dependent_conditions = []
1437 1444 dependent_conditions << "#{reflection.primary_key_name} = \#{record.quoted_id}"
1438 1445 dependent_conditions << "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as]
1439 1446 dependent_conditions << sanitize_sql(reflection.options[:conditions]) if reflection.options[:conditions]
  1447 + dependent_conditions << extra_conditions if extra_conditions
1440 1448 dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
1441 1449
1442 1450 case reflection.options[:dependent]
@@ -1447,9 +1455,24 @@ def configure_dependency_for_has_many(reflection)
1447 1455 end
1448 1456 before_destroy method_name
1449 1457 when :delete_all
1450   - module_eval "before_destroy { |record| #{reflection.class_name}.delete_all(%(#{dependent_conditions})) }"
  1458 + module_eval %Q{
  1459 + before_destroy do |record|
  1460 + delete_all_has_many_dependencies(record,
  1461 + "#{reflection.name}",
  1462 + #{reflection.class_name},
  1463 + "#{dependent_conditions}")
  1464 + end
  1465 + }
1451 1466 when :nullify
1452   - module_eval "before_destroy { |record| #{reflection.class_name}.update_all(%(#{reflection.primary_key_name} = NULL), %(#{dependent_conditions})) }"
  1467 + module_eval %Q{
  1468 + before_destroy do |record|
  1469 + nullify_has_many_dependencies(record,
  1470 + "#{reflection.name}",
  1471 + #{reflection.class_name},
  1472 + "#{reflection.primary_key_name}",
  1473 + "#{dependent_conditions}")
  1474 + end
  1475 + }
1453 1476 else
1454 1477 raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, or :nullify (#{reflection.options[:dependent].inspect})"
1455 1478 end
@@ -1509,6 +1532,14 @@ def configure_dependency_for_belongs_to(reflection)
1509 1532 end
1510 1533 end
1511 1534
  1535 + def delete_all_has_many_dependencies(record, reflection_name, association_class, dependent_conditions)
  1536 + association_class.delete_all(dependent_conditions)
  1537 + end
  1538 +
  1539 + def nullify_has_many_dependencies(record, reflection_name, association_class, primary_key_name, dependent_conditions)
  1540 + association_class.update_all("#{primary_key_name} = NULL", dependent_conditions)
  1541 + end
  1542 +
1512 1543 mattr_accessor :valid_keys_for_has_many_association
1513 1544 @@valid_keys_for_has_many_association = [
1514 1545 :class_name, :table_name, :foreign_key, :primary_key,

0 comments on commit 72b772a

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