diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a44778f..85005bc 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,8 +1,11 @@ -module ApplicationController - extend ActionController::API +class ApplicationController < ActionController::API + include Renderable + before_action :authenticate! - rescue_from Unauthorized, with: -> { render json: { error: "Unauthorized" }, status: :unauthorized } + rescue_from Unauthorized do + render_error("Unauthorized", :unauthorized) + end private def authenticate! diff --git a/app/controllers/concerns/renderable.rb b/app/controllers/concerns/renderable.rb index 86e8f4e..2e984c4 100644 --- a/app/controllers/concerns/renderable.rb +++ b/app/controllers/concerns/renderable.rb @@ -1,17 +1,19 @@ # frozen_string_literal: true -class Renderable +module Renderable extend ActiveSupport::Concern def render_success(data = nil, status = :ok, meta = {}) body = { success: true } body[:data] = data if data body[:meta] = meta if meta.present? - render json: body + render json: body, status: end - def render_error - + def render_error(message, status = :unprocessable_entity, details: nil) + body = { success: false, error: { message: } } + body[:error][:details] = details if details.present? + render json: body, status: end end