Skip to content

Commit c5624be

Browse files
committed
refactor: allow controllers to override the app version
With this alone, the same Rails app could serve different Inertia apps, setting a different version for each one in base controllers.
1 parent b5187be commit c5624be

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/inertia_rails/controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def inertia_view_assigns
6262

6363
private
6464

65+
def inertia_configuration
66+
::InertiaRails.configuration
67+
end
68+
6569
def inertia_layout
6670
layout = ::InertiaRails.layout
6771

lib/inertia_rails/middleware.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ def initialize(app)
55
end
66

77
def call(env)
8-
InertiaRailsRequest.new(@app, env)
9-
.response
8+
InertiaRailsRequest.new(@app, env).response
109
ensure
1110
::InertiaRails.reset!
1211
end
@@ -60,11 +59,15 @@ def get?
6059
request_method == 'GET'
6160
end
6261

62+
def controller
63+
@env["action_controller.instance"]
64+
end
65+
6366
def request_method
6467
@env['REQUEST_METHOD']
6568
end
6669

67-
def inertia_version
70+
def client_version
6871
@env['HTTP_X_INERTIA_VERSION']
6972
end
7073

@@ -73,17 +76,15 @@ def inertia_request?
7376
end
7477

7578
def version_stale?
76-
sent_version != saved_version
79+
coerce_version(client_version) != coerce_version(server_version)
7780
end
7881

79-
def sent_version
80-
return nil if inertia_version.nil?
81-
82-
InertiaRails.version.is_a?(Numeric) ? inertia_version.to_f : inertia_version
82+
def server_version
83+
controller.send(:inertia_configuration).version
8384
end
8485

85-
def saved_version
86-
InertiaRails.version.is_a?(Numeric) ? InertiaRails.version.to_f : InertiaRails.version
86+
def coerce_version(version)
87+
server_version.is_a?(Numeric) ? version.to_f : version
8788
end
8889

8990
def force_refresh(request)

0 commit comments

Comments
 (0)