mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
added docs to alias_attribute method
This commit is contained in:
parent
2805c28e3e
commit
4831a895c4
1 changed files with 31 additions and 0 deletions
|
@ -180,6 +180,37 @@ module ActiveModel
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue