Sha256: c66e123853147c913d253ee003a9f0fff2468aebe914a1b23bbdc1478aa11132

Contents?: true

Size: 1.02 KB

Versions: 13

Compression:

Stored size: 1.02 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 :create, :after do |document|
              relation = document.send(metadata.name)
              relation.to_a.each(&:save) if relation
            end
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
mongoid-braxton-2.0.2 lib/mongoid/relations/auto_save.rb
mongoid-2.0.2 lib/mongoid/relations/auto_save.rb
mongoid-2.0.1 lib/mongoid/relations/auto_save.rb
mongoid-2.0.0 lib/mongoid/relations/auto_save.rb
mongoid-2.0.0.rc.8 lib/mongoid/relations/auto_save.rb
mongoid-2.0.0.rc.7 lib/mongoid/relations/auto_save.rb
stonegao-mongoid-2.0.0.rc.6 lib/mongoid/relations/auto_save.rb
mongoid-2.0.0.rc.6 lib/mongoid/relations/auto_save.rb
mongoid-2.0.0.rc.5 lib/mongoid/relations/auto_save.rb
mongoid-2.0.0.rc.4 lib/mongoid/relations/auto_save.rb
mongoid-2.0.0.rc.3 lib/mongoid/relations/auto_save.rb
mongoid-2.0.0.rc.2 lib/mongoid/relations/auto_save.rb
mongoid-2.0.0.rc.1 lib/mongoid/relations/auto_save.rb