Skip to content

Commit 5a7fe11

Browse files
committed
Add support for configuration via ENV
1 parent 5e1abc2 commit 5a7fe11

File tree

3 files changed

+88
-19
lines changed

3 files changed

+88
-19
lines changed

lib/inertia_rails/configuration.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ class Configuration
2929

3030
OPTION_NAMES = DEFAULTS.keys.freeze
3131

32-
protected attr_reader :controller
33-
protected attr_reader :options
32+
def self.default
33+
new(**DEFAULTS).with_env_options
34+
end
3435

3536
def initialize(controller: nil, **attrs)
3637
@controller = controller
@@ -42,7 +43,7 @@ def initialize(controller: nil, **attrs)
4243
end
4344

4445
def bind_controller(controller)
45-
Configuration.new(**@options, controller: controller)
46+
Configuration.new(**options, controller: controller)
4647
end
4748

4849
def freeze
@@ -71,23 +72,40 @@ def component_path_resolver(path:, action:)
7172

7273
OPTION_NAMES.each do |option|
7374
define_method(option) {
74-
evaluate_option @options[option]
75+
evaluate_option options[option]
7576
} unless method_defined?(option)
7677
define_method("#{option}=") { |value|
7778
@options[option] = value
7879
}
7980
end
8081

81-
def self.default
82-
new(**DEFAULTS)
82+
protected attr_reader :controller
83+
84+
def options
85+
@with_env_options ? @options.merge(env_options) : @options
8386
end
87+
protected :options
8488

85-
private
89+
def with_env_options
90+
@with_env_options = true
91+
self
92+
end
93+
94+
private
8695

8796
def evaluate_option(value)
8897
return value unless value.respond_to?(:call)
8998
return value.call unless controller
9099
controller.instance_exec(&value)
91100
end
101+
102+
def env_options
103+
@env_options ||= DEFAULTS.keys.each_with_object({}) do |key, hash|
104+
value = ENV.fetch("INERTIA_#{key.to_s.upcase}", nil)
105+
next if value.nil?
106+
107+
hash[key] = %w[true false].include?(value) ? value == 'true' : value
108+
end
109+
end
92110
end
93111
end

spec/inertia/configuration_spec.rb

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,60 @@
1616
expect(merged_config.ssr_enabled).to eq false
1717
}.not_to raise_error
1818
end
19+
20+
context 'with environment variables' do
21+
it 'always prioritizes environment variables' do
22+
with_env(
23+
'INERTIA_SSR_ENABLED' => 'true',
24+
'INERTIA_SSR_URL' => 'http://env-ssr-url:1234',
25+
'INERTIA_DEEP_MERGE_SHARED_DATA' => 'true'
26+
) do
27+
config = InertiaRails::Configuration.default
28+
29+
config.ssr_enabled = false
30+
31+
expect(config.ssr_enabled).to eq true
32+
expect(config.ssr_url).to eq 'http://env-ssr-url:1234'
33+
expect(config.deep_merge_shared_data).to eq true
34+
end
35+
end
36+
37+
it 'respects environment variables when merging defaults' do
38+
with_env(
39+
'INERTIA_SSR_ENABLED' => 'true'
40+
) do
41+
config = InertiaRails::Configuration.default
42+
config.ssr_enabled = false
43+
config.deep_merge_shared_data = true
44+
45+
controller_config = InertiaRails::Configuration.new(ssr_url: 'http://ssr-url:1234')
46+
controller_config.with_defaults(config)
47+
48+
expect(controller_config.ssr_enabled).to eq true
49+
expect(controller_config.ssr_url).to eq 'http://ssr-url:1234'
50+
expect(controller_config.deep_merge_shared_data).to eq true
51+
end
52+
end
53+
end
1954
end
2055

2156
describe 'inertia_config' do
22-
it 'overrides the global values' do
23-
get configuration_path
24-
25-
expect(response.parsed_body.symbolize_keys).to include(
26-
deep_merge_shared_data: true,
27-
default_render: false,
28-
layout: "test",
29-
ssr_enabled: true,
30-
ssr_url: "http://localhost:7777",
31-
version: "2.0",
32-
encrypt_history: false,
33-
)
57+
it 'overrides the global values and ENV' do
58+
with_env(
59+
'INERTIA_SSR_URL' => 'http://env-ssr-url:1234'
60+
) do
61+
get configuration_path
62+
63+
expect(response.parsed_body.symbolize_keys).to include(
64+
deep_merge_shared_data: true,
65+
default_render: false,
66+
layout: 'test',
67+
ssr_enabled: true,
68+
ssr_url: 'http://localhost:7777',
69+
version: '2.0',
70+
encrypt_history: false
71+
)
72+
end
3473
end
3574
end
3675

spec/support/helper_module.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module HelperModule
24
def with_forgery_protection
35
orig = ActionController::Base.allow_forgery_protection
@@ -8,4 +10,14 @@ def with_forgery_protection
810
ActionController::Base.allow_forgery_protection = orig
911
end
1012
end
13+
14+
def with_env(**env)
15+
orig = ENV.to_h
16+
begin
17+
ENV.replace(env)
18+
yield if block_given?
19+
ensure
20+
ENV.replace(orig)
21+
end
22+
end
1123
end

0 commit comments

Comments
 (0)