123456789_123456789_123456789_123456789_123456789_

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

DSL Calls

included

We need to redefine where the JSON configuration is getting defined, similar to ActiveRecord.

[ GitHub ]


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)

This method is for internal use only.

Get the names of all fields that will be serialized.

Examples:

Get all the field names.

document.send(:field_names)

Returns:

[ GitHub ]

  
# File 'lib/mongoid/serializable.rb', line 79

def field_names(options)
  names = (as_attributes.keys + attribute_names).uniq.sort

  only = Array.wrap(options[:only]).map(&:to_s)
  except = Array.wrap(options[: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.

Examples:

Get the association names.

document.relation_names(:include => [ :addresses ])

Parameters:

Returns:

  • (Array<Symbol>)

    The names of the included associations.

[ GitHub ]

  
# File 'lib/mongoid/serializable.rb', line 151

def relation_names(inclusions)
  inclusions.is_a?(Hash) ? inclusions.keys : Array.wrap(inclusions)
end

#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.

Examples:

Get the association options.

document.relation_names(:include => [ :addresses ])

Parameters:

Returns:

  • (Hash)

    The options for the association.

[ GitHub ]

  
# File 'lib/mongoid/serializable.rb', line 166

def relation_options(inclusions, options, name)
  if inclusions.is_a?(Hash)
    inclusions[name]
  else
    { except: options[:except], only: options[:only] }
  end
end

#serializable_hash(options = nil) ⇒ Hash

Gets the document as a serializable hash, used by ActiveModel’s JSON serializer.

Examples:

Get the serializable hash.

document.serializable_hash

Get the serializable hash with options.

document.serializable_hash(:include => :addresses)

Parameters:

  • options (Hash) (defaults to: nil)

    The options to pass.

Options Hash (options):

Returns:

  • (Hash)

    The document, ready to be serialized.

[ GitHub ]

  
# File 'lib/mongoid/serializable.rb', line 50

def serializable_hash(options = nil)
  options ||= {}
  attrs = {}

  names = field_names(options)

  method_names = Array.wrap(options[: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, options)
    end
  end
  serialize_relations(attrs, options) if options[:include]
  attrs
end

#serialize_attribute(attrs, name, names, options) ⇒ Object (private)

This method is for internal use only.

Serialize a single attribute. Handles associations, fields, and dynamic attributes.

Examples:

Serialize the attribute.

document.serialize_attribute({}, "id" , [ "id" ])

Parameters:

  • attrs (Hash)

    The attributes.

  • name (String)

    The attribute name.

  • names (Array<String>)

    The names of all attributes.

  • options (Hash)

    The options.

Returns:

  • (Object)

    The attribute.

[ GitHub ]

  
# File 'lib/mongoid/serializable.rb', line 108

def serialize_attribute(attrs, name, names, options)
  if relations.key?(name)
    value = send(name)
    attrs[name] = value ? value.serializable_hash(options) : 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.

Examples:

Serialize the included associations.

document.serialize_relations({}, :include => :addresses)

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to serialize.

  • options (Hash) (defaults to: {})

    The serialization options.

Options Hash (options):

  • :include (Symbol)

    What associations to include

  • :only (Symbol)

    Limit the fields to only these.

  • :except (Symbol)

    Dont include these fields.

[ GitHub ]

  
# File 'lib/mongoid/serializable.rb', line 131

def serialize_relations(attributes = {}, options = {})
  inclusions = options[: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(relation_options(inclusions, options, name))
    end
  end
end