模块:Mongoid::Association::Relatable

Overview

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

常量摘要折叠

SHARED_OPTIONS =

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

返回:

  • ( Array<Symbol> )

    共享选项。

[
  :class_name,
  :inverse_of,
  :validate,
  :extend
].冻结
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 " (只读)

关联的名称。

返回:

  • (符号)

    关联的名称。



33
34
35
# File 'lib/mongoid/association/relatable.rb', line 33

def 名称
  @name
end

# options哈希(只读)

此关联上的选项。

返回:

  • (哈希)

    选项。



38
39
40
# File 'lib/mongoid/association/relatable.rb', line 38

def 选项
  @options
end

#parent_inclusionsArray<String>

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

返回:

  • ( Array<String> )

    关联。



306
307
308
# File 'lib/mongoid/association/relatable.rb', line 306

def parent_inclusions
  @parent_inclusions
end

实例方法详细信息

# == (other) ⇒对象

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

返回:

  • ( Object )

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



62
63
64
65
66
67
# File 'lib/mongoid/association/relatable.rb', line 62

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

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

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

参数:

  • doc (文档)

    要绑定的文档。

返回:

  • ( true | false )

    是否可以绑定文档。



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

def 可绑定?(doc) false end

# counter_cache_column_namestring

获取计数器缓存列名称。

返回:

  • ( string )

    计数器缓存列名称。



270
271
272
273
274
# File 'lib/mongoid/association/relatable.rb', line 270

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

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

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

参数:

  • 所有者 (文档)

    此关联所依赖的文档。

  • 目标 ( Document | Array< Document > )

    关联的目标(父级)。

返回:



256
257
258
# File 'lib/mongoid/association/relatable.rb', line 256

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

#具有破坏性?true | false

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

返回:

  • ( true | false )

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



263
264
265
# File 'lib/mongoid/association/relatable.rb', line 263

def 具有破坏性?
  @Destructive ||= !!(依赖 && (依赖 == :delete_all || 依赖 == :destroy))
end

#扩展模块

获取扩展。

返回:

  • (模块)

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



279
280
281
# File 'lib/mongoid/association/relatable.rb', line 279

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

# foreign_key_checkstring

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

例子:

获取外键检查方法。

association.foreign_key_check

返回:

  • ( string )

    外键检查。



245
246
247
# File 'lib/mongoid/association/relatable.rb', line 245

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

#foreign_key_setterstring

外键设置器方法的名称。

返回:

  • ( string )

    外键设置器的名称。



215
216
217
218
219
# File 'lib/mongoid/association/relatable.rb', line 215

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> )

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



75
76
77
# File 'lib/mongoid/association/relatable.rb', line 75

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

# in_to?true | false

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

返回:

  • ( true | false )

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



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

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

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

初始化关联。

参数:

  • _class ( class )

    拥有此关联的模型的类。

  • 名称 (符号)

    关联的名称。

  • opts 哈希 (默认为: {}

    关联选项。

  • (区块)

    可选区块。



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mongoid/association/relatable.rb', line 46

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) ⇒符号

获取反向名称。

返回:

  • (符号)

    反向名称。



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

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

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

获取反向关联元数据。

参数:

  • 其他 对象 (默认为: nil

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

返回:



118
119
120
# File 'lib/mongoid/association/relatable.rb', line 118

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

# inverse_classstring也称为: inverse_klass

拥有此关联的对象的类。

返回:

  • ( string )

    所属对象的类。



185
186
187
# File 'lib/mongoid/association/relatable.rb', line 185

def inverse_class
  @owner_class
end

# inverse_class_namestring

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

返回:

  • ( string )

    所属对象的类名。



178
179
180
# File 'lib/mongoid/association/relatable.rb', line 178

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

# inverse_setter (other = nil) ⇒ string

反向 setter 方法的名称。

返回:

  • ( string )

    反向 setter 的名称。



208
209
210
# File 'lib/mongoid/association/relatable.rb', line 208

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

# inverse_type =" nil "

获取反向类型。

返回:

  • ( nil )

    关联的默认值为 nil。



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

def inverse_type end

# inverse_type_setterstring

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

例子:

获取反向类型 setter。

association.inverse_type_setter

返回:

  • ( string )

    setter 的名称。



235
236
237
# File 'lib/mongoid/association/relatable.rb', line 235

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

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

获取反向名称。

参数:

  • 其他 对象 (默认为: nil

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

返回:

  • ( Array<Symbol> )

    反向名称列表。



101
102
103
104
105
106
107
108
109
110
# File 'lib/mongoid/association/relatable.rb', line 101

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 )

    主键。



194
195
196
# File 'lib/mongoid/association/relatable.rb', line 194

def key
  store_foreign_key? ? foreign_key : primary_key
end

#很多?true | false

此关联是 embeds_many 还是 has_many 关联?

返回:

  • ( true | false )

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



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

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

# one?true | false

此关联是 embeds_one 还是 has_one 关联?

返回:

  • ( true | false )

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



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

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

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

此关联的原子路径。

返回:



224
225
226
# File 'lib/mongoid/association/relatable.rb', line 224

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

# relation_classstring也称为: klass

关联对象的类。

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

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

返回:

  • ( string )

    关联对象的类。



167
168
169
170
171
172
# File 'lib/mongoid/association/relatable.rb', line 167

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 )

    关联对象的类名。



148
149
150
# File 'lib/mongoid/association/relatable.rb', line 148

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

# setterstring

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

返回:

  • ( string )

    setter 名称。



201
202
203
# File 'lib/mongoid/association/relatable.rb', line 201

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

# type_setterstring

注意:

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

获取类型 setter。

返回:

  • ( string )

    类型 setter 方法。



83
84
85
# File 'lib/mongoid/association/relatable.rb', line 83

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

# validate?true | false

是否应验证关联对象。

返回:

  • ( true | false )

    是否应验证关联对象。



295
296
297
298
299
300
301
# File 'lib/mongoid/association/relatable.rb', line 295

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