11require_relative "inertia_rails"
2- require_relative "helper"
32
43module InertiaRails
54 module Controller
65 extend ActiveSupport ::Concern
76
87 included do
8+ helper_method :inertia_headers
9+
910 before_action do
10- # :inertia_errors are deleted from the session by the middleware
11- InertiaRails . share ( errors : session [ :inertia_errors ] ) if session [ :inertia_errors ] . present?
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
19+
20+ @_inertia_shared_plain_data ||= { }
21+ @_inertia_shared_blocks ||= [ error_sharing ]
22+ @_inertia_html_headers ||= [ ]
1223 end
13- helper ::InertiaRails ::Helper
1424
1525 after_action do
1626 cookies [ 'XSRF-TOKEN' ] = form_authenticity_token unless !protect_against_forgery?
1727 end
1828 end
1929
2030 module ClassMethods
21- def inertia_share ( ** args , &block )
31+ def inertia_share ( hash = nil , &block )
2232 before_action do
23- InertiaRails . share ( ** args ) if args
24- InertiaRails . share_block ( block ) if block
33+ @_inertia_shared_plain_data = @_inertia_shared_plain_data . merge ( hash ) if hash
34+ @_inertia_shared_blocks = @_inertia_shared_blocks + [ block ] if block_given?
2535 end
2636 end
2737
@@ -33,6 +43,14 @@ def use_inertia_instance_props
3343 end
3444 end
3545
46+ def inertia_headers
47+ @_inertia_html_headers . join . html_safe
48+ end
49+
50+ def inertia_headers = ( value )
51+ @_inertia_html_headers = value
52+ end
53+
3654 def default_render
3755 if InertiaRails . default_render?
3856 render ( inertia : true )
@@ -41,6 +59,10 @@ def default_render
4159 end
4260 end
4361
62+ def shared_data
63+ ( @_inertia_shared_plain_data || { } ) . merge ( evaluated_blocks )
64+ end
65+
4466 def redirect_to ( options = { } , response_options = { } )
4567 capture_inertia_errors ( response_options )
4668 super ( options , response_options )
@@ -80,5 +102,9 @@ def capture_inertia_errors(options)
80102 session [ :inertia_errors ] = inertia_errors
81103 end
82104 end
105+
106+ def evaluated_blocks
107+ ( @_inertia_shared_blocks || [ ] ) . map { |block | instance_exec ( &block ) } . reduce ( &:merge ) || { }
108+ end
83109 end
84110end
0 commit comments