module ActiveModel::Embedding::Associations

Public Class Methods

included(klass) click to toggle source
# 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

Public Instance Methods

save_embedded_documents() { || ... } click to toggle source
# File lib/active_model/embedding/associations.rb, line 14
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