Module: ActiveModel::Embedding::Associations

Defined in:
lib/active_model/embedding/associations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_model/embedding/associations.rb', line 6

def self.included(klass)
  klass.class_eval do
    extend ClassMethods

    class_variable_set :@@embedded_associations, []

    around_save :save_embedded_documents

    def save_embedded_documents
      klass = self.class

      if klass.embedded_associations.present?
        associations = klass.embedded_associations

        targets = associations.map do |association_name|
          public_send association_name
        end.compact

        targets.each(&:save)
      end

      yield
    end
  end
end