module JsonapiActions

Constants

VERSION

Public Instance Methods

create() click to toggle source
# File lib/jsonapi_actions.rb, line 44
def create
  @record = model.new
  @record.assign_attributes(record_params)
  @record.id = id_param if id_param
  authorize(@record)

  if @record.save
    @record.reload # ensure we have after commit stuff from things like carrierwave
    render json_response(@record, status: :created)
  else
    render unprocessable_entity(@record)
  end
end
destroy() click to toggle source
# File lib/jsonapi_actions.rb, line 67
def destroy
  if @record.destroy
    render json: {}, status: 204
  else
    render unprocessable_entity(@record)
  end
end
eager_load(records) click to toggle source
# File lib/jsonapi_actions.rb, line 214
def eager_load(records)
  records
end
filter(records) click to toggle source
# File lib/jsonapi_actions.rb, line 98
def filter(records)
  records
end
id_param() click to toggle source
# File lib/jsonapi_actions.rb, line 135
def id_param
  params.require(:data).permit(policy(@record).permitted_attributes)[:id]
end
include_param() click to toggle source
# File lib/jsonapi_actions.rb, line 143
def include_param
  if %w[* **].include? params[:include]
    inclusion_map
  else
    params[:include].to_s.split(',').reject(&:blank?).map(&:to_sym)
  end
end
inclusion_map() click to toggle source
# File lib/jsonapi_actions.rb, line 151
def inclusion_map
  InclusionMapper.new(serializer, include: params[:include]).map
end
index() click to toggle source
# File lib/jsonapi_actions.rb, line 28
def index
  authorize model, :index?
  @records = policy_scope(model.all)
  @records = parent_scope(@records)
  @records = filter(@records)
  @records = sort(@records)
  @records = paginate(@records)
  @records = eager_load(@records)

  render json_response(@records, meta: pagination_meta(@records).merge(metadata))
end
json_response(data, options = {}) click to toggle source
# File lib/jsonapi_actions.rb, line 176
def json_response(data, options = {})
  if defined?(JSONAPI::Serializer) || defined?(FastJsonapi)
    {
      json: serializer(data).new(data, options.deep_merge(
        include: include_param,
        meta: metadata,
        params: {
          current_user: try(:current_user)
        })
      )
    }

  elsif defined?(ActiveModel::Serializer)
    { json: data }.merge(meta: metadata, include: include_param).merge(options)

  else
    { json: { data: data }.merge(options) }
  end
end
metadata() click to toggle source
# File lib/jsonapi_actions.rb, line 77
def metadata
  {}
end
model() click to toggle source
# File lib/jsonapi_actions.rb, line 172
def model
  self.class.model || self.class.model_name.constantize
end
page() click to toggle source
# File lib/jsonapi_actions.rb, line 121
def page
  @page ||= begin
              page = {}
              page[:number] = (params.dig(:page, :number) || 1).to_i
              page[:size] = [[(params.dig(:page, :size) || 20).to_i, 1000].min, 1].max
              page
            end
end
paginate(records) click to toggle source
# File lib/jsonapi_actions.rb, line 117
def paginate(records)
  records.page(page[:number]).per(page[:size])
end
pagination_meta(collection) click to toggle source
# File lib/jsonapi_actions.rb, line 160
def pagination_meta(collection)
  return {} if collection.nil?

  {
    current_page: collection.current_page,
    next_page: collection.next_page,
    prev_page: collection.prev_page,
    total_pages: collection.total_pages,
    total_count: collection.total_count
  }
end
parent_scope(records) click to toggle source
# File lib/jsonapi_actions.rb, line 81
def parent_scope(records)
  return records if self.class.parent_associations.blank?

  self.class.parent_associations.each do |parent|
    next if params[parent[:param]].blank?

    records = records.joins(parent[:association]) unless parent[:association].blank?
    records = if parent[:table]
                records.where(parent[:table] => { parent[:attribute] => params[parent[:param]] })
              else
                records.where(parent[:attribute] => params[parent[:param]])
              end
  end

  records
end
record_params() click to toggle source
# File lib/jsonapi_actions.rb, line 139
def record_params
  params.require(:data).require(:attributes).permit(policy(@record).permitted_attributes)
end
serializer(data = nil) click to toggle source
# File lib/jsonapi_actions.rb, line 196
def serializer(data = nil)
  serializer_classes = [
    self.class.serializer,
    data.try(:serializer_class),
    "#{data.class.name}Serializer".safe_constantize,
    data.try(:first).try(:serializer_class),
    "#{data.try(:first).class.name}Serializer".safe_constantize
  ].compact

  if serializer_classes.any?
    serializer_classes.first
  else
    "#{model.name}Serializer".safe_constantize
  end
rescue NoMethodError, NameError
  raise 'Unknown serializer'
end
set_record() click to toggle source
# File lib/jsonapi_actions.rb, line 130
def set_record
  @record = model.find(params[:id])
  authorize(@record)
end
show() click to toggle source
# File lib/jsonapi_actions.rb, line 40
def show
  render json_response(@record)
end
sort(records) click to toggle source
# File lib/jsonapi_actions.rb, line 102
def sort(records)
  return records if params[:sort].blank?
  order = {}

  params[:sort].split(',').each do |sort|
    if sort[0] == '-'
      order[sort[1..-1]] = :desc
    else
      order[sort] = :asc
    end
  end

  return records.order(order)
end
unprocessable_entity(record) click to toggle source
# File lib/jsonapi_actions.rb, line 155
def unprocessable_entity(record)
  Rails.logger.debug(record.errors.messages)
  { json: record.errors.messages, status: :unprocessable_entity }
end
update() click to toggle source
# File lib/jsonapi_actions.rb, line 58
def update
  if @record.update(record_params)
    @record.reload # ensure we have after commit stuff from things like carrierwave
    render json_response(@record)
  else
    render unprocessable_entity(@record)
  end
end