Skip to content
Browse files

added docs to alias_attribute method

  • Loading branch information...
1 parent 2805c28 commit 4831a895c445f650c9daf0b445136ceb653f781b Francesco Rodriguez committed
Showing with 31 additions and 0 deletions.
  1. +31 −0 activemodel/lib/active_model/attribute_methods.rb
View
31 activemodel/lib/active_model/attribute_methods.rb
@@ -180,6 +180,37 @@ def attribute_method_affix(*affixes)
undefine_attribute_methods
end
+
+ # Allows you to make aliases for attributes.
+ #
+ # For example:
+ #
+ # class Person
+ #
+ # include ActiveModel::AttributeMethods
+ # attr_accessor :name
+ # attribute_method_prefix 'clear_'
+ #
+ # define_attribute_methods [:name]
+ #
+ # private
+ #
+ # def clear_attribute(attr)
+ # send("#{attr}=", nil)
+ # end
+ # end
+ #
+ # class Person
+ # attr_accessor :nickname
+ #
+ # alias_attribute :nickname, :name
+ # end
+ #
+ # person = Person.new
+ # person.nickname = "Bob"
+ # person.nickname # => "Bob"
+ # person.clear_nickname
+ # person.nickname # => nil
def alias_attribute(new_name, old_name)
attribute_method_matchers.each do |matcher|
matcher_new = matcher.method_name(new_name).to_s

0 comments on commit 4831a89

Please sign in to comment.
Something went wrong with that request. Please try again.