Sha256: 1bda01d8caaca4570f3dc03f2c20ef8ef5f7c8c212150848f07553ace2318ba2

Contents?: true

Size: 1.52 KB

Versions: 47

Compression:

Stored size: 1.52 KB

Contents

module Comee
  module Core
    class ApplicationController < ActionController::API
      include Pagination

      before_action :authenticate

      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] = options.key?(:fields) ? serialize(data, include: options[:fields]) : serialize(data)

        render json: result
      end

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

      def current_user
        return if token.nil?

        user = User.find(auth["id"])
        @current_user ||= user
      end

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

      # In case we want to disable bullet for specific controller actions
      def skip_bullet
        previous_value = Bullet.enable?
        Bullet.enable = false
        yield
      ensure
        Bullet.enable = previous_value
      end

      private

      def serialize(data, options = {})
        ActiveModelSerializers::SerializableResource.new(data, options)
      end

      def token
        return nil if request.env["HTTP_AUTHORIZATION"].nil?

        request.env["HTTP_AUTHORIZATION"].scan(/Bearer (.*)$/).flatten.last
      end

      def auth
        TokenService.decode(token)
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
comee_core-0.2.52 app/controllers/comee/core/application_controller.rb
comee_core-0.2.51 app/controllers/comee/core/application_controller.rb
comee_core-0.2.50 app/controllers/comee/core/application_controller.rb
comee_core-0.2.49 app/controllers/comee/core/application_controller.rb
comee_core-0.2.48 app/controllers/comee/core/application_controller.rb
comee_core-0.2.47 app/controllers/comee/core/application_controller.rb
comee_core-0.2.46 app/controllers/comee/core/application_controller.rb
comee_core-0.2.45 app/controllers/comee/core/application_controller.rb
comee_core-0.2.44 app/controllers/comee/core/application_controller.rb
comee_core-0.2.43 app/controllers/comee/core/application_controller.rb
comee_core-0.2.42 app/controllers/comee/core/application_controller.rb
comee_core-0.2.41 app/controllers/comee/core/application_controller.rb
comee_core-0.2.40 app/controllers/comee/core/application_controller.rb
comee_core-0.2.39 app/controllers/comee/core/application_controller.rb
comee_core-0.2.38 app/controllers/comee/core/application_controller.rb
comee_core-0.2.37 app/controllers/comee/core/application_controller.rb
comee_core-0.2.36 app/controllers/comee/core/application_controller.rb
comee_core-0.2.35 app/controllers/comee/core/application_controller.rb
comee_core-0.2.34 app/controllers/comee/core/application_controller.rb
comee_core-0.2.33 app/controllers/comee/core/application_controller.rb