Sha256: b44b29c5b8001c574e36aae5071e89000057dbdd3fbe9802326676cb1c54d264

Contents?: true

Size: 1.68 KB

Versions: 25

Compression:

Stored size: 1.68 KB

Contents

module MongoMapper
  module Plugins
    module Associations
      class OneProxy < Proxy
        undef_method :object_id
        
        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.new?
              case options[:dependent]
                when :delete
                  target.delete
                when :destroy
                  target.destroy
                when :nullify
                  target[foreign_key] = nil
                  target.save
              end
            end
          end

          reset

          unless doc.nil?
            owner.save if owner.new?
            doc[foreign_key] = owner.id
            doc.save if doc.new?
            loaded
            @target = doc
          end
        end

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

          def instantiate_target(instantiator, attrs={})
            @target = target_class.send(instantiator, attrs.update(foreign_key => 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] || owner.class.name.foreign_key
          end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 4 rubygems

Version Path
mongo_mapper-0.7.3 lib/mongo_mapper/plugins/associations/one_proxy.rb
numon-0.0.1 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-0.7.2 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-0.7.1 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.3.8 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.3.5 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.3.4 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.3.3 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-rails3-0.7.0.1 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.28 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.27 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.26 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.25 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.24 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.23 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.22 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.19 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.18 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.17 lib/mongo_mapper/plugins/associations/one_proxy.rb
mongo_mapper-unstable-2010.2.16 lib/mongo_mapper/plugins/associations/one_proxy.rb