Sha256: 52e2d9d63875938cbdbb78687c6c8346620298f03ee0a04866fc1f518b02fb0d

Contents?: true

Size: 767 Bytes

Versions: 10

Compression:

Stored size: 767 Bytes

Contents

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

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

        protected
          def find_target
            return nil if association_class.nil? || owner[reflection.foreign_key].nil?
            association_class.first(:id => owner[reflection.foreign_key])
          end

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

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
jmonteiro-mongo_mapper-0.1.7 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.1.18 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
jmonteiro-mongo_mapper-0.1.6 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
jmonteiro-mongo_mapper-0.1.5 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.1.17 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
jmonteiro-mongo_mapper-0.1.4 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
jmonteiro-mongo_mapper-0.1.2 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
jmonteiro-mongo_mapper-0.1.1 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
jmonteiro-mongo_mapper-0.1.0 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
mongo_mapper-unstable-2010.1.12 lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb