Sha256: 47d9eb9dbe0c7b20ce49173a30988045e85614f8eaa105ae76111210c3ceb7c6

Contents?: true

Size: 1.14 KB

Versions: 164

Compression:

Stored size: 1.14 KB

Contents

# encoding: utf-8
module Mongoid # :nodoc:
  module Relations #:nodoc:

    # This module contains the behaviour for auto-saving relations in
    # different collections.
    module AutoSave
      extend ActiveSupport::Concern

      module ClassMethods #:nodoc:

        # Set up the autosave behaviour for references many and references one
        # relations. When the option is set to true, these relations will get
        # saved automatically when the parent is first saved, but not if the
        # parent already exists in the database.
        #
        # @example Set up autosave options.
        #   Person.autosave(metadata)
        #
        # @param [ Metadata ] metadata The relation metadata.
        #
        # @since 2.0.0.rc.1
        def autosave(metadata)
          if metadata.autosave?
            set_callback :save, :after do |document|
              relation = document.send(metadata.name)
              if relation
                (relation.do_or_do_not(:in_memory) || Array.wrap(relation)).each do |doc|
                  doc.save
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

164 entries across 72 versions & 3 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/mongoid-2.4.7/lib/mongoid/relations/auto_save.rb
classiccms-0.7.5 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/relations/auto_save.rb
classiccms-0.7.5 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/relations/auto_save.rb
classiccms-0.7.4 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/relations/auto_save.rb
classiccms-0.7.4 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/relations/auto_save.rb
classiccms-0.7.4 vendor/bundle/gems/mongoid-2.4.7/lib/mongoid/relations/auto_save.rb
classiccms-0.7.3 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/relations/auto_save.rb
classiccms-0.7.3 vendor/bundle/gems/mongoid-2.4.7/lib/mongoid/relations/auto_save.rb
classiccms-0.7.3 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/relations/auto_save.rb
mongoid-2.8.1 lib/mongoid/relations/auto_save.rb
mongoid-2.8.0 lib/mongoid/relations/auto_save.rb
classiccms-0.7.2 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/relations/auto_save.rb
classiccms-0.7.2 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/relations/auto_save.rb
classiccms-0.7.2 vendor/bundle/gems/mongoid-2.4.7/lib/mongoid/relations/auto_save.rb
classiccms-0.7.1 vendor/bundle/gems/mongoid-2.4.7/lib/mongoid/relations/auto_save.rb
classiccms-0.7.1 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/relations/auto_save.rb
classiccms-0.7.1 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/relations/auto_save.rb
classiccms-0.7.0 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/relations/auto_save.rb
classiccms-0.7.0 vendor/bundle/gems/mongoid-2.4.7/lib/mongoid/relations/auto_save.rb
classiccms-0.7.0 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/relations/auto_save.rb