class SnFoil::Controller::API

Attributes

i_deserializer[R]
i_serializer[R]

Public Class Methods

deserializer(klass = nil) click to toggle source
# File lib/sn_foil/controller/api.rb, line 16
def deserializer(klass = nil)
  @i_deserializer = klass
end
serializer(klass = nil) click to toggle source
# File lib/sn_foil/controller/api.rb, line 12
def serializer(klass = nil)
  @i_serializer = klass
end

Public Instance Methods

deserializer(**options) click to toggle source
# File lib/sn_foil/controller/api.rb, line 25
def deserializer(**options)
  options[:deserializer] || self.class.i_deserializer
end
render_change(model, **options) click to toggle source
# File lib/sn_foil/controller/api.rb, line 43
def render_change(model, **options)
  if model.errors.empty?
    params
    render json: serializer(**options).new(model,
                                           **options,
                                           params: (options[:controller_params] || options[:params] || {})
                                                     .merge(current_entity: context_entity)).serializable_hash
  else
    render json: model.errors, status: :unprocessable_entity
  end
end
render_destroy(model, **_options) click to toggle source
# File lib/sn_foil/controller/api.rb, line 55
def render_destroy(model, **_options)
  if model.errors.empty?
    render json: {}, status: :no_content
  else
    render json: model.errors, status: :unprocessable_entity
  end
end
render_index(results, **options) click to toggle source
# File lib/sn_foil/controller/api.rb, line 63
def render_index(results, **options)
  render json: serializer(**options).new(paginate(results, **options),
                                         **options,
                                         params: (options[:controller_params] || options[:params] || {})
                                                   .merge(current_entity: context_entity),
                                         meta: meta(results, **options))
                                    .serializable_hash
end
render_show(model, **options) click to toggle source
# File lib/sn_foil/controller/api.rb, line 72
def render_show(model, **options)
  render json: serializer(**options).new(model,
                                         **options,
                                         params: (options[:controller_params] || options[:params] || {})
                                                   .merge(current_entity: context_entity)).serializable_hash
end
serializer(**options) click to toggle source
# File lib/sn_foil/controller/api.rb, line 21
def serializer(**options)
  options[:serializer] || self.class.i_serializer
end
setup_create(**options) click to toggle source
# File lib/sn_foil/controller/api.rb, line 33
def setup_create(**options)
  options[:deserialize] = options[:deserialize].nil? ? true : options[:deserialize]
  super(**options)
end
setup_options(**options) click to toggle source
Calls superclass method
# File lib/sn_foil/controller/api.rb, line 29
def setup_options(**options)
  inject_deserialized_params(super)
end
setup_update(**options) click to toggle source
# File lib/sn_foil/controller/api.rb, line 38
def setup_update(**options)
  options[:deserialize] = options[:deserialize].nil? ? true : options[:deserialize]
  super(**options)
end

Private Instance Methods

inject_deserialized_params(**options) click to toggle source
# File lib/sn_foil/controller/api.rb, line 81
def inject_deserialized_params(**options)
  return options unless options[:params].present? && options[:deserialize] == true
  return options unless deserializer(**options)

  options[:params] = deserializer(**options).new(options[:params], **options).to_h
  options
end