모듈: Mongoid::Association::Referenced::Auto Save

확장자:
ActiveSupport::Concern
포함 항목:
Mongoid::Association
다음에 정의됨:
lib/mongoid/association/referenced/auto_save.rb

개요

Mongoid::Document에 포함된 Mixin 모듈은 주제 문서 를 저장할 때 참조된 연관 관계에서 반대편 문서를 자동으로 저장하는 기능 을 추가합니다.

클래스 메서드 요약 접기

인스턴스 메서드 요약 접기

클래스 메서드 세부 정보

.정의_자동 저장!(association) ⇒ 클래스

연결된 객체 에 대한 연결의 소유 클래스에서 자동 저장 메서드를 정의합니다.

예시:

자동 저장 메서드를 정의합니다:

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

매개변수:

반환합니다:

  • (클래스)

    연결의 소유자 클래스입니다.



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

def self.정의_자동 저장!(연관 관계)
  연관 관계.inverse_class. do |class|
    save_method = :"autosave_documents_for_#{연관 관계.이름}"
    class.send(:define_method, save_method) do
      만약 before_callback_halted?
        self.before_callback_halted = 거짓
      other
        __자동 저장__ do
          만약 assoc_value = ivar(연관 관계.이름)
            배열(assoc_value). do |doc|
              다음 하지 않는 한 change_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
    class.after_persist_parent save_method, 그렇지 않은 경우: :autosaved?
  end
end

인스턴스 메서드 세부 정보

#__autosaving__Object

관련 자동 저장을 시작합니다.

예시:

Begin autosave.

document.__autosaving__


26
27
28
29
30
31
# 파일 'lib/mongoid/association/referenced/auto_save.rb', 줄 26

def __자동 저장__
  스레드.begin_autosave(self)
  yield
보장
  스레드.exit_autosave(self)
end

#자동 저장?true | false

관련 자동 저장에서 무한 루프를 방지하는 데 사용됩니다.

예시:

문서가 자동으로 저장되나요?

document.autosaved?

반환합니다:

  • (true | false)

    문서가 이미 자동 저장되었나요?



18
19
20
# 파일 'lib/mongoid/association/referenced/auto_save.rb', 줄 18

def 자동 저장?
  스레드.자동 저장?(self)
end

#change_for_autosave?(doc, see = Set.new) ⇒ true | false

자동 저장에 대한 변경 사항이 있는지 확인합니다. 문서 새롭거나, 변경되었거나, 폐기로 표시된 경우 또는 autosave: true를 사용하여 메모리 내 참조된 하위 문서가 동일한 조건을 재귀적으로 충족하는 경우 true를 반환합니다.

본 설정하다 자동 저장 연관 관계가 주기를 형성할 때 무한 재귀를 방지합니다(예: autosave: true가 있는 대상의 has_many가 있고 autosave: true가 다시 가리키는 has_many가 있는 경우).

매개변수:

  • doc (문서)

    확인할 문서 입니다.

  • (설정) (기본값: Set.new)

    이미 방문한 문서(사이클 가드).

반환합니다:

  • (true | false)

    문서 에 자동 저장이 필요한지 여부.



46
47
48
49
50
51
# 파일 'lib/mongoid/association/referenced/auto_save.rb', 줄 46

def change_for_autosave?(doc,  = 세트.신규)
  반환 거짓 하지 않는 한 .add?(doc)

  doc.new_record? || doc.변경되었나요? || doc.Marked_for_destruction? ||
    autosave_children_changed?(doc, )
end