Sha256: c839dd476c574a4373c94e15086e96574e70d9bd4966f5c053596dc4fa4e2fde

Contents?: true

Size: 819 Bytes

Versions: 3

Compression:

Stored size: 819 Bytes

Contents

module MongoMapper
  module Associations
    class BelongsToPolymorphicProxy < Proxy
      def replace(doc)        
        if doc
          doc.save if doc.new?
          id, type = doc.id, doc.class.name
        end
        
        @owner.send("#{@association.belongs_to_key_name}=", id)
        @owner.send("#{@association.type_key_name}=", type)
        reset
      end
      
      protected
        def find_target
          proxy_class.find(proxy_id) if proxy_id && proxy_class
        end
        
        def proxy_id
          @proxy_id ||= @owner.send(@association.belongs_to_key_name)
        end
        
        def proxy_class
          @proxy_class ||= begin
            klass = @owner.send(@association.type_key_name)
            klass && klass.constantize
          end
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
djsun-mongomapper-0.3.1.1 lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb
djsun-mongomapper-0.3.1 lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb
jnunemaker-mongomapper-0.3.2 lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb