@@ -5,23 +5,22 @@ module Controller
5
5
extend ActiveSupport ::Concern
6
6
7
7
included do
8
- error_sharing = proc do
9
- # :inertia_errors are deleted from the session by the middleware
10
- if @_request && session [ :inertia_errors ] . present?
11
- { errors : session [ :inertia_errors ] }
12
- else
13
- { }
14
- end
15
- end
16
8
helper_method :inertia_headers
17
9
18
- class_attribute :shared_plain_data
19
- class_attribute :shared_blocks
20
- class_attribute :inertia_html_headers
10
+ before_action do
11
+ error_sharing = proc do
12
+ # :inertia_errors are deleted from the session by the middleware
13
+ if @_request && session [ :inertia_errors ] . present?
14
+ { errors : session [ :inertia_errors ] }
15
+ else
16
+ { }
17
+ end
18
+ end
21
19
22
- self . shared_plain_data = { }
23
- self . shared_blocks = [ error_sharing ]
24
- self . inertia_html_headers = [ ]
20
+ @_inertia_shared_plain_data ||= { }
21
+ @_inertia_shared_blocks ||= [ error_sharing ]
22
+ @_inertia_html_headers ||= [ ]
23
+ end
25
24
26
25
after_action do
27
26
cookies [ 'XSRF-TOKEN' ] = form_authenticity_token unless request . inertia? || !protect_against_forgery?
@@ -30,8 +29,10 @@ module Controller
30
29
31
30
module ClassMethods
32
31
def inertia_share ( hash = nil , &block )
33
- share_plain_data ( hash ) if hash
34
- share_block ( &block ) if block_given?
32
+ before_action do
33
+ @_inertia_shared_plain_data = @_inertia_shared_plain_data . merge ( hash ) if hash
34
+ @_inertia_shared_blocks = @_inertia_shared_blocks + [ block ] if block_given?
35
+ end
35
36
end
36
37
37
38
def use_inertia_instance_props
@@ -42,16 +43,16 @@ def use_inertia_instance_props
42
43
end
43
44
44
45
def share_plain_data ( hash )
45
- self . shared_plain_data = shared_plain_data . merge ( hash )
46
+ @_inertia_shared_plain_data = @_inertia_shared_plain_data . merge ( hash )
46
47
end
47
48
48
49
def share_block ( &block )
49
- self . shared_blocks = shared_blocks + [ block ]
50
+ @_inertia_shared_blocks = @_inertia_shared_blocks + [ block ]
50
51
end
51
52
end
52
53
53
54
def inertia_headers
54
- inertia_html_headers . join . html_safe
55
+ @_inertia_html_headers . join . html_safe
55
56
end
56
57
57
58
def default_render
@@ -63,7 +64,8 @@ def default_render
63
64
end
64
65
65
66
def shared_data
66
- shared_plain_data . merge ( evaluated_blocks )
67
+ return { } unless @_inertia_shared_plain_data
68
+ @_inertia_shared_plain_data . merge ( evaluated_blocks )
67
69
end
68
70
69
71
def redirect_to ( options = { } , response_options = { } )
@@ -107,7 +109,7 @@ def capture_inertia_errors(options)
107
109
end
108
110
109
111
def evaluated_blocks
110
- shared_blocks . map { |block | instance_exec ( &block ) } . reduce ( &:merge ) || { }
112
+ @_inertia_shared_blocks & .map { |block | instance_exec ( &block ) } & .reduce ( &:merge ) || { }
111
113
end
112
114
end
113
115
end
0 commit comments