Skip to content

Commit

Permalink
Copy ActionFilter to support Rails < 7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Oct 21, 2024
1 parent b998284 commit 10d11e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
27 changes: 27 additions & 0 deletions lib/inertia_rails/action_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true
#
# Based on AbstractController::Callbacks::ActionFilter
# https://github.com/rails/rails/blob/v7.2.0/actionpack/lib/abstract_controller/callbacks.rb#L39
module InertiaRails
class ActionFilter
def initialize(conditional_key, actions)
@conditional_key = conditional_key
@actions = Array(actions).map(&:to_s).to_set
end

def match?(controller)
missing_action = @actions.find { |action| !controller.available_action?(action) }
if missing_action
message = <<~MSG
The #{missing_action} action could not be found for the :inertia_share
callback on #{controller.class.name}, but it is listed in the controller's
#{@conditional_key.inspect} option.
MSG

raise ActionNotFound.new(message, controller, missing_action)
end

@actions.include?(controller.action_name)
end
end
end
5 changes: 3 additions & 2 deletions lib/inertia_rails/controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative "inertia_rails"
require_relative "helper"
require_relative "action_filter"

module InertiaRails
module Controller
Expand Down Expand Up @@ -94,7 +95,7 @@ def extract_inertia_share_options(props)

def extract_inertia_share_option(options, from, to)
if (from_value = options.delete(from))
filter = AbstractController::Callbacks::ActionFilter.new([:inertia_share], from, from_value)
filter = InertiaRails::ActionFilter.new(from, from_value)
options[to] = Array(options[to]).unshift(filter)
end
end
Expand All @@ -105,7 +106,7 @@ def filter_to_proc(filter)
-> { send(filter) }
when Proc
filter
when AbstractController::Callbacks::ActionFilter
when InertiaRails::ActionFilter
-> { filter.match?(self) }
else
raise ArgumentError, "You must pass a symbol or a proc as a filter."
Expand Down

0 comments on commit 10d11e8

Please sign in to comment.