-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbase_controller.rb
More file actions
47 lines (37 loc) · 1.15 KB
/
base_controller.rb
File metadata and controls
47 lines (37 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Api::V1::Shopkeeper::BaseController < ApplicationController
include DeviseTokenAuth::Concerns::SetUserByToken
include SetCurrentRequestDetails
include Pundit::Authorization
include CurrentShopkeeperHelper
include Pagy::Method
before_action :authenticate_shopkeeper!
after_action :verify_authorized
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
def pundit_user
current_accounts_shopkeeper
end
def policy_scope(scope)
super([:api, :shopkeeper, scope])
end
def authorize(record, query = nil)
super([:api, :shopkeeper, record], query)
end
private
def render_validation_error(record)
render json: {code: 422, error_message: record.errors.full_messages.to_sentence}, status: :unprocessable_entity
end
def render_error(code:, message:, status:)
render json: {code: code, error_message: message}, status: status
end
def user_not_authorized
render_error(code: 401, message: I18n.t("unauthorized"), status: :unauthorized)
end
def pagy_meta(pagy)
{
current_page: pagy.page,
total_pages: pagy.pages,
total_count: pagy.count,
limit: pagy.limit
}
end
end