Sha256: 15e265f98b0675dead39d2ecdbb9018e1daa7df3bdaa3cbe7823c53fd058332a
Contents?: true
Size: 1.03 KB
Versions: 3
Compression:
Stored size: 1.03 KB
Contents
require 'action_controller' module Fields module Serializer module ActionController extend ActiveSupport::Concern # Render the result of an ActiveRecord query including only the fields specified if any # or the whole serialized objects. # # @param [ActiveRecord_Relation] query - The query to render in json # @option options [Array] :fields - The list of fields to return in json api syntax # @option options [Class] :model_class - The model class of the objects to be queried to optimize db hits. # @option options [Hash] :options - Any other valid option to render method. def render_json_fields(query, **options) fields = options.delete(:fields) model_class = options.delete(:model_class) if fields query = query.includes(*model_class.fields_to_includes(fields)) options.merge!(each_serializer: FieldSerializer, fields: Array(fields)) end render options.merge!(json: query.to_a) end end end end
Version data entries
3 entries across 3 versions & 1 rubygems