Sha256: 721f235c1822f6f6758aa058edc7fcf944029c74c8b42fa7d2baf3ce682c26fb

Contents?: true

Size: 1.91 KB

Versions: 16

Compression:

Stored size: 1.91 KB

Contents

module Workarea
  module Api
    module Admin
      class ApplicationController < Workarea::ApplicationController
        include CurrentRelease
        include ::Swagger::Blocks

        respond_to :json

        before_action :current_user # to force HTTP basic auth
        before_action :set_json_format
        before_action { params.permit! }
        around_action :audit_log

        rescue_from Mongoid::Errors::DocumentNotFound, with: :handle_not_found
        rescue_from Mongoid::Errors::Validations, with: :handle_invalid
        rescue_from Mongoid::Errors::UnknownAttribute, with: :handle_unknown_attribute

        def current_user
          return @current_user if defined?(@current_user)

          if user = authenticate_with_http_basic { |u, p| Api::Admin.authenticate(u, p) }
            @current_user = user
          else
            request_http_basic_authentication
          end
        end

        def sort_field
          params[:sort_by].presence || :created_at
        end

        def sort_direction
          if params[:sort_direction].in?(%w(asc desc))
            params[:sort_direction]
          else
            :desc
          end
        end

        private

        def current_release_id
          params[:release_id]
        end

        def set_json_format
          request.format = :json
        end

        def handle_not_found(e)
          payload = { params: e.params, problem: e.problem }
          render json: payload, status: :not_found
        end

        def handle_invalid(e)
          payload = { problem: e.summary, document: e.document }
          render json: payload, status: :unprocessable_entity
        end

        def handle_unknown_attribute(e)
          render json: e.as_json.slice('problem'), status: :unprocessable_entity
        end

        def audit_log
          Mongoid::AuditLog.record(current_user) do
            yield
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
workarea-api-4.5.6 admin/app/controllers/workarea/api/admin/application_controller.rb
workarea-api-admin-4.5.6 app/controllers/workarea/api/admin/application_controller.rb
workarea-api-4.5.5 admin/app/controllers/workarea/api/admin/application_controller.rb
workarea-api-admin-4.5.5 app/controllers/workarea/api/admin/application_controller.rb
workarea-api-4.5.4 admin/app/controllers/workarea/api/admin/application_controller.rb
workarea-api-admin-4.5.4 app/controllers/workarea/api/admin/application_controller.rb
workarea-api-4.5.3 admin/app/controllers/workarea/api/admin/application_controller.rb
workarea-api-admin-4.5.3 app/controllers/workarea/api/admin/application_controller.rb
workarea-api-4.5.2 admin/app/controllers/workarea/api/admin/application_controller.rb
workarea-api-admin-4.5.2 app/controllers/workarea/api/admin/application_controller.rb
workarea-api-4.5.0 admin/app/controllers/workarea/api/admin/application_controller.rb
workarea-api-admin-4.5.0 app/controllers/workarea/api/admin/application_controller.rb
workarea-api-4.4.7 admin/app/controllers/workarea/api/admin/application_controller.rb
workarea-api-admin-4.4.7 app/controllers/workarea/api/admin/application_controller.rb
workarea-api-4.4.6 admin/app/controllers/workarea/api/admin/application_controller.rb
workarea-api-admin-4.4.6 app/controllers/workarea/api/admin/application_controller.rb