Skip to content

Commit 3c8f909

Browse files
Joel Ambassjeremy
Joel Ambass
authored andcommitted
Allow configuration in initializers
Closes #105 This sets `GlobalID.app` and `SecureGlobalID.expires_in` to the default values and then allows overwriting them via `config.global_id.app` and `config.global_id.expires_in` in an `config/initializers/*.rb` file.
1 parent d2a0ece commit 3c8f909

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/global_id/railtie.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ class Railtie < Rails::Railtie # :nodoc:
1414
config.eager_load_namespaces << GlobalID
1515

1616
initializer 'global_id' do |app|
17+
default_expires_in = 1.month
18+
default_app_name = app.railtie_name.remove('_application').dasherize
1719

18-
app.config.global_id.app ||= app.railtie_name.remove('_application').dasherize
19-
GlobalID.app = app.config.global_id.app
20-
21-
app.config.global_id.expires_in ||= 1.month
22-
SignedGlobalID.expires_in = app.config.global_id.expires_in
20+
GlobalID.app = app.config.global_id.app ||= default_app_name
21+
SignedGlobalID.expires_in = app.config.global_id.expires_in ||= default_expires_in
2322

2423
config.after_initialize do
24+
GlobalID.app = app.config.global_id.app ||= default_app_name
25+
SignedGlobalID.expires_in = app.config.global_id.expires_in ||= default_expires_in
26+
2527
app.config.global_id.verifier ||= begin
2628
GlobalID::Verifier.new(app.key_generator.generate_key('signed_global_ids'))
2729
rescue ArgumentError

test/cases/railtie_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ def setup
2828
assert_equal 'foo', GlobalID.app
2929
end
3030

31+
test 'config.global_id can be used to set configurations after the railtie has been loaded' do
32+
@app.config.eager_load = true
33+
@app.config.before_eager_load do
34+
@app.config.global_id.app = 'foobar'
35+
@app.config.global_id.expires_in = 1.year
36+
end
37+
38+
@app.initialize!
39+
assert_equal 'foobar', GlobalID.app
40+
assert_equal 1.year, SignedGlobalID.expires_in
41+
end
42+
3143
test 'SignedGlobalID.verifier defaults to Blog::Application.message_verifier(:signed_global_ids) when secret_token is present' do
3244
@app.config.secret_token = ('x' * 30)
3345
@app.initialize!

0 commit comments

Comments
 (0)