模块:Mongoid::Association::Accessors

扩展方式:
ActiveSupport::Concern
包含在:
Mongoid::Association
定义于:
lib/mongoid/association/accessors.rb

Overview

此模块包含与通过 getter 和 setter 访问关联相关的所有行为,以及如何委托构建者以创建新关联。

类方法摘要折叠

实例方法摘要折叠

类方法详细信息

define_builder!(关联)⇒

为 embeds_one 关联定义构建器方法。它被定义为 #build_name。

例子:

Person.define_builder!(association)

参数:

返回:

  • ( class )

    正在设立班级。



387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/mongoid/association/accessors.rb', line 387

def self.define_builder!(关联)
  名称 = 关联.名称
  关联.inverse_class.点击 do |klass|
    klass.re_define_method("build_#{name}") do |*args|
      属性, _options = parse_args(*args)
      文档 = 工厂.构建(关联.lation_class, 属性)
      _Building do
        子项 = 发送(" #{ name } = ", 文档)
        子项.run_callbacks(:build)
        子项
      end
    end
  end
end

define_creator!(关联)⇒

为 embeds_one 关联定义创建者方法。它被定义为 #create_name。对象构建后会立即保存。

例子:

Person.define_creator!(association)

参数:

返回:

  • ( class )

    正在设立班级。



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/mongoid/association/accessors.rb', line 412

def self.define_creator!(关联)
  名称 = 关联.名称
  关联.inverse_class.点击 do |klass|
    klass.re_define_method("create_#{name}") do |*args|
      属性, _options = parse_args(*args)
      文档 = 工厂.构建(关联.klass, 属性)
      doc = _assing do
        发送(" #{ name } = ", 文档)
      end
      doc.保存
      保存 if new_record? && 关联.store_foreign_key?
      doc
    end
  end
end

define_existence_check!(关联)⇒

添加关联的存在性检查。

例子:

添加存在性检查。

Person.define_existence_check!(association)

检查关联是否存在。

person = Person.new
person.has_game?
person.game?

参数:

返回:

  • ( class )

    正在设立的model。



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/mongoid/association/accessors.rb', line 277

def self.define_existence_check!(关联)
  名称 = 关联.名称
  关联.inverse_class.点击 do |klass|
    klass.module_eval <<-END, __FILE__, __LINE__ + 1
        def #{名称}? without_autobuild { !__send__(:#{名称}).blank? } end alias :has_#{名称}? :#{名称}?
    END
  end
end

define_getter!(关联)⇒

定义关联的 getter。这里没有什么特别的:只需返回关联的实例变量(如果存在)或构建事物。

例子:

设置关联的 getter。

Person.define_getter!(association)

参数:

返回:

  • ( class )

    正在设立班级。



299
300
301
302
303
304
305
306
307
308
# File 'lib/mongoid/association/accessors.rb', line 299

def self.define_getter!(关联)
  名称 = 关联.名称
  关联.inverse_class.点击 do |klass|
    klass.re_define_method(名称) do |重新加载 = false|
       = get_relation(名称, 关联, nil, 重新加载)
       = 发送("build_#{name}") if .nil? && 关联.自动构建? && !without_autobuild?
      
    end
  end
end

define_ids_getter!(关联)⇒

定义关联中 document ID 的 getter。应仅在引用的多个关联时指定。

例子:

为关联设置 id getter。

Person.define_ids_getter!(association)

参数:

返回:

  • ( class )

    正在设立班级。



319
320
321
322
323
324
325
326
# File 'lib/mongoid/association/accessors.rb', line 319

def self.define_ids_getter!(关联)
  ids_method = "#{association.name.to_s.singularize}_ids"
  关联.inverse_class.点击 do |klass|
    klass.re_define_method(ids_method) do
      发送(关联.名称).采摘(:_id)
    end
  end
end

define_ids_setter!(关联)→ 对象

定义 setter 方法,允许您通过 ID 在此关联中设立 document。定义的 setter 查找具有给定 ID 的 document,并使用找到的 document 调用常规关联 setter。应该仅为引用的许多关联定义 Ids setter。

@param [ Mongoid::Association::Relatable ] 关联 关联的关联。

@return [ Class ] 正在设立的类。

例子:

设置关联的 id_setter。

Person.define_ids_setter!(association)


368
369
370
371
372
373
374
375
376
# File 'lib/mongoid/association/accessors.rb', line 368

def self.define_ids_setter!(关联)
  ids_method = "#{association.name.to_s.singularize}_ids="
  关联.inverse_class.aliased_associations[ids_method.印章] = 关联.名称.to_s
  关联.inverse_class.点击 do |klass|
    klass.re_define_method(ids_method) do |id|
      发送(关联.setter, 关联.lation_class.find(id.拒绝(:blank?)))
    end
  end
end

define_setter!(关联)⇒

定义关联的 setter。这会根据某些条件执行一些操作。如果存在关联,则会进行目标替换,否则将使用提供的目标创建新关联。

例子:

为关联设置 setter。

Person.define_setter!(association)

参数:

返回:

  • ( class )

    正在设立班级。



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/mongoid/association/accessors.rb', line 339

def self.define_setter!(关联)
  名称 = 关联.名称
  关联.inverse_class.点击 do |klass|
    klass.re_define_method(" #{ name } = ") do |对象|
      without_autobuild do
        if  = get_relation(名称, 关联, 对象)
           = __build__(名称, , 关联) 除非 .respond_to?(:subsubscribe)

          set_relation(名称, .替换(对象.可替换的))
        else
          __build__(名称, 对象.可替换的, 关联)
        end
      end
    end
  end
end

实例方法详细信息

# __build__ (name, 对象,association,selected_fields = nil) ⇒代理

构建相关文档并创建关联,除非该文档为零,然后对此文档设置关联。

例子:

建立关联。

person.__build__(:addresses, { :_id => 1 }, association)

参数:

  • 名称 ( string | Symbol )

    关联的名称。

  • 对象 ( Hash | BSON::ObjectId )

    要使用的 ID 或属性。

  • 关联 ( Mongoid::Association::Relatable )

    关联元数据。

  • created_fields 哈希 (默认为: nil

    通过 #only 检索的字段。 如果指定了selected_fields,则无法在构建的文档中访问未在其中列出的字段。

返回:



25
26
27
28
# File 'lib/mongoid/association/accessors.rb', line 25

def __build__(名称, 对象, 关联, created_fields = nil)
  关系 = create_relation(对象, 关联, created_fields)
  set_relation(名称, 关系)
end

# create_relation (对象,association,selected_fields = nil) ⇒代理

从对象和关联元数据创建关联。

例子:

创建关联。

person.create_relation(document, association)

参数:

  • 对象 ( Document | Array< Document > )

    关联目标。

  • 关联 ( Mongoid::Association::Relatable )

    关联元数据。

  • created_fields 哈希 (默认为: nil

    通过 #only 检索的字段。 如果指定了selected_fields,则在创建的关联文档中将无法访问未在其中列出的字段。

返回:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mongoid/association/accessors.rb', line 42

def create_relation(对象, 关联, created_fields = nil)
  key = @attributes[关联.inverse_type]
  类型 = key ? 关联.解析器.model_for(key) : nil
  目标 = if t = 关联.构建(self, 对象, 类型, created_fields)
             关联.create_relation(self, t)
           end

  # 只需要对嵌入式关联执行此操作。 待处理的回调
  # 仅在物化文档时添加,这种情况只发生
  # 针对嵌入式关联。 中没有调用数据库
  # 构建引用关联。
  if 关联.嵌入式?
    阵列(目标). do |doc|
      doc.try(:run_pending_callbacks)
    end
  end

  目标
end

# reset_relation_criteria (name) ⇒对象

重置关联代理内的条件。 由多对多关联用于使根本的id大量保持同步。

例子:

重置关联条件。

person.reset_relation_criteria(:preferences)

参数:

  • 名称 (符号)

    关联的名称。



69
70
71
72
73
# File 'lib/mongoid/association/accessors.rb', line 69

def reset_relation_criteria(名称)
  return 除非 instance_variable_defined?(" @_ #{ name } ")

  发送(名称).reset_unloaded
end

# set_relation (name, relation) ⇒代理

将提供的关联设置为具有提供名称的类上的实例变量。 用作助手,只是为了保持代码简洁。

例子:

在文档上设置代理。

person.set(:addresses, addresses)

参数:

  • 名称 ( string | Symbol )

    关联的名称。

  • 关系 (代理)

    要设立的关联。

返回:



85
86
87
# File 'lib/mongoid/association/accessors.rb', line 85

def set_relation(名称, 关系)
  instance_variable_set(" @_ #{ name } ", 关系)
end