Class: Comee::Core::ApplicationController

Inherits:
ActionController::API
  • Object
show all
Includes:
Pagination
Defined in:
app/controllers/comee/core/application_controller.rb

Instance Method Summary collapse

Methods included from Pagination

#default_per_page, #order_by, #order_direction, #page_no, #paginate, #paginate_offset, #per_page

Instance Method Details

#application_codeObject

Raises:

  • (StandardError)


42
43
44
45
46
47
# File 'app/controllers/comee/core/application_controller.rb', line 42

def application_code
  code = Rails.application.config.application_code
  raise(StandardError, "Code is not set for the current application.") unless code

  code
end

#authenticateObject



56
57
58
# File 'app/controllers/comee/core/application_controller.rb', line 56

def authenticate
  render json: {error: "Unauthorized"}, status: 401 if current_user.nil?
end

#current_applicationObject



49
50
51
52
53
54
# File 'app/controllers/comee/core/application_controller.rb', line 49

def current_application
  app_code = application_code
  return ApplicationModule.find_by(code: app_code) if app_code

  nil
end

#current_userObject



33
34
35
36
37
38
39
40
# File 'app/controllers/comee/core/application_controller.rb', line 33

def current_user
  return if token.nil?

  decoded = auth
  return if decoded.nil?

  @current_user ||= User.find_by(id: decoded["id"])
end

#render_content(data, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/comee/core/application_controller.rb', line 8

def render_content(data, options = {})
  result = {success: true}
  if params[:page]
    total = data.count
    data = data.then(&paginate)
    result[:page] = params[:page].to_i
    result[:total] = total
  end
  # result[:data] = if options.key?(:fields) && options.key?(:serializer)
  #                   serialize(data, {include: options[:fields], serializer: options[:serializer]})
  #                 elsif options.key?(:fields) && options.key?(:each_serializer)
  #                   serialize(data, {include: options[:fields], each_serializer: options[:each_serializer]})
  #                 elsif options.key?(:fields)
  #                   serialize(data, {include: options[:fields]})
  #                 else
  #                   serialize(data)
  #                 end
  result[:data] = serialize(data, options)
  render json: result
end

#render_error(error) ⇒ Object



29
30
31
# File 'app/controllers/comee/core/application_controller.rb', line 29

def render_error(error)
  render json: {success: false, error: error.message}, status: 422
end

#skip_bulletObject

In case we want to disable bullet for specific controller actions



61
62
63
64
65
66
67
# File 'app/controllers/comee/core/application_controller.rb', line 61

def skip_bullet
  previous_value = Bullet.enable?
  Bullet.enable = false
  yield
ensure
  Bullet.enable = previous_value
end