123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Association::Builders

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: lib/mongoid/association/builders.rb

Overview

This module is responsible for defining the build and create methods used in one to one associations.

Examples:

Methods that get created.

class Person
  include Mongoid::Document
  embeds_one :name
end

# The following methods get created:
person.build_name({ :first_name => "Durran" })
person.create_name({ :first_name => "Durran" })

Class Method Summary

Instance Method Summary

Class Method Details

.define_builder!(association) ⇒ Class (private)

Defines a builder method. This is defined as #build_name.

Examples:

Person.define_builder!(association)

Parameters:

Returns:

  • (Class)

    The class being set up.

[ GitHub ]

  
# File 'lib/mongoid/association/builders.rb', line 47

def self.define_builder!(association)
  association.inverse_class.tap do |klass|
    klass.re_define_method("build_#{association.name}") do |*args|
      attributes, type, _opts = parse_args(args)

      document = Factory.execute_build(type || association.relation_class, attributes, execute_callbacks: false)
      _building do
        child = send("#{association.name}=", document)
        child.run_pending_callbacks
        child.run_callbacks(:build)
        child
      end
    end
  end
end

.define_creator!(association) ⇒ Class (private)

Defines a creator method. This is defined as #create_name. After the object is built it will immediately save.

Examples:

Person.define_creator!(association)

Parameters:

Returns:

  • (Class)

    The class being set up.

[ GitHub ]

  
# File 'lib/mongoid/association/builders.rb', line 72

def self.define_creator!(association)
  association.inverse_class.tap do |klass|
    klass.re_define_method("create_#{association.name}") do |*args|
      attributes, type, _opts = parse_args(args)

      document = Factory.execute_build(type || association.relation_class, attributes, execute_callbacks: false)
      doc = _assigning do
        send("#{association.name}=", document)
      end
      doc.run_pending_callbacks
      doc.save
      save if new_record? && association.stores_foreign_key?
      doc
    end
  end
end

Instance Method Details

#parse_args(args) ⇒ Array (private)

Parses the arguments passed to the build and create methods. The first argument is always the attributes (defaulting to {} if not given). The last argument is always the options hash, if the last argument is a hash. The first of any remaining arguments is the type.

Parameters:

  • args (Array)

    The arguments passed to the method.

Returns:

  • (Array)

    An array containing the attributes, type, and options.

[ GitHub ]

  
# File 'lib/mongoid/association/builders.rb', line 31

def parse_args(args)
  attributes = args.shift || {}
  opts = args.last.is_a?(Hash) ? args.pop : {}
  type = args.shift

  [ attributes, type, opts ]
end