Sha256: 1f99df73b52eb10012ba034e4b6704565849553765658922a5cc6d502187f1b0

Contents?: true

Size: 822 Bytes

Versions: 3

Compression:

Stored size: 822 Bytes

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Associations
      class BelongsToPolymorphicProxy < Proxy
        undef_method :object_id

        def replace(doc)
          if doc
            doc.save if doc.new?
            id, type = doc.id, doc.class.name
          end

          owner[association.foreign_key] = id
          owner[association.type_key_name] = type
          reset
        end

        protected
          def find_target
            return nil if association_class.nil? || owner[association.foreign_key].nil?
            association_class.find_by_id(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

3 entries across 3 versions & 2 rubygems

Version Path
mongo_mapper_ign-0.7.7 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
pwnash-mongo_mapper-0.7.6 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper_ign-0.7.6 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb