module ResourceAllowHeader

Constants

HEADER_ALLOW
HTTP_ABILITY_METHOD_MAP

noinspection RubyStringKeysInHashInspection

VERSION

Protected Class Methods

configure(&block) click to toggle source
# File lib/resource_allow_header.rb, line 16
def self.configure(&block)
  block_given? ? instance_exec(self, &block) : self
end

Protected Instance Methods

allow(http_method, ability_action = map_http_method_to_ability_action(http_method), **options, &block) click to toggle source
# File lib/resource_allow_header.rb, line 48
def allow(http_method, ability_action = map_http_method_to_ability_action(http_method), **options, &block)
  before_action(**options) do
    allow_resource = block_given? && proc { instance_exec(&block) } || nil

    self.current_action_lazy_allows = Hash(current_action_lazy_allows).merge(
      http_method => { resource: allow_resource, action: ability_action }
    )
  end
end
allow?(action, resource) click to toggle source
# File lib/resource_allow_header.rb, line 65
def allow?(action, resource)
  if can_proc.respond_to?(:call)
    return instance_exec(action, resource, self, &can_proc)
  end

  can?(action, resource)
end
compute_allow_header(resource: implicit_resource) click to toggle source
# File lib/resource_allow_header.rb, line 28
def compute_allow_header(resource: implicit_resource)
  Hash(current_action_lazy_allows).each_with_object([]) do |(method, allow), result|
    allowable_resource = allow[:resource]&.call || resource
    next unless allow?(allow[:action], allowable_resource)
    result << method
  end
end
map_http_method_to_ability_action(http_method) click to toggle source
# File lib/resource_allow_header.rb, line 58
def map_http_method_to_ability_action(http_method)
  HTTP_ABILITY_METHOD_MAP[http_method]
end
set_allow_header() click to toggle source
# File lib/resource_allow_header.rb, line 24
def set_allow_header
  response.header[HEADER_ALLOW] = compute_allow_header.join(', ')
end

Private Instance Methods

implicit_resource() click to toggle source
# File lib/resource_allow_header.rb, line 75
def implicit_resource
  if implicit_resource_proc.respond_to?(:call)
    return instance_exec(self, &implicit_resource_proc)
  end

  @allow_resource || @resource
end