Sha256: e045fccf5e7ae594dce7419b626e37f7bcd7b5e67af206a640a53e4bf3af5a48

Contents?: true

Size: 926 Bytes

Versions: 15

Compression:

Stored size: 926 Bytes

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Associations
      class BelongsToPolymorphicProxy < Proxy
        def replace(doc)
          if doc
            doc.save unless doc.persisted?
            id, type = doc.id, doc.class.name
          end

          proxy_owner[association.foreign_key] = id
          proxy_owner[association.type_key_name] = type
          reset
          unless doc.nil?
            loaded
            @target = doc
          end
          @target
        end

        protected
          def find_target
            return nil if association_class.nil? || proxy_owner[association.foreign_key].nil?
            association_class.find_by_id(proxy_owner[association.foreign_key])
          end

          def association_class
            proxy_owner[association.type_key_name] ? proxy_owner[association.type_key_name].constantize : nil
          end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
mongo_mapper-0.13.1 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.13.0 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.13.0.beta2 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.13.0.beta1 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.12.0 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
lookout-mongo_mapper-0.11.3 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.11.2 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
jamieorc-mongo_mapper-0.11.1.1 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.11.1 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.11.0 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.10.1 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.10.0 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.9.2 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.9.1 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.9.0 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb