模块:Mongoid::Association::Referenced::AutoSave

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

Overview

Mongoid::Document 中包含的 Mixin 模块能力了在保存主题文档时自动将相对侧文档保存在引用关联中的功能。

类方法摘要折叠

实例方法摘要折叠

类方法详细信息

define_autosave! (关联)⇒

在关联对象的关联所属类上定义自动保存方法。

例子:

定义自动保存方法:

Association::Referenced::Autosave.define_autosave!(association)

参数:

返回:

  • ( class )

    关联的所有者类。



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mongoid/association/referenced/auto_save.rb', line 53

def self.define_autosave!(关联)
  关联.inverse_class.点击 do |klass|
    save_method = :"autosave_documents_for_#{关联.名称}"
    klass.发送(:define_method, save_method) do
      if before_callback_halted?
        self.before_callback_halted = false
      else
        __auto Saving__ do
          if assoc_value = ivar(关联.名称)
            阵列(assoc_value). do |doc|
              pc = doc.persistence_context? ? doc.persistence_context : persistence_context.for_child(doc)
              doc.通过(pc) do |d|
                d.保存
              end
            end
          end
        end
      end
    end
    klass.after_persist_parent save_method, 除非: :autoSaved?
  end
end

实例方法详细信息

#__autosaving__Object

开始关联的自动保存。

例子:

Begin autosave.

document.__autosaving__


28
29
30
31
32
33
# File 'lib/mongoid/association/referenced/auto_save.rb', line 28

def __auto Saving__
  线程化.begin_autosave(self)
  产量
确保
  线程化.exit_autosave(self)
end

#自动保存?true | false

用于防止关联的自动保存中出现无限循环。

例子:

文档是否自动保存?

document.autosaved?

返回:

  • ( true | false )

    文档是否已自动保存?



20
21
22
# File 'lib/mongoid/association/referenced/auto_save.rb', line 20

def 自动保存?
  线程化.自动保存?(self)
end

# Changed_for_autosave? (doc) ⇒布尔值

检查是否有自动保存的更改

document.changed_for_autosave?

例子:

如果 self 或 in 发生更改,则返回 true

autosaved associations.

返回:



40
41
42
# File 'lib/mongoid/association/referenced/auto_save.rb', line 40

def changed_for_autosave?(doc)
  doc.new_record? || doc.改变? || doc.marked_for_destruction?
end