File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 1
1
require_relative "inertia_rails"
2
2
require_relative "helper"
3
+ require_relative "action_filter"
3
4
4
5
module InertiaRails
5
6
module Controller
@@ -94,7 +95,7 @@ def extract_inertia_share_options(props)
94
95
95
96
def extract_inertia_share_option ( options , from , to )
96
97
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 )
98
99
options [ to ] = Array ( options [ to ] ) . unshift ( filter )
99
100
end
100
101
end
@@ -105,7 +106,7 @@ def filter_to_proc(filter)
105
106
-> { send ( filter ) }
106
107
when Proc
107
108
filter
108
- when AbstractController :: Callbacks ::ActionFilter
109
+ when InertiaRails ::ActionFilter
109
110
-> { filter . match? ( self ) }
110
111
else
111
112
raise ArgumentError , "You must pass a symbol or a proc as a filter."
You can’t perform that action at this time.
0 commit comments