Skip to content

Commit 4d2468d

Browse files
committed
feat: add inertia_config to override global config in controllers
1 parent 967fa4a commit 4d2468d

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
lines changed

lib/inertia_rails/configuration.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@ class Configuration
1616

1717
OPTION_NAMES = DEFAULTS.keys.freeze
1818

19+
protected attr_reader :controller
1920
protected attr_reader :options
2021

21-
def initialize(**attrs)
22+
def initialize(controller: nil, **attrs)
23+
@controller = controller
2224
@options = attrs.extract!(*OPTION_NAMES)
2325

2426
unless attrs.empty?
2527
raise ArgumentError, "Unknown options for #{self.class}: #{attrs.keys}"
2628
end
2729
end
2830

31+
def bind_controller(controller)
32+
Configuration.new(**@options, controller: controller)
33+
end
34+
2935
def to_h
3036
@options.to_h
3137
end
@@ -36,8 +42,7 @@ def merge(config)
3642

3743
OPTION_NAMES.each do |option|
3844
define_method(option) {
39-
value = @options[option]
40-
value.respond_to?(:call) ? value.call : value
45+
evaluate_option @options[option]
4146
}
4247
define_method("#{option}=") { |value|
4348
@options[option] = value
@@ -47,5 +52,13 @@ def merge(config)
4752
def self.default
4853
new(**DEFAULTS)
4954
end
55+
56+
private
57+
58+
def evaluate_option(value)
59+
return value unless value.respond_to?(:call)
60+
return value.call unless controller
61+
controller.instance_exec(&value)
62+
end
5063
end
5164
end

lib/inertia_rails/controller.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,27 @@ def inertia_share(**args, &block)
2323
end
2424
end
2525

26+
def inertia_config(**attrs)
27+
config = InertiaRails::Configuration.new(**attrs)
28+
29+
if @inertia_configuration
30+
@inertia_configuration.merge!(config)
31+
else
32+
@inertia_configuration = config
33+
end
34+
end
35+
2636
def use_inertia_instance_props
2737
before_action do
2838
@_inertia_instance_props = true
2939
@_inertia_skip_props = view_assigns.keys + ['_inertia_skip_props']
3040
end
3141
end
42+
43+
def _inertia_configuration
44+
config = superclass.try(:_inertia_configuration) || ::InertiaRails.configuration
45+
@inertia_configuration ? config.merge(@inertia_configuration) : config
46+
end
3247
end
3348

3449
def default_render
@@ -61,7 +76,7 @@ def inertia_view_assigns
6176
end
6277

6378
def inertia_configuration
64-
::InertiaRails.configuration
79+
self.class._inertia_configuration.bind_controller(self)
6580
end
6681

6782
def inertia_shared_data
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class InertiaConfigTestController < ApplicationController
2+
inertia_config(
3+
ssr_enabled: true,
4+
deep_merge_shared_data: true,
5+
ssr_enabled: true,
6+
ssr_url: "http://localhost:7777",
7+
layout: "test",
8+
version: "2.0",
9+
)
10+
11+
def configuration
12+
render json: inertia_configuration.to_h
13+
end
14+
end

spec/dummy/app/controllers/inertia_rails_mimic_controller.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class InertiaRailsMimicController < ApplicationController
2-
before_action :enable_inertia_default, only: :default_render_test
2+
inertia_config(
3+
default_render: -> { action_name == "default_render_test" },
4+
)
35
use_inertia_instance_props
46

57
def instance_props_test
@@ -24,10 +26,4 @@ def provided_props_test
2426
def default_component_test
2527
render inertia: true
2628
end
27-
28-
def enable_inertia_default
29-
InertiaRails.configure do |config|
30-
config.default_render = true
31-
end
32-
end
3329
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Rails.application.routes.draw do
22
mount InertiaRails::Engine => "/inertia-rails"
33

4+
get 'configuration' => 'inertia_config_test#configuration'
45
get 'props' => 'inertia_render_test#props'
56
get 'view_data' => 'inertia_render_test#view_data'
67
get 'component' => 'inertia_render_test#component'

spec/inertia/configuration_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
RSpec.describe 'Inertia configuration', type: :request do
22
after { reset_config! }
33

4+
describe 'inertia_config' do
5+
it 'overrides the global values' do
6+
get configuration_path
7+
8+
expect(response.parsed_body.symbolize_keys).to eq(
9+
deep_merge_shared_data: true,
10+
default_render: false,
11+
layout: "test",
12+
ssr_enabled: true,
13+
ssr_url: "http://localhost:7777",
14+
version: "2.0",
15+
)
16+
end
17+
end
18+
419
describe '.version' do
520
subject { JSON.parse(response.body)['version'] }
621

0 commit comments

Comments
 (0)