Module: Mongoid::Attributes::ClassMethods

Defined in:
build/mongoid-6.1/lib/mongoid/attributes.rb

Instance Method Summary collapse

Instance Method Details

#alias_attribute(name, original) ⇒ Object

Alias the provided name to the original field. This will provide an aliased getter, setter, existence check, and all dirty attribute methods.

Examples:

Alias the attribute.

class Product
  include Mongoid::Document
  field :price, :type => Float
  alias_attribute :cost, :price
end

Parameters:

  • name (Symbol)

    The new name.

  • original (Symbol)

    The original name.

Since:

  • 2.3.0



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'build/mongoid-6.1/lib/mongoid/attributes.rb', line 312

def alias_attribute(name, original)
  aliased_fields[name.to_s] = original.to_s
  class_eval <<-RUBY
    alias #{name}  #{original}
    alias #{name}= #{original}=
    alias #{name}? #{original}?
    alias #{name}_change   #{original}_change
    alias #{name}_changed? #{original}_changed?
    alias reset_#{name}!   reset_#{original}!
    alias reset_#{name}_to_default!   reset_#{original}_to_default!
    alias #{name}_was      #{original}_was
    alias #{name}_will_change! #{original}_will_change!
    alias #{name}_before_type_cast #{original}_before_type_cast
  RUBY
end