Skip to content

Commit

Permalink
Allow configuration in initializers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Joel Ambass authored and jeremy committed Oct 31, 2018
1 parent d2a0ece commit 3c8f909
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/global_id/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ class Railtie < Rails::Railtie # :nodoc:
config.eager_load_namespaces << GlobalID

initializer 'global_id' do |app|
default_expires_in = 1.month
default_app_name = app.railtie_name.remove('_application').dasherize

app.config.global_id.app ||= app.railtie_name.remove('_application').dasherize
GlobalID.app = app.config.global_id.app

app.config.global_id.expires_in ||= 1.month
SignedGlobalID.expires_in = app.config.global_id.expires_in
GlobalID.app = app.config.global_id.app ||= default_app_name
SignedGlobalID.expires_in = app.config.global_id.expires_in ||= default_expires_in

config.after_initialize do
GlobalID.app = app.config.global_id.app ||= default_app_name
SignedGlobalID.expires_in = app.config.global_id.expires_in ||= default_expires_in

app.config.global_id.verifier ||= begin
GlobalID::Verifier.new(app.key_generator.generate_key('signed_global_ids'))
rescue ArgumentError
Expand Down
12 changes: 12 additions & 0 deletions test/cases/railtie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def setup
assert_equal 'foo', GlobalID.app
end

test 'config.global_id can be used to set configurations after the railtie has been loaded' do
@app.config.eager_load = true
@app.config.before_eager_load do
@app.config.global_id.app = 'foobar'
@app.config.global_id.expires_in = 1.year
end

@app.initialize!
assert_equal 'foobar', GlobalID.app
assert_equal 1.year, SignedGlobalID.expires_in
end

test 'SignedGlobalID.verifier defaults to Blog::Application.message_verifier(:signed_global_ids) when secret_token is present' do
@app.config.secret_token = ('x' * 30)
@app.initialize!
Expand Down

0 comments on commit 3c8f909

Please sign in to comment.