Browse files

We don't need to build a set for DangerousAttributeError.

We can just use method_defined? and private_method_defined?
  • Loading branch information...
1 parent 82afaa0 commit 55da28dd2fa734de256a13fb09469eaa3ab15599 @jonleighton jonleighton committed Sep 13, 2011
Showing with 13 additions and 7 deletions.
  1. +13 −7 activerecord/lib/active_record/attribute_methods.rb
View
20 activerecord/lib/active_record/attribute_methods.rb
@@ -42,23 +42,29 @@ def undefine_attribute_methods(*args)
# that also derive from Active Record. Raises DangerousAttributeError if the
# method is defined by Active Record though.
def instance_method_already_implemented?(method_name)
+ if dangerous_attribute_method?(method_name)
+ raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord"
+ end
+
method_name = method_name.to_s
index = ancestors.index(ActiveRecord::Base) || ancestors.length
@_defined_class_methods ||= ancestors.first(index).map { |m|
m.instance_methods(false) | m.private_instance_methods(false)
}.flatten.map {|m| m.to_s }.to_set
- @@_defined_activerecord_methods ||= defined_activerecord_methods
- raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name)
@_defined_class_methods.include?(method_name) || generated_attribute_methods.method_defined?(method_name)
end
- def defined_activerecord_methods
+ # A method name is 'dangerous' if it is already defined by Active Record, but
+ # not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
+ def dangerous_attribute_method?(method_name)
active_record = ActiveRecord::Base
- super_klass = ActiveRecord::Base.superclass
- methods = (active_record.instance_methods - super_klass.instance_methods) +
- (active_record.private_instance_methods - super_klass.private_instance_methods)
- methods.map {|m| m.to_s }.to_set
+ superclass = ActiveRecord::Base.superclass
+
+ (active_record.method_defined?(method_name) ||
+ active_record.private_method_defined?(method_name)) &&
+ !superclass.method_defined?(method_name) &&
+ !superclass.private_method_defined?(method_name)
end
end

0 comments on commit 55da28d

Please sign in to comment.