module ApiBlocks::Controller

Public Instance Methods

authorize(record, query = nil, policy_class: nil) click to toggle source

Override authorize to lookup pundit policies under the `scope` namespace

Calls superclass method
# File lib/api_blocks/controller.rb, line 49
def authorize(record, query = nil, policy_class: nil)
  api_scope = self.class.inherited_pundit_api_scope || []

  super(api_scope + [record], query, policy_class: policy_class)
end
handle_api_error(error_class) { |ex| ... } click to toggle source

Defines a error handler that returns

# File lib/api_blocks/controller.rb, line 80
def handle_api_error(error_class)
  rescue_from error_class do |ex|
    problem, status =
      if block_given?
        yield ex
      else
        [{ detail: ex.message }, :ok]
      end

    render problem: problem, status: status
  end
end
inherited_pundit_api_scope() click to toggle source

Returns the `pundit_api_scope` value that was defined last looking up into the inheritance chain of the current class.

# File lib/api_blocks/controller.rb, line 63
def inherited_pundit_api_scope
  ancestors
    .select { |a| a.respond_to?(:pundit_api_scope) }
    .find(&:pundit_api_scope)
    .pundit_api_scope
end
policy_scope(scope, policy_scope_class: nil) click to toggle source

Override policy_scope to lookup pundit policies under the `scope` namespace

Calls superclass method
# File lib/api_blocks/controller.rb, line 41
def policy_scope(scope, policy_scope_class: nil)
  api_scope = self.class.inherited_pundit_api_scope || []

  super(api_scope + [scope], policy_scope_class: policy_scope_class)
end
pundit_api_scope() click to toggle source
# File lib/api_blocks/controller.rb, line 75
def pundit_api_scope
  @pundit_api_scope
end
pundit_scope(*scope) click to toggle source

Provide a default scope to pundit's `PolicyFinder`.

# File lib/api_blocks/controller.rb, line 71
def pundit_scope(*scope)
  @pundit_api_scope ||= scope
end