Module: ResponseHandler
- Extended by:
- ActiveSupport::Concern
- Included in:
- Admin::Api::AdminBaseController, Api::BaseController
- Defined in:
- app/controllers/concerns/response_handler.rb
Instance Method Summary collapse
- #config_default_response_settings ⇒ Object
- #render_error(status, message, errors = nil, source: nil, meta: {}, admin_server_error: false) ⇒ Object
- #render_no_content ⇒ Object
- #render_success ⇒ Object
- #set_response_format ⇒ Object
Instance Method Details
#config_default_response_settings ⇒ Object
9 10 11 |
# File 'app/controllers/concerns/response_handler.rb', line 9 def config_default_response_settings set_response_format end |
#render_error(status, message, errors = nil, source: nil, meta: {}, admin_server_error: false) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/controllers/concerns/response_handler.rb', line 28 def render_error(status, , errors = nil, source: nil, meta: {}, admin_server_error: false) response = { 'status' => 'error', 'source' => source, 'errors' => {}, 'meta' => } if errors.is_a?(ActiveModel::Errors) errors.each do |error| attribute = error.attribute.to_s = error. response['errors'][attribute] ||= [] response['errors'][attribute] << end elsif errors.is_a?(Hash) response['errors'] = errors else response['errors'] = admin_server_error ? { 'root' => { 'serverError' => { message: } } } : { 'server' => [] } end render json: response, status: status end |
#render_no_content ⇒ Object
24 25 26 |
# File 'app/controllers/concerns/response_handler.rb', line 24 def render_no_content render json: {}, status: :no_content end |
#render_success ⇒ Object
20 21 22 |
# File 'app/controllers/concerns/response_handler.rb', line 20 def render_success render json: {}, status: :ok end |
#set_response_format ⇒ Object
13 14 15 16 17 18 |
# File 'app/controllers/concerns/response_handler.rb', line 13 def set_response_format if request.format.to_s != 'text/csv' request.format = :json self.content_type = 'application/json' end end |