模块:Mongoid::Association::Builders

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

Overview

该模块负责定义一对一关联中使用的构建和创建方法。

例子:

创建的方法。


class Person
  include Mongoid::Document
  embeds_one :name
end

# The following methods get created:
person.build_name({ :first_name => "Durran" })
person.create_name({ :first_name => "Durran" })

类方法摘要折叠

类方法详细信息

define_builder!(关联)⇒

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

例子:

Person.define_builder!(association)

参数:

返回:

  • ( class )

    正在设立班级。



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 文件 'lib/mongoid/association/构建者.rb', line 47

def self.define_builder!(关联)
  关联.inverse_class.点击 do |klass|
    klass.re_define_method("build_#{Association.name}") do |*args|
      属性, 类型, _opts = parse_args(args)

      文档 = 工厂.execute_build(类型 || 关联.lation_class, 属性, execute_callbacks: false)
      _Building do
        子项 = 发送(" #{ Association . name } = ", 文档)
        子项.run_pending_callbacks
        子项.run_callbacks(:build)
        子项
      end
    end
  end
end

define_creator!(关联)⇒

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

例子:

Person.define_creator!(association)

参数:

返回:

  • ( class )

    正在设立班级。



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 文件 'lib/mongoid/association/构建者.rb', line 72

def self.define_creator!(关联)
  关联.inverse_class.点击 do |klass|
    klass.re_define_method("create_#{Association.name}") do |*args|
      属性, 类型, _opts = parse_args(args)

      文档 = 工厂.execute_build(类型 || 关联.lation_class, 属性, execute_callbacks: false)
      doc = _assing do
        发送(" #{ Association . name } = ", 文档)
      end
      doc.run_pending_callbacks
      doc.保存
      保存 if new_record? && 关联.store_foreign_key?
      doc
    end
  end
end