Sha256: 0f92321e62421506f4f892975043b105c55d164cd59676635fe729e47f242ffb

Contents?: true

Size: 1.89 KB

Versions: 11

Compression:

Stored size: 1.89 KB

Contents

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

      # Initializing a related association only requires looking up the object
      # by its id.
      #
      # Options:
      #
      # document: The +Document+ that contains the relationship.
      # options: The association +Options+.
      def initialize(document, foreign_key, options, target = nil)
        @options = options
        @target = target || options.klass.find(foreign_key)
        extends(options)
      end

      class << self
        # Instantiate a new +BelongsToRelated+ or return nil if the foreign key is
        # nil. It is preferrable to use this method over the traditional call
        # to new.
        #
        # Options:
        #
        # document: The +Document+ that contains the relationship.
        # options: The association +Options+.
        def instantiate(document, options, target = nil)
          foreign_key = document.send(options.foreign_key)
          foreign_key.blank? ? nil : new(document, foreign_key, options, target)
        end

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

        # Perform an update of the relationship of the parent and child. This
        # will assimilate the child +Document+ into the parent's object graph.
        #
        # Options:
        #
        # related: The related object
        # parent: The parent +Document+ to update.
        # options: The association +Options+
        #
        # Example:
        #
        # <tt>BelongsToRelated.update(game, person, options)</tt>
        def update(target, parent, options)
          if target
            parent.send("#{options.foreign_key}=", target.id)
            return instantiate(parent, options, target)
          end
          target
        end
      end

    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
mongoid-1.2.8 lib/mongoid/associations/belongs_to_related.rb
mongoid-1.2.7 lib/mongoid/associations/belongs_to_related.rb
mongoid-pre-2.0.0.pre lib/mongoid/associations/belongs_to_related.rb
mongoid-1.2.6 lib/mongoid/associations/belongs_to_related.rb
mongoid-1.2.5 lib/mongoid/associations/belongs_to_related.rb
mongoid-1.2.4 lib/mongoid/associations/belongs_to_related.rb
mongoid-1.2.3 lib/mongoid/associations/belongs_to_related.rb
mongoid-1.2.2 lib/mongoid/associations/belongs_to_related.rb
mongoid-1.2.1 lib/mongoid/associations/belongs_to_related.rb
mongoid-1.2.0 lib/mongoid/associations/belongs_to_related.rb
mongoid-1.1.4 lib/mongoid/associations/belongs_to_related.rb