This commit is contained in:
Mohammad Kanaan 2026-06-10 19:16:28 +03:00
parent 9058d1ae2e
commit ed626b5066
2 changed files with 12 additions and 7 deletions

View file

@ -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!

View file

@ -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