diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-11-29 20:10:33 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-11-29 20:13:37 +0000 |
commit | 8df787d42890017f182c1ac6cb082317c255a456 (patch) | |
tree | 55f662b09ad6d8e941999f3a6f98011044d413f0 /activemodel/lib | |
parent | f3c84dc31692204aacac3c125dcfcc986fd961a0 (diff) | |
download | rails-8df787d42890017f182c1ac6cb082317c255a456.tar.gz rails-8df787d42890017f182c1ac6cb082317c255a456.tar.bz2 rails-8df787d42890017f182c1ac6cb082317c255a456.zip |
Deprecated `define_attr_method` in `ActiveModel::AttributeMethods`
This only existed to support methods like `set_table_name` in Active
Record, which are themselves being deprecated.
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model/attribute_methods.rb | 57 |
1 files changed, 20 insertions, 37 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index e69cb5c459..dba059eacd 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -66,46 +66,29 @@ module ActiveModel end module ClassMethods - # Defines an "attribute" method (like +inheritance_column+ or +table_name+). - # A new (class) method will be created with the given name. If a value is - # specified, the new method will return that value (as a string). - # Otherwise, the given block will be used to compute the value of the - # method. - # - # The original method will be aliased, with the new name being prefixed - # with "original_". This allows the new method to access the original - # value. - # - # Example: - # - # class Person - # - # include ActiveModel::AttributeMethods - # - # cattr_accessor :primary_key - # cattr_accessor :inheritance_column - # - # define_attr_method :primary_key, "sysid" - # define_attr_method( :inheritance_column ) do - # original_inheritance_column + "_id" - # end - # - # end - # - # Provides you with: - # - # Person.primary_key - # # => "sysid" - # Person.inheritance_column = 'address' - # Person.inheritance_column - # # => 'address_id' - def define_attr_method(name, value=nil, &block) + def define_attr_method(name, value=nil, deprecation_warning = true, &block) #:nodoc: + # This deprecation_warning param is for internal use so that we can silence + # the warning from Active Record, because we are implementing more specific + # messages there instead. + # + # It doesn't apply to the original_#{name} method as we want to warn if + # people are calling that regardless. + if deprecation_warning + ActiveSupport::Deprecation.warn("define_attr_method is deprecated and will be removed without replacement.") + end + sing = singleton_class sing.class_eval <<-eorb, __FILE__, __LINE__ + 1 - if method_defined?('original_#{name}') - undef :'original_#{name}' + remove_possible_method :'original_#{name}' + remove_possible_method :'_original_#{name}' + alias_method :'_original_#{name}', :'#{name}' + define_method :'original_#{name}' do + ActiveSupport::Deprecation.warn( + "This method is generated by ActiveModel::AttributeMethods::ClassMethods#define_attr_method, " \ + "which is deprecated and will be removed." + ) + send(:'_original_#{name}') end - alias_method :'original_#{name}', :'#{name}' eorb if block_given? sing.send :define_method, name, &block |