diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-12-01 11:24:30 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-12-01 23:41:51 +0000 |
commit | 3a40d386194cf6d572b145cea0da57c286204554 (patch) | |
tree | 2d8d28049fa7baef3b0dd440dcbcde315c4ed6bb /activerecord | |
parent | 3dcb127109428c1948a9f3bcad7101bd8a7f4d8a (diff) | |
download | rails-3a40d386194cf6d572b145cea0da57c286204554.tar.gz rails-3a40d386194cf6d572b145cea0da57c286204554.tar.bz2 rails-3a40d386194cf6d572b145cea0da57c286204554.zip |
Get rid of the underscore versions of attribute methods!
This makes me happy!
Diffstat (limited to 'activerecord')
6 files changed, 40 insertions, 6 deletions
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index bf28cffbbe..978e415e58 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -93,6 +93,7 @@ module ActiveRecord autoload :TimeZoneConversion autoload :Write autoload :Serialization + autoload :DeprecatedUnderscoreRead end end diff --git a/activerecord/lib/active_record/attribute_methods/deprecated_underscore_read.rb b/activerecord/lib/active_record/attribute_methods/deprecated_underscore_read.rb new file mode 100644 index 0000000000..0eb0db65b1 --- /dev/null +++ b/activerecord/lib/active_record/attribute_methods/deprecated_underscore_read.rb @@ -0,0 +1,32 @@ +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 diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index 6eff716703..98aa1ed225 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -13,7 +13,6 @@ module ActiveRecord def id read_attribute(self.class.primary_key) end - alias _id id # Sets the primary key value def id=(value) @@ -27,7 +26,7 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 59e57135f9..8eec06302a 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -61,8 +61,6 @@ module ActiveRecord 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 @@ module ActiveRecord singleton_class.send(:define_method, "cast_#{attr_name}") do |v| eval(cast_code) end - - alias_method("_#{attr_name}", attr_name) end end end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 76aa121ade..6ba571a632 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2200,6 +2200,7 @@ MSG include AttributeMethods::TimeZoneConversion include AttributeMethods::Dirty include AttributeMethods::Serialization + include AttributeMethods::DeprecatedUnderscoreRead include ActiveModel::MassAssignmentSecurity include Callbacks, ActiveModel::Observing, Timestamp include Associations, NamedScope diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 17cf1ec907..a7cad329e8 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -114,6 +114,11 @@ class AttributeMethodsTest < ActiveRecord::TestCase 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 |