Module: Mongoid::Fields::ClassMethods

Defined in:
build/mongoid-7.4/lib/mongoid/fields.rb,
build/mongoid-7.4/lib/mongoid/fields.rb

Instance Method Summary collapse

Instance Method Details

#attribute_namesArray<String>

Returns an array of names for the attributes available on this object.

Provides the field names in an ORM-agnostic way. Rails v3.1+ uses this method to automatically wrap params in JSON requests.

Examples:

Get the field names

Model.attribute_names

Returns:

  • (Array<String>)

    The field names



286
287
288
# File 'build/mongoid-7.4/lib/mongoid/fields.rb', line 286

def attribute_names
  fields.keys
end

#cleanse_localized_field_names(name) ⇒ Field

Removes the _translations from the given field name. This is done only when there doesn't already exist a field name or relation with the same name (i.e. with the _translations suffix). This check for an existing field is done recursively

Parameters:

  • name (String | Symbol)

    The name of the field to cleanse.

Returns:

  • (Field)

    The field name without _translations



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'build/mongoid-7.4/lib/mongoid/fields.rb', line 88

def cleanse_localized_field_names(name)
  name = database_field_name(name.to_s)

  klass = self
  [].tap do |res|
    ar = name.split('.')
    ar.each_with_index do |fn, i|
      key = fn
      unless klass.fields.key?(fn) || klass.relations.key?(fn)
        if tr = fn.match(/(.*)_translations\z/)&.captures&.first
          key = tr
        else
          key = fn
        end

      end
      res.push(key)

      if klass.fields.key?(fn)
        res.push(ar.drop(i+1).join('.')) unless i == ar.length - 1
        break
      elsif klass.relations.key?(fn)
        klass = klass.relations[key].klass
      end
    end
  end.join('.')
end

#database_field_name(name) ⇒ String

Get the name of the provided field as it is stored in the database. Used in determining if the field is aliased or not. Recursively finds aliases for embedded documents and fields, delimited with period “.” character.

Examples:

Get the database field name of a field.

Model.database_field_name(:authorization)

Get the database field name of an embedded field.

Model.database_field_name('customers.addresses.city')

Parameters:

  • name (String, Symbol)

    The name to get.

Returns:

  • (String)

    The name of the field as stored in the database.



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'build/mongoid-7.4/lib/mongoid/fields.rb', line 304

def database_field_name(name)
  if Mongoid.broken_alias_handling
    return nil unless name
    normalized = name.to_s
    aliased_fields[normalized] || normalized
  else
    return nil unless name.present?
    key = name.to_s
    segment, remaining = key.split('.', 2)
    segment = aliased_fields[segment]&.dup || segment
    return segment unless remaining

    relation = relations[aliased_associations[segment] || segment]
    if relation
      "#{segment}.#{relation.klass.database_field_name(remaining)}"
    else
      "#{segment}.#{remaining}"
    end
  end
end

#extract_id_field(attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts the id field from the specified attributes hash based on aliases defined in this class.

Parameters:

  • attributes (Hash)

    The attributes to inspect.

Returns:

  • (Object)

    The id value.



71
72
73
74
75
76
77
78
# File 'build/mongoid-7.4/lib/mongoid/fields.rb', line 71

def extract_id_field(attributes)
  id_fields.each do |k|
    if v = attributes[k]
      return v
    end
  end
  nil
end

#field(name, options = {}) ⇒ Field

Defines all the fields that are accessible on the Document For each field that is defined, a getter and setter will be added as an instance method to the Document.

Examples:

Define a field.

field :score, :type => Integer, :default => 0

Parameters:

  • name (Symbol)

    The name of the field.

  • options (Hash) (defaults to: {})

    The options to pass to the field.

Options Hash (options):

  • :type (Class)

    The type of the field.

  • :label (String)

    The label for the field.

  • :default (Object, Proc)

    The field's default

Returns:

  • (Field)

    The generated field



340
341
342
343
344
345
346
347
348
# File 'build/mongoid-7.4/lib/mongoid/fields.rb', line 340

def field(name, options = {})
  named = name.to_s
  Validators::Macro.validate(self, name, options)
  added = add_field(named, options)
  descendants.each do |subclass|
    subclass.add_field(named, options)
  end
  added
end

#id_fieldsArray<Symbol | String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the list of id fields for this model class, as both strings and symbols.

Returns:

  • (Array<Symbol | String>)

    List of id fields.



52
53
54
55
56
57
58
59
60
61
# File 'build/mongoid-7.4/lib/mongoid/fields.rb', line 52

def id_fields
  IDS.dup.tap do |id_fields|
    aliased_fields.each do |k, v|
      if v == '_id'
        id_fields << k.to_sym
        id_fields << k
      end
    end
  end
end

#replace_field(name, type) ⇒ Serializable

Replace a field with a new type.

Examples:

Replace the field.

Model.replace_field("_id", String)

Parameters:

  • name (String)

    The name of the field.

  • type (Class)

    The new type of field.

Returns:



359
360
361
362
# File 'build/mongoid-7.4/lib/mongoid/fields.rb', line 359

def replace_field(name, type)
  remove_defaults(name)
  add_field(name, fields[name].options.merge(type: type))
end

#using_object_ids?true, false

Convenience method for determining if we are using BSON::ObjectIds as our id.

Examples:

Does this class use object ids?

person.using_object_ids?

Returns:

  • (true, false)

    If the class uses BSON::ObjectIds for the id.



371
372
373
# File 'build/mongoid-7.4/lib/mongoid/fields.rb', line 371

def using_object_ids?
  fields["_id"].object_id_field?
end