Skip to content

Commit

Permalink
drying up more code in associations.rb
Browse files Browse the repository at this point in the history
Signed-off-by: wycats <wycats@gmail.com>
  • Loading branch information
tenderlove authored and wycats committed Mar 7, 2010
1 parent 939291e commit a424f19
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -1561,24 +1561,18 @@ def configure_dependency_for_has_one(reflection)

def configure_dependency_for_belongs_to(reflection)
if reflection.options.include?(:dependent)
case reflection.options[:dependent]
when :destroy
method_name = "belongs_to_dependent_destroy_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.destroy unless association.nil?
end
after_destroy method_name
when :delete
method_name = "belongs_to_dependent_delete_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.delete unless association.nil?
end
after_destroy method_name
else
raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{reflection.options[:dependent].inspect})"
name = reflection.options[:dependent]

unless [:destroy, :delete].include?(name)
raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{reflection.options[:dependent].inspect})"
end

method_name = :"belongs_to_dependent_#{name}_for_#{reflection.name}"
define_method(method_name) do
association = send(reflection.name)
association.destroy unless association.nil?
end
after_destroy method_name
end
end

Expand Down

0 comments on commit a424f19

Please sign in to comment.