-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
472 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
## [Unreleased] | ||
|
||
## [0.0.1] - 2023-01-03 | ||
## [0.0.1] - 2023-01-15 | ||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require "pundit/before" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,98 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
require "active_support/core_ext/class/attribute" | ||
require "active_support/callbacks" | ||
require "active_support/core_ext/array/wrap" | ||
require "active_support/core_ext/module/redefine_method" | ||
require "active_support/core_ext/object/blank" | ||
|
||
require_relative "before/version" | ||
|
||
module Pundit | ||
module Before | ||
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength | ||
# rubocop:disable Metrics/MethodLength | ||
def self.included(base) | ||
base.extend ClassMethods | ||
base.include ActiveSupport::Callbacks | ||
|
||
base.class_eval do | ||
class_attribute :_pundit_before, default: [] | ||
define_callbacks :_pundit_before, skip_after_callbacks_if_terminated: true | ||
|
||
def self.method_added(method_name) | ||
super | ||
|
||
return if @_pundit_before_running | ||
return unless method_name.to_s =~ /.*\?$/ && public_method_defined?(method_name) | ||
return if @_pundit_before_running | ||
|
||
@_pundit_before_running = true | ||
|
||
old_method = instance_method(method_name) | ||
|
||
redefine_method(method_name) do | ||
result = catch :halt do | ||
(_pundit_before.presence || []).each do |name, block| | ||
name.present? ? send(name) : instance_eval(&block) | ||
end | ||
nil | ||
end | ||
|
||
result.nil? ? old_method.bind(self).call : result | ||
@_pundit_before_result = nil | ||
@_pundit_before_method = method_name | ||
|
||
run_callbacks :_pundit_before | ||
|
||
@_pundit_before_result.nil? ? old_method.bind(self).call : @_pundit_before_result | ||
end | ||
|
||
@_pundit_before_running = false | ||
end | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength | ||
# rubocop:enable Metrics/MethodLength | ||
|
||
class CallbackFilter | ||
def initialize(methods) | ||
@methods = Array(methods).map(&:to_sym) | ||
end | ||
|
||
def match?(object) | ||
@methods.include?(object.instance_variable_get(:@_pundit_before_method).to_sym) | ||
end | ||
|
||
alias after match? | ||
alias before match? | ||
alias around match? | ||
end | ||
|
||
module ClassMethods | ||
def before(method_name=nil, &block) | ||
self._pundit_before = _pundit_before.dup.push([method_name, block]) | ||
def before(*method_names, **options, &block) | ||
_normalize_callback_options(options) | ||
|
||
if block_given? | ||
set_callback :_pundit_before, :before, **options, &block | ||
else | ||
set_callback :_pundit_before, :before, *method_names, **options | ||
end | ||
end | ||
|
||
def skip_before(*method_names, **options) | ||
_normalize_callback_options(options) | ||
skip_callback :_pundit_before, :before, *method_names, **options | ||
end | ||
|
||
def _normalize_callback_options(options) | ||
_normalize_callback_option(options, :only, :if) | ||
_normalize_callback_option(options, :except, :unless) | ||
end | ||
|
||
def _normalize_callback_option(options, from, to) | ||
return unless (from = options.delete(from)) | ||
|
||
from = CallbackFilter.new(from) | ||
options[to] = Array(options[to]).unshift(from) | ||
end | ||
end | ||
|
||
def allow! | ||
throw :halt, true | ||
@_pundit_before_result = true | ||
throw :abort | ||
end | ||
|
||
def deny! | ||
throw :halt, false | ||
@_pundit_before_result = false | ||
throw :abort | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.