Permalink
Browse files

avoiding a few closure references by evaling. [#4223 state:resolved]

Signed-off-by: wycats <wycats@gmail.com>
  • Loading branch information...
tenderlove authored and wycats committed Mar 18, 2010
1 parent 753304b commit 3172db12e4e7f6abab8ddd25f7911d29a5b65ea0
Showing with 18 additions and 12 deletions.
  1. +18 −12 activerecord/lib/active_record/associations.rb
@@ -1545,15 +1545,19 @@ def configure_dependency_for_has_one(reflection)
case name
when :destroy, :delete
- define_method(method_name) do
- association = send(reflection.name)
- association.send(name) if association
- end
+ class_eval <<-eoruby, __FILE__, __LINE__ + 1
+ def #{method_name}
+ association = #{reflection.name}
+ association.#{name} if association
+ end
+ eoruby
when :nullify
- define_method(method_name) do
- association = send(reflection.name)
- association.update_attribute(reflection.primary_key_name, nil) if association
- end
+ class_eval <<-eoruby, __FILE__, __LINE__ + 1
+ def #{method_name}
+ association = #{reflection.name}
+ association.update_attribute(#{reflection.primary_key_name.inspect}, nil) if association
+ end
+ eoruby
else
raise ArgumentError, "The :dependent option expects either :destroy, :delete or :nullify (#{reflection.options[:dependent].inspect})"
end
@@ -1571,10 +1575,12 @@ def configure_dependency_for_belongs_to(reflection)
end
method_name = :"belongs_to_dependent_#{name}_for_#{reflection.name}"
- define_method(method_name) do
- association = send(reflection.name)
- association.send(name) if association
- end
+ class_eval <<-eoruby, __FILE__, __LINE__ + 1
+ def #{method_name}
+ association = #{reflection.name}
+ association.#{name} if association
+ end
+ eoruby
after_destroy method_name
end
end

0 comments on commit 3172db1

Please sign in to comment.