Skip to content

Commit 6c76a33

Browse files
authored
Merge pull request #111 from PedroAugustoRamalhoDuarte/fix-data-sharing
Refactor inertia share to use instance variables
2 parents 6aa0edf + c622c7a commit 6c76a33

16 files changed

+128
-93
lines changed

lib/inertia_rails/controller.rb

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
require_relative "inertia_rails"
2-
require_relative "helper"
32

43
module 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
84110
end

lib/inertia_rails/helper.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/inertia_rails/inertia_rails.rb

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,10 @@
33
require 'inertia_rails/lazy'
44

55
module InertiaRails
6-
thread_mattr_accessor :threadsafe_shared_plain_data
7-
thread_mattr_accessor :threadsafe_shared_blocks
8-
thread_mattr_accessor :threadsafe_html_headers
9-
106
def self.configure
117
yield(Configuration)
128
end
139

14-
# "Getters"
15-
def self.shared_data(controller)
16-
shared_plain_data.
17-
merge!(evaluated_blocks(controller, shared_blocks))
18-
end
19-
2010
def self.version
2111
Configuration.evaluated_version
2212
end
@@ -35,35 +25,12 @@ def self.ssr_url
3525

3626
def self.default_render?
3727
Configuration.default_render
38-
end
39-
40-
def self.html_headers
41-
self.threadsafe_html_headers || []
4228
end
4329

4430
def self.deep_merge_shared_data?
4531
Configuration.deep_merge_shared_data
4632
end
4733

48-
# "Setters"
49-
def self.share(**args)
50-
self.shared_plain_data = self.shared_plain_data.merge(args)
51-
end
52-
53-
def self.share_block(block)
54-
self.shared_blocks = self.shared_blocks + [block]
55-
end
56-
57-
def self.html_headers=(headers)
58-
self.threadsafe_html_headers = headers
59-
end
60-
61-
def self.reset!
62-
self.shared_plain_data = {}
63-
self.shared_blocks = []
64-
self.html_headers = []
65-
end
66-
6734
def self.lazy(value = nil, &block)
6835
InertiaRails::Lazy.new(value, &block)
6936
end
@@ -82,25 +49,4 @@ def self.evaluated_version
8249
self.version.respond_to?(:call) ? self.version.call : self.version
8350
end
8451
end
85-
86-
# Getters and setters to provide default values for the threadsafe attributes
87-
def self.shared_plain_data
88-
self.threadsafe_shared_plain_data || {}
89-
end
90-
91-
def self.shared_plain_data=(val)
92-
self.threadsafe_shared_plain_data = val
93-
end
94-
95-
def self.shared_blocks
96-
self.threadsafe_shared_blocks || []
97-
end
98-
99-
def self.shared_blocks=(val)
100-
self.threadsafe_shared_blocks = val
101-
end
102-
103-
def self.evaluated_blocks(controller, blocks)
104-
blocks.flat_map { |block| controller.instance_exec(&block) }.reduce(&:merge) || {}
105-
end
10652
end

lib/inertia_rails/middleware.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ def initialize(app)
77
def call(env)
88
InertiaRailsRequest.new(@app, env)
99
.response
10-
ensure
11-
::InertiaRails.reset!
1210
end
1311

1412
class InertiaRailsRequest
@@ -22,7 +20,7 @@ def response
2220
status, headers, body = @app.call(@env)
2321
request = ActionDispatch::Request.new(@env)
2422

25-
# Inertia errors are added to the session via redirect_to
23+
# Inertia errors are added to the session via redirect_to
2624
request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
2725

2826
status = 303 if inertia_non_post_redirect?(status)
@@ -97,4 +95,3 @@ def copy_xsrf_to_csrf!
9795
end
9896
end
9997
end
100-

lib/inertia_rails/renderer.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'net/http'
2-
require 'json'
31
require_relative "inertia_rails"
42

53
module InertiaRails
@@ -33,8 +31,8 @@ def render
3331
def render_ssr
3432
uri = URI("#{::InertiaRails.ssr_url}/render")
3533
res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
36-
37-
::InertiaRails.html_headers = res['head']
34+
35+
@controller.inertia_headers = res['head']
3836
@render_method.call html: res['body'].html_safe, layout: layout, locals: (view_data).merge({page: page})
3937
end
4038

@@ -48,7 +46,7 @@ def computed_props
4846
#
4947
# Functionally, this permits using either string or symbol keys in the controller. Since the results
5048
# is cast to json, we should treat string/symbol keys as identical.
51-
_props = ::InertiaRails.shared_data(@controller).deep_symbolize_keys.send(prop_merge_method, @props.deep_symbolize_keys).select do |key, prop|
49+
_props = @controller.shared_data.merge.deep_symbolize_keys.send(prop_merge_method, @props.deep_symbolize_keys).select do |key, prop|
5250
if rendering_partial_component?
5351
key.in? partial_keys
5452
else

lib/inertia_rails/rspec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def inertia_tests_setup?
6565
}
6666

6767
config.before(:each, inertia: true) do
68-
::InertiaRails.reset!
6968
new_renderer = InertiaRails::Renderer.method(:new)
7069
allow(InertiaRails::Renderer).to receive(:new) do |component, controller, request, response, render, named_args|
7170
new_renderer.call(component, controller, request, response, inertia_wrap_render(render), **named_args)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class InertiaConditionalSharingController < ApplicationController
2+
before_action :conditionally_share_props, only: [:show]
3+
inertia_share normal_shared_prop: 1
4+
5+
def index
6+
render inertia: 'EmptyTestComponent', props: {
7+
index_only_prop: 1,
8+
}
9+
end
10+
11+
def show
12+
render inertia: 'EmptyTestComponent', props: {
13+
show_only_prop: 1,
14+
}
15+
end
16+
17+
protected
18+
19+
def conditionally_share_props
20+
self.class.inertia_share conditionally_shared_show_prop: 1
21+
end
22+
end

spec/dummy/app/controllers/inertia_share_test_controller.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ class InertiaShareTestController < ApplicationController
44
inertia_share do
55
{
66
position: 'center',
7-
number: 29,
7+
number: number,
88
}
99
end
10-
10+
1111
def share
1212
render inertia: 'ShareTestComponent'
1313
end
14+
15+
private
16+
17+
def number
18+
29
19+
end
1420
end

spec/dummy/app/controllers/inertia_test_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ def content_type_test
6464
format.xml { render xml: [ 1, 2, 3 ] }
6565
end
6666
end
67+
68+
def redirect_to_share_test
69+
redirect_to share_path
70+
end
6771
end

spec/dummy/config/application.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,3 @@ class Application < Rails::Application
3434
config.secret_key_base = SecureRandom.hex
3535
end
3636
end
37-

0 commit comments

Comments
 (0)