Sha256: ec443de838779c60a94fdc42b67b2e005774b302292c01ca7b76e38f5d7dff76

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module EmbeddedDocument
      extend ActiveSupport::Concern

      included do
        attr_accessor :_parent_document
      end

      module ClassMethods
        def embeddable?
          true
        end

        def embedded_in(owner_name)
          alias_method owner_name, :_parent_document
        end
      end

      def new?
        _root_document.try(:new?) || @_new
      end

      def destroyed?
        !!_root_document.try(:destroyed?)
      end

      def save(options={})
        _root_document.try(:save, options).tap do |result|
          persist(options) if result
        end
      end

      def save!(options={})
        valid? || raise(DocumentNotValid.new(self))
        _root_document.try(:save!, options).tap do |result|
          persist(options) if result
        end
      end

      def persist(options={})
        @_new = false
        clear_changes if respond_to?(:clear_changes)
        save_to_collection(options)
      end

      def _root_document
        @_root_document ||= _parent_document.try(:_root_document)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
mongo_mapper-0.14.0 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.14.0.rc1 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.13.1 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.13.0 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.13.0.beta2 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.13.0.beta1 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.12.0 lib/mongo_mapper/plugins/embedded_document.rb
lookout-mongo_mapper-0.11.3 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.11.2 lib/mongo_mapper/plugins/embedded_document.rb
jamieorc-mongo_mapper-0.11.1.1 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.11.1 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.11.0 lib/mongo_mapper/plugins/embedded_document.rb