mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Deprecate using method_missing for attributes that are columns.
This shouldn't ever happen unless people are doing something particularly weird, but adding a deprecation in case there are bugs not caught by our tests.
This commit is contained in:
parent
c89e1c7bde
commit
1a421ceccb
2 changed files with 30 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
require 'active_support/core_ext/enumerable'
|
require 'active_support/core_ext/enumerable'
|
||||||
|
require 'active_support/deprecation'
|
||||||
|
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
# = Active Record Attribute Methods
|
# = Active Record Attribute Methods
|
||||||
|
@ -77,6 +78,20 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def attribute_missing(match, *args, &block)
|
||||||
|
if self.class.columns_hash[match.attr_name]
|
||||||
|
ActiveSupport::Deprecation.warn(
|
||||||
|
"The method `#{match.method_name}', matching the attribute `#{match.attr_name}' has " \
|
||||||
|
"dispatched through method_missing. This shouldn't happen, because `#{match.attr_name}' " \
|
||||||
|
"is a column of the table. If this error has happened through normal usage of Active " \
|
||||||
|
"Record (rather than through your own code or external libraries), please report it as " \
|
||||||
|
"a bug."
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
def respond_to?(name, include_private = false)
|
def respond_to?(name, include_private = false)
|
||||||
self.class.define_attribute_methods unless self.class.attribute_methods_generated?
|
self.class.define_attribute_methods unless self.class.attribute_methods_generated?
|
||||||
super
|
super
|
||||||
|
|
|
@ -675,6 +675,21 @@ class AttributeMethodsTest < ActiveRecord::TestCase
|
||||||
assert subklass.method_defined?(:id), "subklass is missing id method"
|
assert subklass.method_defined?(:id), "subklass is missing id method"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_dispatching_column_attributes_through_method_missing_deprecated
|
||||||
|
Topic.define_attribute_methods
|
||||||
|
|
||||||
|
topic = Topic.new(:id => 5)
|
||||||
|
topic.id = 5
|
||||||
|
|
||||||
|
topic.method(:id).owner.send(:remove_method, :id)
|
||||||
|
|
||||||
|
assert_deprecated do
|
||||||
|
assert_equal 5, topic.id
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
Topic.undefine_attribute_methods
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def cached_columns
|
def cached_columns
|
||||||
@cached_columns ||= (time_related_columns_on_topic + serialized_columns_on_topic).map(&:name)
|
@cached_columns ||= (time_related_columns_on_topic + serialized_columns_on_topic).map(&:name)
|
||||||
|
|
Loading…
Reference in a new issue