模块:Mongoid::Association::Referenced::AutoSave
- 扩展方式:
- ActiveSupport::Concern
- 包含在:
- Mongoid::Association
- 定义于:
- lib/mongoid/association/referenced/auto_save.rb
Overview
Mongoid::Document 中包含的 Mixin 模块能力了在保存主题文档时自动将相对侧文档保存在引用关联中的功能。
类方法摘要折叠
-
。 define_autosave! (关联)⇒ 类
在关联对象的关联所属类上定义自动保存方法。
实例方法摘要折叠
-
# __auto Saving__ ⇒ 对象
开始关联的自动保存。
-
#自动保存? ⇒ true | false
用于防止关联的自动保存中出现无限循环。
-
#Changed_for_autosave? (doc,een = Set.new) ⇒ true | false
检查是否有自动保存的更改。
类方法详细信息
。 define_autosave! (关联)⇒类
在关联对象的关联所属类上定义自动保存方法。
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
开始关联的自动保存。
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
用于防止关联的自动保存中出现无限循环。
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)。
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 |