Class: ActiveModel::Serializer::Adapter::JsonApi
- Inherits:
-
ActiveModel::Serializer::Adapter
- Object
- ActiveModel::Serializer::Adapter
- ActiveModel::Serializer::Adapter::JsonApi
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/active_model/serializer/adapter/json_api.rb
Defined Under Namespace
Classes: FragmentCache, PaginationLinks
Constant Summary
Constants inherited from ActiveModel::Serializer::Adapter
Instance Attribute Summary
Attributes inherited from ActiveModel::Serializer::Adapter
Instance Method Summary collapse
- #fragment_cache(cached_hash, non_cached_hash) ⇒ Object
-
#initialize(serializer, options = {}) ⇒ JsonApi
constructor
A new instance of JsonApi.
- #serializable_hash(options = nil) ⇒ Object
Methods inherited from ActiveModel::Serializer::Adapter
adapter_class, adapter_map, adapters, #as_json, create, find_by_name, inherited, lookup, register
Constructor Details
permalink #initialize(serializer, options = {}) ⇒ JsonApi
Returns a new instance of JsonApi.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/active_model/serializer/adapter/json_api.rb', line 6 def initialize(serializer, = {}) super @hash = { data: [] } @included = ActiveModel::Serializer::Utils.include_args_to_hash(@options[:include]) fields = .delete(:fields) if fields @fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key) else @fieldset = [:fieldset] end end |
Instance Method Details
permalink #fragment_cache(cached_hash, non_cached_hash) ⇒ Object
[View source]
44 45 46 47 |
# File 'lib/active_model/serializer/adapter/json_api.rb', line 44 def fragment_cache(cached_hash, non_cached_hash) root = false if @options.include?(:include) ActiveModel::Serializer::Adapter::JsonApi::FragmentCache.new().fragment_cache(root, cached_hash, non_cached_hash) end |
permalink #serializable_hash(options = nil) ⇒ Object
[View source]
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_model/serializer/adapter/json_api.rb', line 19 def serializable_hash( = nil) ||= {} if serializer.respond_to?(:each) serializer.each do |s| result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash() @hash[:data] << result[:data] if result[:included] @hash[:included] ||= [] @hash[:included] |= result[:included] end end add_links() else primary_data = primary_data_for(serializer, ) relationships = relationships_for(serializer) included = included_for(serializer) @hash[:data] = primary_data @hash[:data][:relationships] = relationships if relationships.any? @hash[:included] = included if included.any? end @hash end |