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.
Class Method Summary
-
.define_builder!(association) ⇒ Class
private
Defines a builder method.
-
.define_creator!(association) ⇒ Class
private
Defines a creator method.
Instance Method Summary
-
#parse_args(*args) ⇒ Array<Hash>
private
Parse out the attributes and the options from the args passed to a build_ or create_ methods.
Class Method Details
.define_builder!(association) ⇒ Class
(private)
Defines a builder method. This is defined as #build_name.
# File 'lib/mongoid/association/builders.rb', line 46
def self.define_builder!(association) association.inverse_class.tap do |klass| klass.re_define_method("build_#{association.name}") do |*args| attributes, = parse_args(*args) document = Factory.execute_build(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.
# File 'lib/mongoid/association/builders.rb', line 70
def self.define_creator!(association) association.inverse_class.tap do |klass| klass.re_define_method("create_#{association.name}") do |*args| attributes, = parse_args(*args) document = Factory.execute_build(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<Hash> (private)
Parse out the attributes and the options from the args passed to a build_ or create_ methods.
# File 'lib/mongoid/association/builders.rb', line 34
def parse_args(*args) [ args.first || {}, args.size > 1 ? args[1] : {} ] end