aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorHongli Lai (Phusion) <hongli@phusion.nl>2008-09-21 23:51:02 +0200
committerMichael Koziarski <michael@koziarski.com>2008-09-24 13:27:39 +0200
commit72b772ae9b692add0359574b0da7038bd1420a5a (patch)
treec02e495394b826cdb1c700c0c58dcc3bee4e671f /activerecord
parent487758b3b88a38da3a75900839aea03774904fe1 (diff)
downloadrails-72b772ae9b692add0359574b0da7038bd1420a5a.tar.gz
rails-72b772ae9b692add0359574b0da7038bd1420a5a.tar.bz2
rails-72b772ae9b692add0359574b0da7038bd1420a5a.zip
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]
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb37
1 files changed, 34 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 6f4be9391b..3f8ec4d09c 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1428,15 +1428,23 @@ module ActiveRecord
[]
end
+ # Creates before_destroy callback methods that nullify, delete or destroy
+ # has_many associated objects, according to the defined :dependent rule.
+ #
# See HasManyAssociation#delete_records. Dependent associations
# delete children, otherwise foreign key is set to NULL.
- def configure_dependency_for_has_many(reflection)
+ #
+ # The +extra_conditions+ parameter, which is not used within the main
+ # Active Record codebase, is meant to allow plugins to define extra
+ # finder conditions.
+ def configure_dependency_for_has_many(reflection, extra_conditions = nil)
if reflection.options.include?(:dependent)
# Add polymorphic type if the :as option is present
dependent_conditions = []
dependent_conditions << "#{reflection.primary_key_name} = \#{record.quoted_id}"
dependent_conditions << "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as]
dependent_conditions << sanitize_sql(reflection.options[:conditions]) if reflection.options[:conditions]
+ dependent_conditions << extra_conditions if extra_conditions
dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
case reflection.options[:dependent]
@@ -1447,9 +1455,24 @@ module ActiveRecord
end
before_destroy method_name
when :delete_all
- module_eval "before_destroy { |record| #{reflection.class_name}.delete_all(%(#{dependent_conditions})) }"
+ module_eval %Q{
+ before_destroy do |record|
+ delete_all_has_many_dependencies(record,
+ "#{reflection.name}",
+ #{reflection.class_name},
+ "#{dependent_conditions}")
+ end
+ }
when :nullify
- module_eval "before_destroy { |record| #{reflection.class_name}.update_all(%(#{reflection.primary_key_name} = NULL), %(#{dependent_conditions})) }"
+ module_eval %Q{
+ before_destroy do |record|
+ nullify_has_many_dependencies(record,
+ "#{reflection.name}",
+ #{reflection.class_name},
+ "#{reflection.primary_key_name}",
+ "#{dependent_conditions}")
+ end
+ }
else
raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, or :nullify (#{reflection.options[:dependent].inspect})"
end
@@ -1509,6 +1532,14 @@ module ActiveRecord
end
end
+ def delete_all_has_many_dependencies(record, reflection_name, association_class, dependent_conditions)
+ association_class.delete_all(dependent_conditions)
+ end
+
+ def nullify_has_many_dependencies(record, reflection_name, association_class, primary_key_name, dependent_conditions)
+ association_class.update_all("#{primary_key_name} = NULL", dependent_conditions)
+ end
+
mattr_accessor :valid_keys_for_has_many_association
@@valid_keys_for_has_many_association = [
:class_name, :table_name, :foreign_key, :primary_key,