aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-09-13 23:23:29 +0100
committerJon Leighton <j@jonathanleighton.com>2011-09-14 00:00:37 +0100
commit55da28dd2fa734de256a13fb09469eaa3ab15599 (patch)
treed432392646475cdf303e684b94e876c749ddedea /activerecord/lib/active_record/attribute_methods.rb
parent82afaa06861d03f6a2887d40e40c0ded76db88db (diff)
downloadrails-55da28dd2fa734de256a13fb09469eaa3ab15599.tar.gz
rails-55da28dd2fa734de256a13fb09469eaa3ab15599.tar.bz2
rails-55da28dd2fa734de256a13fb09469eaa3ab15599.zip
We don't need to build a set for DangerousAttributeError.
We can just use method_defined? and private_method_defined?
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb20
1 files changed, 13 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index dc6dc2e63a..0b074da0a5 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -42,23 +42,29 @@ module ActiveRecord
# 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