Sha256: 5a5274394011aa57f8b4d2a7d7ad2a2e53adcca5fe5b7156eef1982615ee84b8

Contents?: true

Size: 1 KB

Versions: 8

Compression:

Stored size: 1 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module EmbeddedDocument
      def self.configure(model)
        model.class_eval do
          attr_accessor :_parent_document
        end
      end

      module ClassMethods
        def embeddable?
          true
        end

        def embedded_in(owner_name)
          define_method(owner_name) { _parent_document }
        end
      end

      module InstanceMethods
        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|
            @_new = false if result
          end
        end

        def save!(options={})
          _root_document.try(:save, options).tap do |result|
            @_new = false if result
          end
        end

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

Version data entries

8 entries across 8 versions & 5 rubygems

Version Path
jonbell-mongo_mapper-0.8.6 lib/mongo_mapper/plugins/embedded_document.rb
ign-mongo_mapper-0.8.6.2 lib/mongo_mapper/plugins/embedded_document.rb
ign-mongo_mapper-0.8.6.1 lib/mongo_mapper/plugins/embedded_document.rb
ssherman-mongo_mapper-0.8.6 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.8.6 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.8.5 lib/mongo_mapper/plugins/embedded_document.rb
honkster-mongo_mapper-0.8.4 lib/mongo_mapper/plugins/embedded_document.rb
mongo_mapper-0.8.4 lib/mongo_mapper/plugins/embedded_document.rb