Sha256: 759e6cc5614199130565363f6208588f269c78703333db9eea4e5d7caa75dcf4

Contents?: true

Size: 1.9 KB

Versions: 10

Compression:

Stored size: 1.9 KB

Contents

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

      attr_reader :document, :options

      # Creates the new association by setting the internal
      # document 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:
      #
      # document: The parent +Document+
      # options: The association options
      def initialize(document, options)
        @document, @options = document, 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)
        @document
      end

      # Delegate all missing methods over to the parent +Document+.
      def method_missing(name, *args, &block)
        @document.send(name, *args, &block)
      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)
          parent = document.parent
          parent.nil? ? nil : new(parent, 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.
        def update(parent, child, options)
          child.parentize(parent, options.inverse_of)
          child.notify
          parent
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mongoid-0.10.6 lib/mongoid/associations/belongs_to.rb
mongoid-0.10.5 lib/mongoid/associations/belongs_to.rb
mongoid-0.10.4 lib/mongoid/associations/belongs_to.rb
mongoid-0.10.3 lib/mongoid/associations/belongs_to.rb
mongoid-0.10.2 lib/mongoid/associations/belongs_to.rb
mongoid-0.10.1 lib/mongoid/associations/belongs_to.rb
mongoid-0.10.0 lib/mongoid/associations/belongs_to.rb
mongoid-0.9.12 lib/mongoid/associations/belongs_to.rb
mongoid-0.9.11 lib/mongoid/associations/belongs_to.rb
mongoid-0.9.10 lib/mongoid/associations/belongs_to.rb