Class: Kadmin::ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Includes:
Concerns::AuthorizedUser
Defined in:
app/controllers/kadmin/application_controller.rb

Direct Known Subclasses

AuthController, DashController

Instance Method Summary collapse

Methods included from Concerns::AuthorizedUser

#authorize, #authorized?, #authorized_user, #current_user, #logged_in?

Instance Method Details

#handle_error(error, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'app/controllers/kadmin/application_controller.rb', line 43

def handle_error(error, options = {})
  options = {
    title: error.try(:title) || error.class.name,
    message: error.message,
    status: :internal_server_error,
    error: error
  }.merge(options)
  render 'kadmin/error', status: options[:status], locals: options
end

#handle_unexpected_error(error) ⇒ Object



38
39
40
41
# File 'app/controllers/kadmin/application_controller.rb', line 38

def handle_unexpected_error(error)
  Rails.logger.error(error)
  handle_error(error, title: I18n.t('kadmin.errors.unexpected'), message: I18n.t('kadmin.errors.unexpected_message'))
end

#not_found(error) ⇒ Object



34
35
36
# File 'app/controllers/kadmin/application_controller.rb', line 34

def not_found(error)
  handle_error(error, title: I18n.t('kadmin.errors.not_found'), status: :not_found)
end

#organizationObject



86
87
88
89
90
91
92
# File 'app/controllers/kadmin/application_controller.rb', line 86

def organization
  if authorized_user.present?
    @organization ||= Kadmin::Organization.find_by!(name: authorized_user.organization)
  end
rescue ActiveRecord::RecordNotFound
  render plain: "Forbidden - organization #{authorized_user.organization} not found in DB", status: :forbidden
end

#params_missing(error) ⇒ Object



30
31
32
# File 'app/controllers/kadmin/application_controller.rb', line 30

def params_missing(error)
  handle_error(error, title: I18n.t('kadmin.errors.params_missing'), status: :bad_request)
end

#scoped_all(organization_scoped_ar) ⇒ Object

returns all organization_scoped_ar object(s) that are of the user’s organization. admin user gets all. you can chain scopes, e.g. scoped_all(Segments.my_scope) is valid organization_scoped_ar is an ActiveRecord that has organization_scope(Organization) scope defined



78
79
80
81
82
83
84
# File 'app/controllers/kadmin/application_controller.rb', line 78

def scoped_all(organization_scoped_ar)
  if authorized_user.admin?
    organization_scoped_ar.all
  else
    organization_scoped_ar.organization_scope(organization).all
  end
end

#scoped_find_by!(organization_scoped_ar, id) ⇒ Object

returns organization_scoped_ar object(s) by id (or array of ids) or throw RecordNotFound in case id(s) does not exist or is not visible in scope

organization_scoped_ar is an ActiveRecord that has organization_scope(Organization) scope defined



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/kadmin/application_controller.rb', line 59

def scoped_find_by!(organization_scoped_ar, id)
  if authorized_user.admin?
    if id.is_a?(Array)
      return organization_scoped_ar.find(id)
    else
      return organization_scoped_ar.find_by!(id: id)
    end
  else
    if id.is_a?(Array)
      return organization_scoped_ar.organization_scope(@organization).find(id)
    else
      return organization_scoped_ar.organization_scope(@organization).find_by!(id: id)
    end
  end
end