模块:Mongoid::Association::Relatable

Overview

该模块提供关联类型之间共享的行为。

常量摘要折叠

SHARED_OPTIONS =

所有关联类型之间共享的选项。

返回:

  • ( Array<Symbol> )

    共享选项。

%i[
  class_name
  inverse_of
  验证
  扩展
].冻结
PRIMARY_KEY_DEFAULT =

主键默认值。

返回:

  • ( string )

    主键字段默认值。

'_id'

实例属性摘要折叠

实例方法摘要折叠

选项中包含的方法

#as , #autoBuilding?#autosave#cascading_callbacks? , #counter_cached? , #cycle? 、#depend、# forced_nil_inverse , #indexed?#inverse_of#order#poly态?#primary_key#store_as#touch_field#touchable? , #type

Constrainable中包含的方法

#convert_to_foreign_key

实例属性详细信息

# name =" Symbol " (只读)

关联的名称。

返回:

  • (符号)

    关联的名称。



31
32
33
# File 'lib/mongoid/association/relatable.rb', line 31

def 名称
  @name
end

# options哈希(只读)

此关联上的选项。

返回:

  • (哈希)

    选项。



36
37
38
# File 'lib/mongoid/association/relatable.rb', line 36

def 选项
  @options
end

#所有者类 (只读)

拥有此关联的类。

返回:

  • ( class )

    所有者类。



41
42
43
# File 'lib/mongoid/association/relatable.rb', line 41

def Owner_class
  @owner_class
end

#parent_inclusionsArray<String>

包含树中位于该关联之上的关联。

返回:

  • ( Array<String> )

    关联。



319
320
321
# File 'lib/mongoid/association/relatable.rb', line 319

def parent_inclusions
  @parent_inclusions ||= []
end

实例方法详细信息

# == (other) ⇒对象

将此关联与另一个关联进行比较。

返回:

  • ( Object )

    与此关联进行比较的对象。



65
66
67
68
69
70
# File 'lib/mongoid/association/relatable.rb', line 65

def ==(其他)
  lation_class_name == 其他.lation_class_name &&
    inverse_class_name == 其他.inverse_class_name &&
    名称 == 其他.名称 &&
    选项 == 其他.选项
end

# 可绑定?(_doc) ⇒ true | false

尝试使用此关联绑定对象是否会引发错误。

参数:

  • doc (文档)

    要绑定的文档。

返回:

  • ( true | false )

    是否可以绑定文档。



96
97
98
# File 'lib/mongoid/association/relatable.rb', line 96

def 可绑定?(_doc)
  false
end

# counter_cache_column_namestring

获取计数器缓存列名称。

返回:

  • ( string )

    计数器缓存列名称。



275
276
277
278
279
280
281
282
# File 'lib/mongoid/association/relatable.rb', line 275

def counter_cache_column_name
  @counter_cache_column_name ||= if @options[:counter_cache].is_a?(字符串) ||
                                    @options[:counter_cache].is_a?(符号)
                                   @options[:counter_cache]
                                 else
                                   " #{ inverse || inverse_class_name . demodulize . underscore .pluralize } } _count "
                                 end
end

# create_relation (所有者, 目标) ⇒代理

使用所有者和目标创建关联代理对象。

参数:

  • 所有者 (文档)

    此关联所依赖的文档。

  • 目标 ( Document | Array< Document > )

    关联的目标(父级)。

返回:



261
262
263
# File 'lib/mongoid/association/relatable.rb', line 261

def create_relation(所有者, 目标)
  关系.new(所有者, 目标, self)
end

#具有破坏性?true | false

依赖方法是否具有破坏性。

返回:

  • ( true | false )

    如果依赖的方法具有破坏性。



268
269
270
# File 'lib/mongoid/association/relatable.rb', line 268

def 具有破坏性?
  @Destructive ||= !!(依赖 && %i[delete_all 销毁].包括?(依赖))
end

#扩展模块

获取扩展。

返回:

  • (模块)

    扩展模块(如果已定义)。



287
288
289
# File 'lib/mongoid/association/relatable.rb', line 287

def 扩展
  @extension ||= @options[:extend]
end

# foreign_key_checkstring

获取用于检查外键是否已更改的方法的名称。

例子:

获取外键检查方法。

association.foreign_key_check

返回:

  • ( string )

    外键检查。



250
251
252
# File 'lib/mongoid/association/relatable.rb', line 250

def foreign_key_check
  @foreign_key_check ||= " #{ foreign_key } _previously_changed? " if store_foreign_key? && foreign_key
end

#foreign_key_setterstring

外键设置器方法的名称。

返回:

  • ( string )

    外键设置器的名称。



220
221
222
223
224
# File 'lib/mongoid/association/relatable.rb', line 220

def foreign_key_setter
  # 注意:您无法检查此关联是否存储了外键
  # 查看 HasOne 和 HasMany 绑定,它们引用了 foreign_key_setter
  @foreign_key_setter ||= " #{ foreign_key } = " if foreign_key
end

# get_callbacks (callback_type) ⇒ Array<Proc |符号>

获取给定类型的回调。

参数:

  • callback_type (符号)

    回调类型的类型。

返回:

  • ( Array<Proc | Symbol> )

    回调列表,可以是方法名称或过程。



78
79
80
# File 'lib/mongoid/association/relatable.rb', line 78

def get_callbacks(callback_type)
  阵列(选项[callback_type])
end

# in_to?true | false

此关联是 embedded_in 关联还是 Belongs_to 关联?

返回:

  • ( true | false )

    如果是 embedded_in 或 belongs_to 关联,则为 true;如果不是,则为 false。



341
342
343
# File 'lib/mongoid/association/relatable.rb', line 341

def in_to?
  [ 引用::属于关联, Embedded::EmbeddedIn ].任何? { |a| is_a?(a) }
end

#initialize (_class, name, opts = {}, &block) ⇒对象

初始化关联。

参数:

  • _class ( class )

    拥有此关联的模型的类。

  • 名称 (符号)

    关联的名称。

  • opts 哈希 (默认为: {}

    关联选项。

  • (区块)

    可选区块。



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mongoid/association/relatable.rb', line 49

def 初始化(_class, 名称, opts = {}, )
  @owner_class = _class
  @name = 名称
  @options = opts
  @extension = nil

  @module_path = _class.名称 ? _class.名称.拆分(' :: ')[0..-2].连接 (JOIN)(' :: ') : ' '
  @module_path << ' :: ' 除非 @module_path.空?

  create_extension!()
  validate!
end

# inverse (other = nil) ⇒符号

获取反向名称。

返回:

  • (符号)

    反向名称。



294
295
296
297
# File 'lib/mongoid/association/relatable.rb', line 294

def 反向(其他 = nil)
  候选 = inverses(其他)
  候选.检测 { |C| C } if 候选
end

# inverse_association (other = nil) ⇒ Mongoid::Association::Relatable

获取逆向关联的元数据。

参数:

  • 其他 对象 (默认为: nil

    确定反向时要使用的其他模型类或模型对象。

返回:



123
124
125
# File 'lib/mongoid/association/relatable.rb', line 123

def inverse_association(其他 = nil)
  (其他 || lation_class).关系[反向(其他)]
end

# inverse_classstring也称为: inverse_klass

拥有此关联的对象的类。

返回:

  • ( string )

    所属对象的类。



190
191
192
# File 'lib/mongoid/association/relatable.rb', line 190

def inverse_class
  @owner_class
end

# inverse_class_namestring

拥有此关联的对象的类名。

返回:

  • ( string )

    所属对象的类名。



183
184
185
# File 'lib/mongoid/association/relatable.rb', line 183

def inverse_class_name
  @inverse_class_name ||= @owner_class.名称
end

# inverse_setter (other = nil) ⇒ string

反向 setter 方法的名称。

返回:

  • ( string )

    反向 setter 的名称。



213
214
215
# File 'lib/mongoid/association/relatable.rb', line 213

def inverse_setter(其他 = nil)
  @inverse_setter ||= " #{ inverses ( other ) . first } = " 除非 inverses(其他).空白?
end

# inverse_type =" nil "

获取反向类型。

返回:

  • ( nil )

    关联的默认值为 nil。



130
# File 'lib/mongoid/association/relatable.rb', line 130

def inverse_type end

# inverse_type_setterstring

获取用于在多态关联上设置文档类型的字段的 setter。

例子:

获取反向类型 setter。

association.inverse_type_setter

返回:

  • ( string )

    setter 的名称。



240
241
242
# File 'lib/mongoid/association/relatable.rb', line 240

def inverse_type_setter
  @inverse_type_setter ||= " #{ inverse_type } = " if inverse_type
end

# inverses (other = nil) ⇒ Array<Symbol>

获取反向名称。

参数:

  • 其他 对象 (默认为: nil

    确定反向时要使用的其他模型类或模型对象。

返回:

  • ( Array<Symbol> )

    反向名称列表。



106
107
108
109
110
111
112
113
114
115
# File 'lib/mongoid/association/relatable.rb', line 106

def inverses(其他 = nil)
  return [ inverse_of ] if inverse_of
  return [] if @options.键?(:inverse_of) && !inverse_of

  if 多态?
    polymorphic_inverses(其他)
  else
    describe_inverses(其他)
  end
end

#符号 | string

如果此关联存储外键,则为外键字段。 否则,为主键。

返回:

  • (符号 | string )

    主键。



199
200
201
# File 'lib/mongoid/association/relatable.rb', line 199

def key
  store_foreign_key? ? foreign_key : primary_key
end

#很多?true | false

此关联是 embeds_many 还是 has_many 关联?

返回:

  • ( true | false )

    如果是 *_many 关联,则为 true;如果不是,则为 false。



326
327
328
# File 'lib/mongoid/association/relatable.rb', line 326

def 很多?
  [ 引用::有很多, Embedded::嵌入许多 ].任何? { |a| is_a?(a) }
end

# one?true | false

此关联是 embeds_one 还是 has_one 关联?

返回:

  • ( true | false )

    如果是 *_one 关联,则为 true;如果不是,则为 false。



333
334
335
# File 'lib/mongoid/association/relatable.rb', line 333

def 一个?
  [ 引用::有一个, Embedded::嵌入一个 ].任何? { |a| is_a?(a) }
end

#路径(文档)⇒ Mongoid::Atomic::Paths::Root

此关联的原子路径。

返回:



229
230
231
# File 'lib/mongoid/association/relatable.rb', line 229

def 路径(文档)
  关系.路径(文档)
end

# relation_classstring也称为: klass

关联对象的类。

此方法返回与relation_class_name相对应的类实例,该实例是相对于托管文档类进行解析的。

如果该类不存在,此方法将引发 NameError。 发生这种情况的原因可能是尚未定义目标类。 请注意,多态关联通常没有明确定义的目标类,因为目标类可以从一个对象更改为另一个对象,对多态关联调用此方法通常会失败并显示 NameError 或产生误导性结果(如果一个类确实发生在使用与关联名称相同的名称定义)。

返回:

  • ( string )

    关联对象的类。



172
173
174
175
176
177
# File 'lib/mongoid/association/relatable.rb', line 172

def lation_class
  @klass ||= 开始
    cls_name = @options[:class_name] || ActiveSupport::变形器.分类(名称)
    resolve_name(inverse_class, cls_name)
  end
end

# relation_class_namestring也称为: class_name

注意:

此方法的返回值不应用于确定两个关联是否具有相同的目标类,因为返回值并不总是完全限定的类名。 要比较类,请使用relation_class方法检索关联目标的类实例。

类名,可能是未限定的或

前缀,关联的

对象。

此方法返回关联定义中使用的类名称。 如果在关联中给出 :class_name 选项,则在此处返回该选项的确切值。 如果未给出 :class_name 选项,则类的名称根据关联名称计算得出,但不会解析为实际的类。

此方法返回的类名可能与定义的类不对应,因为尚未加载相应的类,或者因为关联引用了完全不存在的类。 要获取关联类,请使用relation_class方法。

返回:

  • ( string )

    关联对象的类名。



153
154
155
# File 'lib/mongoid/association/relatable.rb', line 153

def lation_class_name
  @class_name ||= @options[:class_name] || ActiveSupport::变形器.分类(名称)
end

# setterstring

此对象上用于分配关联对象的 setter 名称。

返回:

  • ( string )

    setter 名称。



206
207
208
# File 'lib/mongoid/association/relatable.rb', line 206

def setter
  @setter ||= " #{ name } = "
end

# type_setterstring

注意:

仅与采用 :as 选项的多态关联相关。

获取类型 setter。

返回:

  • ( string )

    类型 setter 方法。



86
87
88
# File 'lib/mongoid/association/relatable.rb', line 86

def type_setter
  @type_setter ||= " #{ type } = " if 类型
end

# validate?true | false

是否应验证关联对象。

返回:

  • ( true | false )

    是否应验证关联对象。



303
304
305
306
307
308
309
# File 'lib/mongoid/association/relatable.rb', line 303

def validate?
  @validate ||= if @options[:validate].nil?
                  validation_default
                else
                  !!@options[:validate]
                end
end