Module: Mongoid::Attributes

Extended by:
ActiveSupport::Concern
Includes:
Nested, Processing, Readonly
Included in:
Composable
Defined in:
lib/mongoid/attributes.rb,
lib/mongoid/attributes/nested.rb,
lib/mongoid/attributes/dynamic.rb,
lib/mongoid/attributes/readonly.rb,
lib/mongoid/attributes/processing.rb

Overview

This module contains the logic for handling the internal attributes hash, and how to get and set values.

Defined Under Namespace

Modules: ClassMethods, Dynamic, Nested, Processing, Readonly

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Readonly

#attribute_writable?

Methods included from Processing

#process_attributes

Instance Attribute Details

- (Object) attributes (readonly) Also known as: raw_attributes

Returns the value of attribute attributes



18
19
20
# File 'lib/mongoid/attributes.rb', line 18

def attributes
  @attributes
end

Instance Method Details

- (Object) assign_attributes(attrs = nil)

Allows you to set all the attributes for a particular mass-assignment security role by passing in a hash of attributes with keys matching the attribute names (which again matches the column names) and the role name using the :as option. To bypass mass-assignment security you can use the :without_protection => true option.

Examples:

Assign the attributes.

person.assign_attributes(:title => "Mr.")

Assign the attributes (with a role).

person.assign_attributes({ :title => "Mr." }, :as => :admin)

Since:

  • 2.2.1



201
202
203
204
205
# File 'lib/mongoid/attributes.rb', line 201

def assign_attributes(attrs = nil)
  _assigning do
    process_attributes(attrs)
  end
end

- (true, false) attribute_missing?(name)

Determine if the attribute is missing from the document, due to loading it from the database with missing fields.

Examples:

Is the attribute missing?

document.attribute_missing?("test")

Since:

  • 4.0.0



237
238
239
240
241
242
243
# File 'lib/mongoid/attributes.rb', line 237

def attribute_missing?(name)
  selection = __selected_fields
  return false unless selection
  field = fields[name]
  (selection.values.first == 0 && selection_excluded?(name, selection, field)) ||
    (selection.values.first == 1 && !selection_included?(name, selection, field))
end

- (true, false) attribute_present?(name)

Determine if an attribute is present.

Examples:

Is the attribute present?

person.attribute_present?("title")

Since:

  • 1.0.0



31
32
33
34
# File 'lib/mongoid/attributes.rb', line 31

def attribute_present?(name)
  attribute = read_attribute(name)
  !attribute.blank? || attribute == false
end

- (Hash) attributes_before_type_cast

Get the attributes that have not been cast.

Examples:

Get the attributes before type cast.

document.attributes_before_type_cast

Since:

  • 3.1.0



44
45
46
# File 'lib/mongoid/attributes.rb', line 44

def attributes_before_type_cast
  @attributes_before_type_cast ||= {}
end

- (true, false) has_attribute?(name)

Does the document have the provided attribute?

Examples:

Does the document have the attribute?

model.has_attribute?(:name)

Since:

  • 3.0.0



58
59
60
# File 'lib/mongoid/attributes.rb', line 58

def has_attribute?(name)
  attributes.has_key?(name.to_s)
end

- (true, false) has_attribute_before_type_cast?(name)

Does the document have the provided attribute before it was assigned and type cast?

Examples:

Does the document have the attribute before it was assigned?

model.has_attribute_before_type_cast?(:name)

Since:

  • 3.1.0



74
75
76
# File 'lib/mongoid/attributes.rb', line 74

def has_attribute_before_type_cast?(name)
  attributes_before_type_cast.has_key?(name.to_s)
end

- (Object) read_attribute(name) Also known as: []

Read a value from the document attributes. If the value does not exist it will return nil.

Examples:

Read an attribute.

person.read_attribute(:title)

Read an attribute (alternate syntax.)

person[:title]

Since:

  • 1.0.0



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mongoid/attributes.rb', line 92

def read_attribute(name)
  normalized = database_field_name(name.to_s)
  if attribute_missing?(normalized)
    raise ActiveModel::MissingAttributeError, "Missing attribute: '#{name}'."
  end
  if hash_dot_syntax?(normalized)
    attributes.__nested__(normalized)
  else
    attributes[normalized]
  end
end

- (Object) read_attribute_before_type_cast(name)

Read a value from the attributes before type cast. If the value has not yet been assigned then this will return the attribute's existing value using read_attribute.

Examples:

Read an attribute before type cast.

person.read_attribute_before_type_cast(:price)

Since:

  • 3.1.0



118
119
120
121
122
123
124
125
# File 'lib/mongoid/attributes.rb', line 118

def read_attribute_before_type_cast(name)
  attr = name.to_s
  if attributes_before_type_cast.has_key?(attr)
    attributes_before_type_cast[attr]
  else
    read_attribute(attr)
  end
end

- (Object) remove_attribute(name)

Remove a value from the Document attributes. If the value does not exist it will fail gracefully.

Examples:

Remove the attribute.

person.remove_attribute(:title)

Raises:

Since:

  • 1.0.0



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mongoid/attributes.rb', line 139

def remove_attribute(name)
  access = name.to_s
  unless attribute_writable?(name)
    raise Errors::ReadonlyAttribute.new(name, :nil)
  end
  _assigning do
    attribute_will_change!(access)
    delayed_atomic_unsets[atomic_attribute_name(access)] = [] unless new_record?
    attributes.delete(access)
  end
end

- (Object) write_attribute(name, value) Also known as: []=

Write a single attribute to the document attribute hash. This will also fire the before and after update callbacks, and perform any necessary typecasting.

Examples:

Write the attribute.

person.write_attribute(:title, "Mr.")

Write the attribute (alternate syntax.)

person[:title] = "Mr."

Since:

  • 1.0.0



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mongoid/attributes.rb', line 165

def write_attribute(name, value)
  access = database_field_name(name.to_s)
  if attribute_writable?(access)
    _assigning do
      validate_attribute_value(access, value)
      localized = fields[access].try(:localized?)
      attributes_before_type_cast[name.to_s] = value
      typed_value = typed_value_for(access, value)
      unless attributes[access] == typed_value || attribute_changed?(access)
        attribute_will_change!(access)
      end
      if localized
        (attributes[access] ||= {}).merge!(typed_value)
      else
        attributes[access] = typed_value
      end
      typed_value
    end
  end
end

- (Object) write_attributes(attrs = nil) Also known as: attributes=

Writes the supplied attributes hash to the document. This will only overwrite existing attributes if they are present in the new Hash, all others will be preserved.

Examples:

Write the attributes.

person.write_attributes(:title => "Mr.")

Write the attributes (alternate syntax.)

person.attributes = { :title => "Mr." }

Since:

  • 1.0.0



221
222
223
# File 'lib/mongoid/attributes.rb', line 221

def write_attributes(attrs = nil)
  assign_attributes(attrs)
end