Class: WithAccounts::ApplicationController

Inherits:
ActionController::API
  • Object
show all
Includes:
ErrorsHelpers
Defined in:
app/controllers/with_accounts/application_controller.rb

Direct Known Subclasses

MeaningfulController

Constant Summary collapse

@@log_errors =
ENV['LOG_ERRORS'] == 'true' ||
Rails.env.in?(%w[development test staging]) ||
false

Instance Method Summary collapse

Methods included from ErrorsHelpers

#error_key, #format_errors

Instance Method Details

#render_around_action_errorsObject



39
40
41
42
43
44
45
46
47
# File 'app/controllers/with_accounts/application_controller.rb', line 39

def render_around_action_errors
  yield
rescue StandardError => e
  message = 'filtered'
  if @@log_errors
    message = Rails.env.production? ? e.message : e.full_message
  end
  render_low_camel_cased({ errors: {internal_server_error: message} }, :internal_server_error)
end

#render_errors(errors_hash, status = :bad_request) ⇒ Object



13
14
15
16
17
18
19
20
# File 'app/controllers/with_accounts/application_controller.rb', line 13

def render_errors(errors_hash, status = :bad_request)
  if @@log_errors
    logger.warn(errors_hash)
    render_low_camel_cased({ errors: format_errors(errors_hash) }, status)
  else
    render body: '{}', status: :bad_request
  end
end

#render_low_camel_cased(hash_or_array_of_hashes, status) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/with_accounts/application_controller.rb', line 22

def render_low_camel_cased(hash_or_array_of_hashes, status)
  low_camelized = case hash_or_array_of_hashes
                  when Hash
                    hash_or_array_of_hashes.deep_transform_keys { _1.to_s.camelize(:lower) }
                  when Array
                    hash_or_array_of_hashes.map { |hash| hash.deep_transform_keys { _1.to_s.camelize(:lower) } }
                  else
                    raise StandardError
                  end
  st = status.is_a?(Integer) ? status : status.to_sym
  render json: low_camelized, status: st
end

#set_json_content_typeObject



35
36
37
# File 'app/controllers/with_accounts/application_controller.rb', line 35

def set_json_content_type
  response.headers['Content-Type'] = 'application/json; charset=utf-8'
end

#snake_case_paramsObject



49
50
51
# File 'app/controllers/with_accounts/application_controller.rb', line 49

def snake_case_params
  params.to_snake_case!
end