Sha256: d7cf1dd09b42e79d334dcac7b468380d2ec1e5421dd7151842518ee2c33438b5

Contents?: true

Size: 853 Bytes

Versions: 1

Compression:

Stored size: 853 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

1 entries across 1 versions & 1 rubygems

Version Path
mongo_mapper_ign-0.7.8 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb