提交 3a40d386 编写于 作者: J Jon Leighton

Get rid of the underscore versions of attribute methods!

This makes me happy!
上级 3dcb1271
......@@ -93,6 +93,7 @@ module AttributeMethods
autoload :TimeZoneConversion
autoload :Write
autoload :Serialization
autoload :DeprecatedUnderscoreRead
end
end
......
require 'active_support/concern'
require 'active_support/deprecation'
module ActiveRecord
module AttributeMethods
module DeprecatedUnderscoreRead
extend ActiveSupport::Concern
included do
attribute_method_prefix "_"
end
module ClassMethods
protected
def define_method__attribute(attr_name)
# Do nothing, let it hit method missing instead.
end
end
protected
def _attribute(attr_name)
ActiveSupport::Deprecation.warn(
"You have called '_#{attr_name}'. This is deprecated. Please use " \
"either '#{attr_name}' or read_attribute('#{attr_name}')."
)
read_attribute(attr_name)
end
end
end
end
......@@ -13,7 +13,6 @@ def to_key
def id
read_attribute(self.class.primary_key)
end
alias _id id
# Sets the primary key value
def id=(value)
......@@ -27,7 +26,7 @@ def id?
module ClassMethods
def dangerous_attribute_method?(method_name)
super && !['id', 'id=', 'id?', '_id'].include?(method_name)
super && !['id', 'id=', 'id?'].include?(method_name)
end
# Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the
......
......@@ -61,8 +61,6 @@ def #{attr_name}
def self.cast_#{attr_name}(v)
#{cast_code}
end
alias _#{attr_name} #{attr_name}
STR
else
generated_attribute_methods.module_eval do
......@@ -73,8 +71,6 @@ def self.cast_#{attr_name}(v)
singleton_class.send(:define_method, "cast_#{attr_name}") do |v|
eval(cast_code)
end
alias_method("_#{attr_name}", attr_name)
end
end
end
......
......@@ -2200,6 +2200,7 @@ def populate_with_current_scope_attributes
include AttributeMethods::TimeZoneConversion
include AttributeMethods::Dirty
include AttributeMethods::Serialization
include AttributeMethods::DeprecatedUnderscoreRead
include ActiveModel::MassAssignmentSecurity
include Callbacks, ActiveModel::Observing, Timestamp
include Associations, NamedScope
......
......@@ -114,6 +114,11 @@ def test_respond_to?
assert !topic.respond_to?(:nothingness)
end
def test_deprecated_underscore_method
topic = Topic.find(1)
assert_equal topic.title, assert_deprecated { topic._title }
end
def test_respond_to_with_custom_primary_key
keyboard = Keyboard.create
assert_not_nil keyboard.key_number
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册