Sha256: f418c2458f7f59c8d762688519749b4004945e4ac5854a7d689b18f4a779dc0c

Contents?: true

Size: 1.77 KB

Versions: 7

Compression:

Stored size: 1.77 KB

Contents

require "pagy_cursor/pagy/extras/cursor"
require "pagy_cursor/pagy/extras/uuid_cursor"

module Api::Controllers::Base
  extend ActiveSupport::Concern

  # TODO Why doesn't `before_action :doorkeeper_authorize!` throw an exception?
  class NotAuthenticatedError < StandardError
  end

  included do
    include ActionController::Helpers
    helper ApplicationHelper

    include LoadsAndAuthorizesResource
    include Pagy::Backend

    before_action :set_default_response_format
    before_action :doorkeeper_authorize!

    rescue_from CanCan::AccessDenied, ActiveRecord::RecordNotFound do |exception|
      render json: {error: "Not found"}, status: :not_found
    end

    rescue_from NotAuthenticatedError do |exception|
      render json: {error: "Invalid token"}, status: :unauthorized
    end

    before_action :apply_pagination, only: [:index]
  end

  def permitted_fields
    []
  end

  def permitted_arrays
    {}
  end

  def process_params(strong_params)
  end

  def current_user
    raise NotAuthenticatedError unless doorkeeper_token.present?
    doorkeeper_token.update(last_used_at: Time.zone.now)
    @current_user ||= User.find_by(id: doorkeeper_token[:resource_owner_id])
  end

  def current_team
    # Application agents are users but only have one team.
    current_user&.teams&.first
  end

  def apply_pagination
    collection_variable = "@#{self.class.name.split("::").last.gsub("Controller", "").underscore}"
    collection = instance_variable_get collection_variable
    @pagy, collection = pagy_cursor collection
    instance_variable_set collection_variable, collection
  end

  def set_default_response_format
    request.format = :json
  end

  class_methods do
    def regex_to_remove_controller_namespace
      /^#{name.split("::").first(2).join("::") + "::"}/
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bullet_train-api-1.1.10 app/controllers/concerns/api/controllers/base.rb
bullet_train-api-1.1.9 app/controllers/concerns/api/controllers/base.rb
bullet_train-api-1.1.8 app/controllers/concerns/api/controllers/base.rb
bullet_train-api-1.1.6 app/controllers/concerns/api/controllers/base.rb
bullet_train-api-1.1.5 app/controllers/concerns/api/controllers/base.rb
bullet_train-api-1.1.4 app/controllers/concerns/api/controllers/base.rb
bullet_train-api-1.1.3 app/controllers/concerns/api/controllers/base.rb