Sha256: 69f1639b675a6e048b87f299f155f8315833c60ac480b5a7fdc8cbdd18e7c4b7
Contents?: true
Size: 1.52 KB
Versions: 9
Compression:
Stored size: 1.52 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 # Finds a document in this association. # If :all is passed, returns all the documents # If an id is passed, will return the document for that id. def find(param) return @documents if param == :all return detect { |document| document.id == param } end end end end
Version data entries
9 entries across 9 versions & 1 rubygems