模块: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 )

    关联的所有者类。



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mongoid/association/referenced/auto_save.rb', line 62

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|
              来年 除非 changed_for_autosave?(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__


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

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

#自动保存?true | false

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

例子:

文档是否自动保存?

document.autosaved?

返回:

  • ( true | false )

    文档是否已自动保存?



18
19
20
# File 'lib/mongoid/association/referenced/auto_save.rb', line 18

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

# Changed_for_autosave? (doc,seen = Set.new) ⇒ true | false

检查是否有自动保存的更改。如果 document 为新建 document、已更改 document 或标记为待销毁 document,或者任何内存中引用的具有 autosave: true 的子 document 递归地满足相同条件,则返回 true。

当自动保存关联形成循环时,seen设立可防止无限递归(例如,具有 autosave: true 的 Belongs_to,其目标具有指向后面的具有 autosave: true 的 has_many)。

参数:

  • doc (文档)

    要检查的文档。

  • 已看过 ( ) (默认为:Set.new

    已访问的 document(cycleguard)。

返回:

  • ( true | false )

    Whether the document needs autosaving.



46
47
48
49
50
51
# File 'lib/mongoid/association/referenced/auto_save.rb', line 46

def changed_for_autosave?(doc, 已看过 = .new)
  return false 除非 已看过.add?(doc)

  doc.new_record? || doc.改变? || doc.marked_for_destruction? ||
    autosave_children_changed?(doc, 已看过)
end