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)


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

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

  code
end

#authenticateObject



54
55
56
# File 'app/controllers/comee/core/application_controller.rb', line 54

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

#current_applicationObject



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

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
# File 'app/controllers/comee/core/application_controller.rb', line 33

def current_user
  return if token.nil?

  user = User.find(auth["id"])
  @current_user ||= user
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



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

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