Module: Mongoid::Criteria::Modifiable
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/mongoid/criteria/modifiable.rb |
Overview
Mixin module for ::Mongoid::Criteria
which adds the ability to build or create new documents with attributes initialized to the conditions of the criteria.
Instance Attribute Summary
- #create_attrs readonly Internal use only Internal use only
- #create_attrs Additional attributes to add to the Document upon creation.(Additional attributes to add to the Document upon creation.) readonly Internal use only
Instance Method Summary
-
#build(attrs = {}, &block) ⇒ Document
(also: #new)
Build a document given the selector and return it.
-
#create(attrs = {}, &block) ⇒ Document
Create a document in the database given the selector and return it.
-
#create!(attrs = {}, &block) ⇒ Document
Create a document in the database given the selector and return it.
-
#create_with(attrs = {}) ⇒ Mongoid::Criteria
Define attributes with which new documents will be created.
-
#find_or_create_by(attrs = {}, &block) ⇒ Document
Find the first
::Mongoid::Document
given the conditions, or creates a new document with the conditions that were supplied. -
#find_or_create_by!(attrs = {}, &block) ⇒ Document
Find the first
::Mongoid::Document
given the conditions, or creates a new document with the conditions that were supplied. -
#find_or_initialize_by(attrs = {}, &block) ⇒ Document
Find the first
::Mongoid::Document
given the conditions, or initializes a new document with the conditions that were supplied. -
#first_or_create(attrs = nil, &block) ⇒ Document
Find the first
::Mongoid::Document
, or creates a new document with the conditions that were supplied plus attributes. -
#first_or_create!(attrs = nil, &block) ⇒ Document
Find the first
::Mongoid::Document
, or creates a new document with the conditions that were supplied plus attributes and will raise an error if validation fails. -
#first_or_initialize(attrs = nil, &block) ⇒ Document
Find the first
::Mongoid::Document
, or initializes a new document with the conditions that were supplied plus attributes. -
#new(attrs = {}, &block)
Alias for #build.
-
#create_document(method, attrs = nil, &block) ⇒ Document
private
Internal use only
Internal use only
Create a document given the provided method and attributes from the existing selector.
-
#find_or(method, attrs = {}, &block) ⇒ Document
private
Internal use only
Internal use only
Find the first object or create/initialize it.
-
#first_or(method, attrs = {}, &block) ⇒ Document
private
Internal use only
Internal use only
Find the first document or create/initialize it.
- #invalid_embedded_doc?(value) ⇒ Boolean private
- #invalid_key?(hash, key) ⇒ Boolean private
Instance Attribute Details
#create_attrs (readonly)
# File 'lib/mongoid/criteria/modifiable.rb', line 14
attr_reader :create_attrs
#create_attrs Additional attributes to add to the Document upon creation.(Additional attributes to add to the Document upon creation.) (readonly)
[ GitHub ]# File 'lib/mongoid/criteria/modifiable.rb', line 14
attr_reader :create_attrs
Instance Method Details
#build(attrs = {}, &block) ⇒ Document Also known as: #new
Build a document given the selector and return it. Complex criteria, such as $in and $or operations will get ignored.
# File 'lib/mongoid/criteria/modifiable.rb', line 26
def build(attrs = {}, &block) create_document(:new, attrs, &block) end
#create(attrs = {}, &block) ⇒ Document
Create a document in the database given the selector and return it. Complex criteria, such as $in and $or operations will get ignored.
# File 'lib/mongoid/criteria/modifiable.rb', line 41
def create(attrs = {}, &block) create_document(:create, attrs, &block) end
#create!(attrs = {}, &block) ⇒ Document
Create a document in the database given the selector and return it. Complex criteria, such as $in and $or operations will get ignored. If validation fails, an error will be raised.
# File 'lib/mongoid/criteria/modifiable.rb', line 58
def create!(attrs = {}, &block) create_document(:create!, attrs, &block) end
#create_document(method, attrs = nil, &block) ⇒ Document (private)
Create a document given the provided method and attributes from the existing selector.
# File 'lib/mongoid/criteria/modifiable.rb', line 174
def create_document(method, attrs = nil, &block) attrs = (create_attrs || {}).merge(attrs || {}) attributes = selector.reduce(attrs) do |hash, (key, value)| unless invalid_key?(hash, key) || (value) hash[key] = value end hash end if attributes[:_parent] = parent_document attributes[:_association] = association end if polymorphic? && @criterion klass.__send__(method, attributes.merge(@criterion), &block) else klass.__send__(method, attributes, &block) end end
#create_with(attrs = {}) ⇒ Mongoid::Criteria
Define attributes with which new documents will be created.
Note that if #find_or_create_by is called after this in a method chain, the attributes in the query will override those from this method.
# File 'lib/mongoid/criteria/modifiable.rb', line 71
def create_with(attrs = {}) tap do @create_attrs ||= {} @create_attrs.update(attrs) end end
#find_or(method, attrs = {}, &block) ⇒ Document (private)
Find the first object or create/initialize it.
# File 'lib/mongoid/criteria/modifiable.rb', line 204
def find_or(method, attrs = {}, &block) where(attrs).first || send(method, attrs, &block) end
#find_or_create_by(attrs = {}, &block) ⇒ Document
Find the first ::Mongoid::Document
given the conditions, or creates a new document with the conditions that were supplied.
#find_or_create_by!(attrs = {}, &block) ⇒ Document
Find the first ::Mongoid::Document
given the conditions, or creates a new document with the conditions that were supplied. If validation fails an exception will be raised.
#find_or_initialize_by(attrs = {}, &block) ⇒ Document
Find the first ::Mongoid::Document
given the conditions, or initializes a new document with the conditions that were supplied.
#first_or(method, attrs = {}, &block) ⇒ Document (private)
Find the first document or create/initialize it.
# File 'lib/mongoid/criteria/modifiable.rb', line 219
def first_or(method, attrs = {}, &block) first || create_document(method, attrs, &block) end
#first_or_create(attrs = nil, &block) ⇒ Document
Find the first ::Mongoid::Document
, or creates a new document with the conditions that were supplied plus attributes.
#first_or_create!(attrs = nil, &block) ⇒ Document
Find the first ::Mongoid::Document
, or creates a new document with the conditions that were supplied plus attributes and will raise an error if validation fails.
#first_or_initialize(attrs = nil, &block) ⇒ Document
Find the first ::Mongoid::Document
, or initializes a new document with the conditions that were supplied plus attributes.
#invalid_embedded_doc?(value) ⇒ Boolean (private)
# File 'lib/mongoid/criteria/modifiable.rb', line 231
def (value) # @todo Change this to BSON::String::ILLEGAL_KEY when ruby driver 2.3.0 is # released and mongoid is updated to depend on driver >= 2.3.0 value.is_a?(Hash) && value.any? do |key, v| key.to_s =~ Mongoid::Document::ILLEGAL_KEY || (v) end end
#invalid_key?(hash, key) ⇒ Boolean (private)
# File 'lib/mongoid/criteria/modifiable.rb', line 225
def invalid_key?(hash, key) # @todo Change this to BSON::String::ILLEGAL_KEY when ruby driver 2.3.0 is # released and mongoid is updated to depend on driver >= 2.3.0 key.to_s =~ Mongoid::Document::ILLEGAL_KEY || hash.key?(key.to_sym) || hash.key?(key) end
#new(attrs = {}, &block)
Alias for #build.
# File 'lib/mongoid/criteria/modifiable.rb', line 29
alias :new :build