Skip to content

Commit ba50ac4

Browse files
:refactor: refactor InertiaRails::Controller to use only instance variables
1 parent 55293f4 commit ba50ac4

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

lib/inertia_rails/controller.rb

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@ module Controller
55
extend ActiveSupport::Concern
66

77
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
168
helper_method :inertia_headers
179

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
2119

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
2524

2625
after_action do
2726
cookies['XSRF-TOKEN'] = form_authenticity_token unless request.inertia? || !protect_against_forgery?
@@ -30,8 +29,10 @@ module Controller
3029

3130
module ClassMethods
3231
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
3536
end
3637

3738
def use_inertia_instance_props
@@ -42,16 +43,16 @@ def use_inertia_instance_props
4243
end
4344

4445
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)
4647
end
4748

4849
def share_block(&block)
49-
self.shared_blocks = shared_blocks + [block]
50+
@_inertia_shared_blocks = @_inertia_shared_blocks + [block]
5051
end
5152
end
5253

5354
def inertia_headers
54-
inertia_html_headers.join.html_safe
55+
@_inertia_html_headers.join.html_safe
5556
end
5657

5758
def default_render
@@ -63,7 +64,8 @@ def default_render
6364
end
6465

6566
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)
6769
end
6870

6971
def redirect_to(options = {}, response_options = {})
@@ -107,7 +109,7 @@ def capture_inertia_errors(options)
107109
end
108110

109111
def evaluated_blocks
110-
shared_blocks.map { |block| instance_exec(&block) }.reduce(&:merge) || {}
112+
@_inertia_shared_blocks&.map { |block| instance_exec(&block) }&.reduce(&:merge) || {}
111113
end
112114
end
113115
end

lib/inertia_rails/renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def render_ssr
3232
uri = URI("#{::InertiaRails.ssr_url}/render")
3333
res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
3434

35-
@controller.inertia_html_headers = res['head']
35+
@controller.instance_variable_set("@_inertia_html_headers", res['head'])
3636
@render_method.call html: res['body'].html_safe, layout: layout, locals: (view_data).merge({page: page})
3737
end
3838

0 commit comments

Comments
 (0)