Class: Api::Dashboard::BaseController

Inherits:
ActionController::API
  • Object
show all
Includes:
ControllerHelpers, ElasticApmContext, Knock::Authenticable, LogrageCustomLogger, Pagy::Backend, Pundit::Authorization, ResponseCacheConcern
Defined in:
app/controllers/api/dashboard/base_controller.rb

Defined Under Namespace

Classes: NotAuthorized

Instance Method Summary collapse

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#current_userObject



76
77
78
# File 'app/controllers/api/dashboard/base_controller.rb', line 76

def current_user
  @current_user ||= current_staff
end

#identity_cache_memoization(&block) ⇒ Object



30
31
32
# File 'app/controllers/api/dashboard/base_controller.rb', line 30

def identity_cache_memoization(&block)
  IdentityCache.cache.with_memoization(&block)
end

#restaurantsObject

def authenticate_roles

current_user.roles.pluck(:restaurant_id).include?()

end



41
42
43
44
45
46
47
# File 'app/controllers/api/dashboard/base_controller.rb', line 41

def restaurants
  @restaurants ||= if params[:restaurant_id].present?
                     current_staff.restaurants.where(id: params[:restaurant_id])
                   else
                     current_staff.restaurants
                   end
end

#set_options(pagy = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/api/dashboard/base_controller.rb', line 49

def set_options(pagy = {})
  Pagy::VARS[:items] = params.fetch('per_page', 15)
  options = {}
  if pagy.present?
    options[:meta] = {
      total: (pagy).fetch(:count),
      page: (pagy).fetch(:page),
      total_page: (pagy).fetch(:pages),
      default_page: 1,
    }

    options[:links] = {
      self: (pagy).fetch(:page_url),
      first: (pagy).fetch(:first_url),
      next: (pagy).fetch(:next_url),
      prev: (pagy).fetch(:prev_url),
      last: (pagy).fetch(:last_url),
    }
  end
  options[:include] = params.fetch(:include, '').split(',') || []
  options[:fields] = params.fetch(:fields, {}).to_unsafe_hash.map do |rel, fields|
    { rel => fields.split(',').map(&:to_sym) }
  end.reduce({}, :merge) || {}
  options[:params] = { current_staff: current_staff }
  fix_include_options(options)
end