Permalink
Browse files

Fix that ActiveRecord would create attribute methods and override cus…

…tom attribute getters if the method is also defined in Kernel.methods. [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7749 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 24c2457 commit 5b2e8b1eb1731e987faa233dc03e83c221cfcda5 @technoweenie technoweenie committed Oct 6, 2007
View
@@ -1,5 +1,7 @@
*SVN*
+* Fix that ActiveRecord would create attribute methods and override custom attribute getters if the method is also defined in Kernel.methods. [Rick]
+
* Don't call attr_readonly on polymorphic belongs_to associations, in case it matches the name of some other non-ActiveRecord class/module. [Rick]
* Try loading activerecord-<adaptername>-adapter gem before trying a plain require so you can use custom gems for the bundled adapters. Also stops gems from requiring an adapter from an old Active Record gem. [Jeremy Kemper, Derrick Spell]
@@ -45,8 +47,7 @@
* Deprecation: remove deprecated threaded_connections methods. Use allow_concurrency instead. [Jeremy Kemper]
-* Associations macros accept extension blocks alongside modules. #9346 [Josh
-Peek]
+* Associations macros accept extension blocks alongside modules. #9346 [Josh Peek]
* Speed up and simplify query caching. [Jeremy Kemper]
@@ -76,20 +76,18 @@ def define_attribute_methods
end
end
+ # Check to see if the method is defined in the model or any of it's subclasses that also derive from ActiveRecord.
+ # Raise DangerousAttributeError if the method is defined by ActiveRecord though.
def instance_method_already_implemented?(method_name)
- if method_defined?(method_name) || private_method_defined?(method_name) || protected_method_defined?(method_name)
- # method is defined but maybe its a simple Kernel:: method which we could simply override
- @@_overrideable_kernel_methods ||= Set.new(Kernel.methods)
- !@@_overrideable_kernel_methods.include?(method_name)
- else
- false
- end
+ return true if method_name =~ /^id(=$|\?$|$)/
+ @_defined_class_methods ||= Set.new(ancestors.first(ancestors.index(ActiveRecord::Base)).collect! { |m| m.public_instance_methods(false) | m.private_instance_methods(false) | m.protected_instance_methods(false) }.flatten)
+ @@_defined_activerecord_methods ||= Set.new(ActiveRecord::Base.public_instance_methods(false) | ActiveRecord::Base.private_instance_methods(false) | ActiveRecord::Base.protected_instance_methods(false))
+ raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name)
+ @_defined_class_methods.include?(method_name)
end
alias :define_read_methods :define_attribute_methods
-
-
private
# Suffixes a, ?, c become regexp /(a|\?|c)$/
def rebuild_attribute_method_regexp
@@ -29,11 +29,14 @@ class PreparedStatementInvalid < ActiveRecordError #:nodoc:
end
class StaleObjectError < ActiveRecordError #:nodoc:
end
- class ConfigurationError < StandardError #:nodoc:
+ class ConfigurationError < ActiveRecordError #:nodoc:
end
- class ReadOnlyRecord < StandardError #:nodoc:
+ class ReadOnlyRecord < ActiveRecordError #:nodoc:
end
- class Rollback < StandardError #:nodoc:
+ class Rollback < ActiveRecordError #:nodoc:
+ end
+
+ class DangerousAttributeError < ActiveRecordError #:nodoc:
end
# Raised when you've tried to access a column, which wasn't
@@ -53,6 +53,43 @@ def test_should_unserialize_attributes_for_frozen_records
topic = Topic.create("content" => myobj)
topic.freeze
assert_equal myobj, topic.content
-
+ end
+
+ def test_kernel_methods_not_implemented_in_activerecord
+ %w(test name display y).each do |method|
+ assert_equal false, ActiveRecord::Base.instance_method_already_implemented?(method), "##{method} is defined"
+ end
+ end
+
+ def test_primary_key_implemented
+ assert_equal true, Class.new(ActiveRecord::Base).instance_method_already_implemented?('id')
+ end
+
+ def test_defined_kernel_methods_implemented_in_model
+ %w(test name display y).each do |method|
+ klass = Class.new ActiveRecord::Base
+ klass.class_eval "def #{method}() 'defined #{method}' end"
+ assert_equal true, klass.instance_method_already_implemented?(method), "##{method} is not defined"
+ end
+ end
+
+ def test_defined_kernel_methods_implemented_in_model_abstract_subclass
+ %w(test name display y).each do |method|
+ abstract = Class.new ActiveRecord::Base
+ abstract.class_eval "def #{method}() 'defined #{method}' end"
+ abstract.abstract_class = true
+ klass = Class.new abstract
+ assert_equal true, klass.instance_method_already_implemented?(method), "##{method} is not defined"
+ end
+ end
+
+ def test_raises_dangerous_attribute_error_when_defining_activerecord_method_in_model
+ %w(save create_or_update).each do |method|
+ klass = Class.new ActiveRecord::Base
+ klass.class_eval "def #{method}() 'defined #{method}' end"
+ assert_raises ActiveRecord::DangerousAttributeError do
+ klass.instance_method_already_implemented?(method)
+ end
+ end
end
end

0 comments on commit 5b2e8b1

Please sign in to comment.