Skip to content

Commit 2309b58

Browse files
committed
Copy ActionFilter to support Rails < 7.1
1 parent 0875d68 commit 2309b58

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/inertia_rails/action_filter.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
#
3+
# Based on AbstractController::Callbacks::ActionFilter
4+
# https://github.com/rails/rails/blob/v7.2.0/actionpack/lib/abstract_controller/callbacks.rb#L39
5+
module InertiaRails
6+
class ActionFilter
7+
def initialize(conditional_key, actions)
8+
@conditional_key = conditional_key
9+
@actions = Array(actions).map(&:to_s).to_set
10+
end
11+
12+
def match?(controller)
13+
missing_action = @actions.find { |action| !controller.available_action?(action) }
14+
if missing_action
15+
message = <<~MSG
16+
The #{missing_action} action could not be found for the :inertia_share
17+
callback on #{controller.class.name}, but it is listed in the controller's
18+
#{@conditional_key.inspect} option.
19+
MSG
20+
21+
raise ActionNotFound.new(message, controller, missing_action)
22+
end
23+
24+
@actions.include?(controller.action_name)
25+
end
26+
end
27+
end

lib/inertia_rails/controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require_relative "inertia_rails"
22
require_relative "helper"
3+
require_relative "action_filter"
34

45
module InertiaRails
56
module Controller
@@ -94,7 +95,7 @@ def extract_inertia_share_options(props)
9495

9596
def extract_inertia_share_option(options, from, to)
9697
if (from_value = options.delete(from))
97-
filter = AbstractController::Callbacks::ActionFilter.new([:inertia_share], from, from_value)
98+
filter = InertiaRails::ActionFilter.new(from, from_value)
9899
options[to] = Array(options[to]).unshift(filter)
99100
end
100101
end
@@ -105,7 +106,7 @@ def filter_to_proc(filter)
105106
-> { send(filter) }
106107
when Proc
107108
filter
108-
when AbstractController::Callbacks::ActionFilter
109+
when InertiaRails::ActionFilter
109110
-> { filter.match?(self) }
110111
else
111112
raise ArgumentError, "You must pass a symbol or a proc as a filter."

0 commit comments

Comments
 (0)