模块:Mongoid::Fields::ClassMethods

定义于:
lib/mongoid/fields.rb ,
lib/mongoid/fields.rb

实例方法摘要折叠

实例方法详细信息

#attribute_namesArray<String>

返回此对象可用属性的名称大量。

以与 ORM 无关的方式提供字段名称。 Rails v 3.1 + 使用此方法自动包装JSON请求中的参数。

例子:

获取字段名称

Model.attribute_names

返回:

  • ( Array<String> )

    字段名称



454
455
456
# File 'lib/mongoid/fields.rb', line 454

def attribute_names
  字段.密钥
end

# clean_localized_field_names (name) ⇒字段

从给定字段名称中删除 _translations。 仅当尚不存在具有相同名称的字段名称或关系(即带有 _translations 后缀)时,才会执行此操作。 对现有字段的检查是以递归方式完成的

参数:

  • 名称 ( string | Symbol )

    要清理的字段的名称。

返回:

  • (字段)

    不含 _translations 的字段名称



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 clean_localized_field_names(名称)
  名称 = database_field_name(名称.to_s)

  klass = self
  [].点击 do |res|
    ar = 名称.拆分('  ')
    ar.each_with_index do |fn, i|
      key = fn
      除非 klass.字段.键?(fn) || klass.关系.键?(fn)
        if fn.end_with?(TRANSLATIONS_SFX)
          key = fn.delete_suffix(TRANSLATIONS_SFX)
        else
          key = fn
        end

      end
      res.推动(key)

      if klass.字段.键?(fn)
        res.推动(ar.删除(i+1).连接 (JOIN)('  ')) 除非 i == ar.长度 - 1
        中断
      elsif klass.关系.键?(fn)
        klass = klass.关系[key].klass
      end
    end
  end.连接 (JOIN)('  ')
end

# database_field_name (name) ⇒ string

获取所提供字段存储在数据库中的名称。 用于确定字段是否有别名。

参数:

  • 名称 ( string | Symbol )

    要获取的名称。

返回:

  • ( string )

    存储在数据库中的字段名称。



464
465
466
# File 'lib/mongoid/fields.rb', line 464

def database_field_name(名称)
  字段.database_field_name(名称, 关系, aliased_fields, aliased_associations)
end

#extract_id_field (attributes) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

根据此类中定义的别名,从指定属性哈希中提取ID字段。

参数:

  • 属性 (哈希)

    要检查的属性。

返回:

  • ( Object )

    ID 值。



83
84
85
86
87
88
89
90
# File 'lib/mongoid/fields.rb', line 83

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

# 字段 (name, options = {}) ⇒ Field

定义文档上可访问的所有字段。对于定义的每个字段,都会将 getter 和 setter 作为实例方法添加到文档中。

例子:

定义一个字段。

field :score, type: Integer, default: 0

参数:

  • 名称 (符号)

    字段的名称。

  • 选项 哈希 (默认为: {}

    要传递给字段的选项。

选项哈希 ( options ):

  • :type 类 | 符号 | string

    字段的类型。

  • :label string

    字段的标签。

  • :default 对象 | 过程

    该字段的默认。

返回:

  • (字段)

    生成的字段



483
484
485
486
487
488
489
490
491
# File 'lib/mongoid/fields.rb', line 483

def 字段(名称, 选项 = {})
  命名 = 名称.to_s
  验证器::.验证(self, 名称, 选项)
  添加 = add_field(命名, 选项)
  descendants. do |子类|
    子类.add_field(命名, 选项)
  end
  添加
end

# id_fieldsArray<Symbol | string >

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

以字符串和符号形式返回此模型类的ID字段列表。

返回:

  • ( Array<Symbol | string > )

    ID 字段列表。



64
65
66
67
68
69
70
71
72
73
# File 'lib/mongoid/fields.rb', line 64

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

# replace_field (name, type) =" Serializable "

将字段替换为新类型。

例子:

替换字段。

Model.replace_field("_id", String)

参数:

  • 名称 ( string )

    字段的名称。

  • 类型 ( class )

    新的字段类型。

返回:



502
503
504
505
# File 'lib/mongoid/fields.rb', line 502

def replace_field(名称, 类型)
  remove_defaults(名称)
  add_field(名称, 字段[名称].选项.合并(merge)(类型: 类型))
end

# traverse_association_tree (key) {|The, The,是否| ... } ⇒字段

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

向下遍历关联树并搜索给定键的字段。

参数:

  • key ( string )

    用于搜索关联树的键。

  • 区块(&B)

    区块。

收益参数:

  • 使用 (符号)

    当前方法。

  • 使用 (符号 | string )

    字段或关系。

  • 是否 ( true | false )

    第二个让出参数是否为字段。

返回:

  • (字段)

    搜索结束时为给定键找到的字段。 如果找到的最后一个是关联或者没有找到给定键的字段,则返回 nil。



533
534
535
# File 'lib/mongoid/fields.rb', line 533

def traverse_association_tree(key, )
  字段.traverse_association_tree(key, 字段, 关系, aliased_associations, )
end

# using_object_id?true | false

确定是否使用BSON::ObjectIds作为 ID 的便捷方法。

例子:

这个类是否使用对象ID?

person.using_object_ids?

返回:

  • ( true | false )

    如果该类使用BSON::ObjectIds 作为ID 。



514
515
516
# File 'lib/mongoid/fields.rb', line 514

def using_object_id?
  字段[" _id "].object_id_field?
end