Sha256: 0fcbbc10eb69f3d1d9986f787a2c9bbb2c9298351685afb251bff1474a8f2e21

Contents?: true

Size: 960 Bytes

Versions: 4

Compression:

Stored size: 960 Bytes

Contents

module MongoMapper
  module Associations
    class PolymorphicHasManyEmbeddedProxy < Proxy
      def replace(v)
        @_values = v.map do |e|
          ref_type = "#{@association.name}_type"
          if e.kind_of?(EmbeddedDocument)  
            e.class.send(:key, ref_type, String)
            {ref_type => e.class.name}.merge(e.attributes)
          else
            e
          end
        end
        
        @target = nil

        reload_target
      end

      protected
      def find_target
        (@_values || []).map do |e|
          ref_type = "#{@association.name}_type"
          class_name = e[ref_type]
          if class_name
            current = Kernel
            parts = class_name.split("::")
            parts.each do |p|
              current = current.const_get(p)
            end
            klass = current
          else
            @association.klass
          end
          klass.new(e)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fcoury-mongomapper-0.3.1 lib/mongomapper/associations/polymorphic_has_many_embedded_proxy.rb
fcoury-mongomapper-0.3.3 lib/mongomapper/associations/polymorphic_has_many_embedded_proxy.rb
fcoury-mongomapper-0.3.4 lib/mongomapper/associations/polymorphic_has_many_embedded_proxy.rb
fcoury-mongomapper-0.3.5 lib/mongomapper/associations/polymorphic_has_many_embedded_proxy.rb