Sha256: 785616f627d33091faff8451d742055241ee38b860e45ae3e149fd8c82b3d2e5

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

module Mongoid #:nodoc:
  module Associations #:nodoc:
    class HasManyAssociation < DelegateClass(Array) #:nodoc:

      # Creates the new association by finding the attributes in
      # the parent document with its name, and instantiating a
      # new document for each one found. These will then be put in an
      # internal array.
      #
      # This then delegated all methods to the array class since this is
      # essentially a proxy to an array itself.
      def initialize(association_name, document)
        @klass = association_name.to_s.classify.constantize
        attributes = document.attributes[association_name]
        @documents = attributes ? attributes.collect do |attribute|
          child = @klass.new(attribute)
          child.parent = document
          child
        end : []
        super(@documents)
      end

      # Builds a new Document and adds it to the association collection. The
      # document created will be of the same class as the others in the
      # association, and the attributes will be passed into the constructor.
      #
      # Returns the newly created object.
      def build(attributes)
        object = @klass.new(attributes)
        push(object)
        object
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
durran-mongoid-0.2.4 lib/mongoid/associations/has_many_association.rb
mongoid-0.2.7 lib/mongoid/associations/has_many_association.rb
mongoid-0.2.6 lib/mongoid/associations/has_many_association.rb
mongoid-0.2.5 lib/mongoid/associations/has_many_association.rb