Module: Mongoid::Serialization
- Extended by:
- ActiveSupport::Concern
- Included in:
- Components
- Defined in:
- lib/mongoid/serialization.rb
Overview
This module provides the extra behaviour for including relations in JSON and XML serialization.
Instance Method Summary (collapse)
-
- (Hash) serializable_hash(options = nil)
Gets the document as a serializable hash, used by ActiveModel's JSON serializer.
Instance Method Details
- (Hash) serializable_hash(options = nil)
Gets the document as a serializable hash, used by ActiveModel's JSON serializer.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mongoid/serialization.rb', line 28 def serializable_hash( = nil) ||= {} only = Array.wrap([:only]).map(&:to_s) except = Array.wrap([:except]).map(&:to_s) except |= ['_type'] unless Mongoid.include_type_for_serialization field_names = self.class.attribute_names attribute_names = (as_document.keys + field_names).sort if only.any? attribute_names &= only elsif except.any? attribute_names -= except end method_names = Array.wrap([:methods]).map do |name| name.to_s if respond_to?(name) end.compact attrs = {} (attribute_names + method_names).each do |name| without_autobuild do value = send(name) if relations.has_key?(name) attrs[name] = value ? value.serializable_hash() : nil else attrs[name] = value end end end serialize_relations(attrs, ) if [:include] attrs end |