Sha256: 6fdd8a91649c8f7ceb413d9cdb730b26a4579bbf69adce408f88519605a06506

Contents?: true

Size: 1.81 KB

Versions: 17

Compression:

Stored size: 1.81 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Associations #:nodoc:
    class BelongsTo #:nodoc:
      include Proxy

      # Creates the new association by setting the internal
      # target as the passed in Document. This should be the
      # parent.
      #
      # All method calls on this object will then be delegated
      # to the internal document itself.
      #
      # Options:
      #
      # target: The parent +Document+
      # options: The association options
      def initialize(target, options)
        @target, @options = target, options
        extends(options)
      end

      # Returns the parent document. The id param is present for
      # compatibility with rails, however this could be overwritten
      # in the future.
      def find(id)
        @target
      end

      class << self
        # Creates the new association by setting the internal
        # document as the passed in Document. This should be the
        # parent.
        #
        # Options:
        #
        # document: The parent +Document+
        # options: The association options
        def instantiate(document, options)
          target = document._parent
          target.nil? ? nil : new(target, options)
        end

        # Returns the macro used to create the association.
        def macro
          :belongs_to
        end

        # Perform an update of the relationship of the parent and child. This
        # is initialized by setting a parent object as the association on the
        # +Document+. Will properly set a has_one or a has_many.
        #
        # Returns:
        #
        # A new +BelongsTo+ association proxy.
        def update(target, child, options)
          child.parentize(target, options.inverse_of)
          child.notify
          instantiate(child, options)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
mongoid-1.2.14 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.13 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.12 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.11 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.10 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.9 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.8 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.7 lib/mongoid/associations/belongs_to.rb
mongoid-pre-2.0.0.pre lib/mongoid/associations/belongs_to.rb
mongoid-1.2.6 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.5 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.4 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.3 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.2 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.1 lib/mongoid/associations/belongs_to.rb
mongoid-1.2.0 lib/mongoid/associations/belongs_to.rb
mongoid-1.1.4 lib/mongoid/associations/belongs_to.rb