Sha256: 2a7ef923a447ef8e9b728f591814cedcd8be0847eca53c4bee55cd7f41a57484

Contents?: true

Size: 774 Bytes

Versions: 3

Compression:

Stored size: 774 Bytes

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Associations
      class OneEmbeddedPolymorphicProxy < OneEmbeddedProxy
        def replace(value)
          @value = value.respond_to?(:attributes) ? value.attributes.merge(association.type_key_name => value.class.name) : value
          reset
        end

        protected
          def find_target
            if @value
              child = polymorphic_class(@value).load(@value, true)
              assign_references(child)
              child
            end
          end

          def polymorphic_class(doc)
            if class_name = doc[association.type_key_name]
              class_name.constantize
            else
              klass
            end
          end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongo_mapper-0.13.1 lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb
mongo_mapper-0.13.0 lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb
mongo_mapper-0.13.0.beta2 lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb