Sha256: cd9bb88cbaf0963576c78ce904306c5f10f79152b09cd464b7b881105823c76a

Contents?: true

Size: 813 Bytes

Versions: 60

Compression:

Stored size: 813 Bytes

Contents

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

          proxy_owner[association.foreign_key] = id
          proxy_owner[association.type_key_name] = type
          reset
        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

60 entries across 60 versions & 7 rubygems

Version Path
jonbell-mongo_mapper-0.8.6 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
ign-mongo_mapper-0.8.6.2 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
ign-mongo_mapper-0.8.6.1 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
ssherman-mongo_mapper-0.8.6 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.8.6 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.8.5 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
honkster-mongo_mapper-0.8.4 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.8.4 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.19 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.18 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.17 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.16 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.15 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.14 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.13 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.12 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.11 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.10 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.08.09 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-0.8.3 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb