Skip to content

Commit de7d2ae

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

File tree

4 files changed

+82
-19
lines changed

4 files changed

+82
-19
lines changed

lib/inertia_rails/configuration.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ class Configuration
2929

3030
OPTION_NAMES = DEFAULTS.keys.freeze
3131

32+
class << self
33+
def default
34+
new(**DEFAULTS, **env_options)
35+
end
36+
37+
private
38+
39+
def env_options
40+
DEFAULTS.keys.each_with_object({}) do |key, hash|
41+
value = ENV.fetch("INERTIA_#{key.to_s.upcase}", nil)
42+
next if value.nil?
43+
44+
hash[key] = %w[true false].include?(value) ? value == 'true' : value
45+
end
46+
end
47+
end
48+
3249
protected attr_reader :controller
3350
protected attr_reader :options
3451

@@ -71,18 +88,14 @@ def component_path_resolver(path:, action:)
7188

7289
OPTION_NAMES.each do |option|
7390
define_method(option) {
74-
evaluate_option @options[option]
91+
evaluate_option options[option]
7592
} unless method_defined?(option)
7693
define_method("#{option}=") { |value|
7794
@options[option] = value
7895
}
7996
end
8097

81-
def self.default
82-
new(**DEFAULTS)
83-
end
84-
85-
private
98+
private
8699

87100
def evaluate_option(value)
88101
return value unless value.respond_to?(:call)

lib/inertia_rails/renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def render_ssr
6060

6161
def layout
6262
layout = configuration.layout
63-
layout.nil? ? true : layout
63+
layout.nil? || layout
6464
end
6565

6666
def shared_data

spec/inertia/configuration_spec.rb

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,59 @@
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 initialized 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 false
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.deep_merge_shared_data = true
43+
44+
controller_config = InertiaRails::Configuration.new(ssr_url: 'http://ssr-url:1234')
45+
controller_config.with_defaults(config)
46+
47+
expect(controller_config.ssr_enabled).to eq true
48+
expect(controller_config.ssr_url).to eq 'http://ssr-url:1234'
49+
expect(controller_config.deep_merge_shared_data).to eq true
50+
end
51+
end
52+
end
1953
end
2054

2155
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-
)
56+
it 'overrides the global values and ENV' do
57+
with_env(
58+
'INERTIA_SSR_URL' => 'http://env-ssr-url:1234'
59+
) do
60+
get configuration_path
61+
62+
expect(response.parsed_body.symbolize_keys).to include(
63+
deep_merge_shared_data: true,
64+
default_render: false,
65+
layout: 'test',
66+
ssr_enabled: true,
67+
ssr_url: 'http://localhost:7777',
68+
version: '2.0',
69+
encrypt_history: false
70+
)
71+
end
3472
end
3573
end
3674

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)