Module: Mongoid::Serializable
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
ActiveSupport::Concern
|
|
Defined in: | lib/mongoid/serializable.rb |
Overview
This module provides the extra behavior for including associations in JSON and XML serialization.
Instance Method Summary
-
#serializable_hash(options = nil) ⇒ Hash
Gets the document as a serializable hash, used by ActiveModel’s JSON serializer.
-
#field_names(options) ⇒ Array<String>
private
Internal use only
Internal use only
Get the names of all fields that will be serialized.
-
#relation_names(inclusions) ⇒ Array<Symbol>
private
Since the inclusions can be a hash, symbol, or array of symbols, this is provided as a convenience to parse out the names.
-
#relation_options(inclusions, options, name) ⇒ Hash
private
Since the inclusions can be a hash, symbol, or array of symbols, this is provided as a convenience to parse out the options.
-
#serialize_attribute(attrs, name, names, options) ⇒ Object
private
Internal use only
Internal use only
Serialize a single attribute.
-
#serialize_relations(attributes = {}, options = {})
private
For each of the provided include options, get the association needed and provide it in the hash.
DSL Calls
included
We need to redefine where the JSON configuration is getting defined, similar to ActiveRecord
.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# File 'lib/mongoid/serializable.rb', line 13
included do class << self # These methods are previously defined by ActiveModel which we override to include default behavior. remove_method :include_root_in_json if method_defined?(:include_root_in_json) remove_method :include_root_in_json= if method_defined?(:include_root_in_json=) def include_root_in_json @include_root_in_json.nil? ? ::Mongoid.include_root_in_json : @include_root_in_json end def include_root_in_json=(new_value) @include_root_in_json = new_value end end end
Instance Method Details
#field_names(options) ⇒ Array<String> (private)
Get the names of all fields that will be serialized.
# File 'lib/mongoid/serializable.rb', line 79
def field_names( ) names = (as_attributes.keys + attribute_names).uniq.sort only = Array.wrap( [:only]).map(&:to_s) except = Array.wrap( [:except]).map(&:to_s) except |= [self.class.discriminator_key] unless Mongoid.include_type_for_serialization if !only.empty? names &= only elsif !except.empty? names -= except end names end
#relation_names(inclusions) ⇒ Array<Symbol> (private)
Since the inclusions can be a hash, symbol, or array of symbols, this is provided as a convenience to parse out the names.
#relation_options(inclusions, options, name) ⇒ Hash (private)
Since the inclusions can be a hash, symbol, or array of symbols, this is provided as a convenience to parse out the options.
# File 'lib/mongoid/serializable.rb', line 166
def (inclusions, , name) if inclusions.is_a?(Hash) inclusions[name] else { except: [:except], only: [:only] } end end
#serializable_hash(options = nil) ⇒ Hash
Gets the document as a serializable hash, used by ActiveModel’s JSON serializer.
# File 'lib/mongoid/serializable.rb', line 50
def serializable_hash( = nil) ||= {} attrs = {} names = field_names( ) method_names = Array.wrap( [:methods]).map do |name| name.to_s if respond_to?(name) end.compact (names + method_names).each do |name| without_autobuild do serialize_attribute(attrs, name, names, ) end end serialize_relations(attrs, ) if [:include] attrs end
#serialize_attribute(attrs, name, names, options) ⇒ Object
(private)
Serialize a single attribute. Handles associations, fields, and dynamic attributes.
# File 'lib/mongoid/serializable.rb', line 108
def serialize_attribute(attrs, name, names, ) if relations.key?(name) value = send(name) attrs[name] = value ? value.serializable_hash( ) : nil elsif names.include?(name) && !fields.key?(name) attrs[name] = read_raw_attribute(name) elsif !attribute_missing?(name) attrs[name] = send(name) end end
#serialize_relations(attributes = {}, options = {}) (private)
For each of the provided include options, get the association needed and provide it in the hash.
# File 'lib/mongoid/serializable.rb', line 131
def serialize_relations(attributes = {}, = {}) inclusions = [:include] relation_names(inclusions).each do |name| association = relations[name.to_s] if association && relation = send(association.name) attributes[association.name.to_s] = relation.serializable_hash( (inclusions, , name)) end end end