Sha256: 734bc5b2bd8902a138d15faa895d5215e841dd13c1361eb88efebbefbd9e2e1a

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Associations
      class OneProxy < Proxy
        def build(attrs={})
          instantiate_target(:new, attrs)
        end

        def create(attrs={})
          instantiate_target(:create, attrs)
        end

        def create!(attrs={})
          instantiate_target(:create!, attrs)
        end

        def replace(doc)
          load_target

          if !target.nil? && target != doc
            if options[:dependent] && target.persisted?
              case options[:dependent]
                when :delete
                  target.delete
                when :destroy
                  target.destroy
                when :nullify
                  target[foreign_key] = nil
                  target.save
              end
            end
          end

          if doc.nil?
            target.update_attributes(foreign_key => nil) unless target.nil?
          else
            proxy_owner.save unless proxy_owner.persisted?
            doc = klass.new(doc) unless doc.is_a?(klass)
            doc[foreign_key] = proxy_owner.id
            doc.save unless doc.persisted?
            loaded
            @target = doc
          end
        end

        protected
          def find_target
            target_class.first(association.query_options.merge(foreign_key => proxy_owner.id))
          end

          def instantiate_target(instantiator, attrs={})
            @target = target_class.send(instantiator, attrs.update(foreign_key => proxy_owner.id))
            loaded
            @target
          end

          def target_class
            @target_class ||= options[:class] || (options[:class_name] || association.name.to_s.camelize).constantize
          end

          def foreign_key
            options[:foreign_key] || proxy_owner.class.name.foreign_key
          end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongo_mapper-0.9.2 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-0.9.1 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-0.9.0 lib/mongo_mapper/plugins/associations/one_proxy.rb