Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

deleting repeated code

Signed-off-by: wycats <wycats@gmail.com>
  • Loading branch information...
commit 939291e8f2b68e11cde12f5f08886f8fca3ef2b2 1 parent 4bc2cbc
@tenderlove tenderlove authored wycats committed
Showing with 7 additions and 17 deletions.
  1. +7 −17 activerecord/lib/active_record/associations.rb
View
24 activerecord/lib/active_record/associations.rb
@@ -1537,35 +1537,25 @@ def configure_dependency_for_has_many(reflection, extra_conditions = nil)
# has_one associated objects, according to the defined :dependent rule.
def configure_dependency_for_has_one(reflection)
if reflection.options.include?(:dependent)
- case reflection.options[:dependent]
- when :destroy
- method_name = "has_one_dependent_destroy_for_#{reflection.name}".to_sym
+ name = reflection.options[:dependent]
+ method_name = :"has_one_dependent_#{name}_for_#{reflection.name}"
+
+ case name
+ when :destroy, :delete
define_method(method_name) do
association = send(reflection.name)
association.destroy unless association.nil?
end
- before_destroy method_name
- when :delete
- method_name = "has_one_dependent_delete_for_#{reflection.name}".to_sym
- define_method(method_name) do
- # Retrieve the associated object and delete it. The retrieval
- # is necessary because there may be multiple associated objects
- # with foreign keys pointing to this object, and we only want
- # to delete the correct one, not all of them.
- association = send(reflection.name)
- association.delete unless association.nil?
- end
- before_destroy method_name
when :nullify
- method_name = "has_one_dependent_nullify_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.update_attribute(reflection.primary_key_name, nil) unless association.nil?
end
- before_destroy method_name
else
raise ArgumentError, "The :dependent option expects either :destroy, :delete or :nullify (#{reflection.options[:dependent].inspect})"
end
+
+ before_destroy method_name
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.