Sha256: 5cfc26e239fb1c7fd34d1a8d92ecd89d031114aca405ef196f3bbec2c257b62c

Contents?: true

Size: 836 Bytes

Versions: 2

Compression:

Stored size: 836 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)
        
        reload_target
      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

2 entries across 2 versions & 1 rubygems

Version Path
jnunemaker-mongomapper-0.3.0 lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb
jnunemaker-mongomapper-0.3.1 lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb