Module: Mongoid::Fields::ClassMethods
- Defined in:
- lib/mongoid/fields.rb,
lib/mongoid/fields.rb
Instance Method Summary collapse
-
#attribute_names ⇒ Array<String>
Returns an array of names for the attributes available on this object.
-
#cleanse_localized_field_names(name) ⇒ Field
Removes the _translations from the given field name.
-
#database_field_name(name) ⇒ String
Get the name of the provided field as it is stored in the database.
-
#extract_id_field(attributes) ⇒ Object
private
Extracts the id field from the specified attributes hash based on aliases defined in this class.
-
#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.
-
#id_fields ⇒ Array<Symbol | String>
private
Returns the list of id fields for this model class, as both strings and symbols.
-
#replace_field(name, type) ⇒ Serializable
Replace a field with a new type.
-
#traverse_association_tree(key) {|The, The, Whether| ... } ⇒ Field
private
Traverse down the association tree and search for the field for the given key.
-
#using_object_ids? ⇒ true | false
Convenience method for determining if we are using
BSON::ObjectIds
as our id.
Instance Method Details
#attribute_names ⇒ Array<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.
454 455 456 |
# File 'lib/mongoid/fields.rb', line 454 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mongoid/fields.rb', line 100 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 fn.end_with?(TRANSLATIONS_SFX) key = fn.delete_suffix(TRANSLATIONS_SFX) 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.
464 465 466 |
# File 'lib/mongoid/fields.rb', line 464 def database_field_name(name) Fields.database_field_name(name, relations, aliased_fields, aliased_associations) 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.
83 84 85 86 87 88 89 90 |
# File 'lib/mongoid/fields.rb', line 83 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.
483 484 485 486 487 488 489 490 491 |
# File 'lib/mongoid/fields.rb', line 483 def field(name, = {}) named = name.to_s Validators::Macro.validate(self, name, ) added = add_field(named, ) descendants.each do |subclass| subclass.add_field(named, ) end added end |
#id_fields ⇒ Array<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.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mongoid/fields.rb', line 64 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.
502 503 504 505 |
# File 'lib/mongoid/fields.rb', line 502 def replace_field(name, type) remove_defaults(name) add_field(name, fields[name]..merge(type: type)) end |
#traverse_association_tree(key) {|The, The, Whether| ... } ⇒ Field
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.
Traverse down the association tree and search for the field for the given key.
533 534 535 |
# File 'lib/mongoid/fields.rb', line 533 def traverse_association_tree(key, &block) Fields.traverse_association_tree(key, fields, relations, aliased_associations, &block) end |
#using_object_ids? ⇒ true | false
Convenience method for determining if we are using BSON::ObjectIds
as our id.
514 515 516 |
# File 'lib/mongoid/fields.rb', line 514 def using_object_ids? fields["_id"].object_id_field? end |