Sha256: b748343bae2a2903fe0e34052a68cf26acea9020bbf4fb9bebf1b80b3335f453

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 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 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

9 entries across 9 versions & 1 rubygems

Version Path
comee_core-0.2.5 app/controllers/comee/core/application_controller.rb
comee_core-0.2.4 app/controllers/comee/core/application_controller.rb
comee_core-0.2.3 app/controllers/comee/core/application_controller.rb
comee_core-0.2.2 app/controllers/comee/core/application_controller.rb
comee_core-0.2.1 app/controllers/comee/core/application_controller.rb
comee_core-0.2.0 app/controllers/comee/core/application_controller.rb
comee_core-0.1.99 app/controllers/comee/core/application_controller.rb
comee_core-0.1.98 app/controllers/comee/core/application_controller.rb
comee_core-0.1.97 app/controllers/comee/core/application_controller.rb