Skip to content

Commit

Permalink
Allow expires_in to be configured to nil globally
Browse files Browse the repository at this point in the history
This makes it possible to turn off expiration globally.

Closes #127.
  • Loading branch information
tekin committed Jul 27, 2020
1 parent 0f68538 commit 332ed94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/global_id/railtie.rb
Expand Up @@ -18,12 +18,16 @@ class Railtie < Rails::Railtie # :nodoc:
default_expires_in = 1.month
default_app_name = app.railtie_name.remove('_application').dasherize

app.config.global_id.expires_in = app.config.global_id.fetch(:expires_in, default_expires_in)

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

config.after_initialize do
app.config.global_id.expires_in = app.config.global_id.fetch(:expires_in, default_expires_in)

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

app.config.global_id.verifier ||= begin
GlobalID::Verifier.new(app.key_generator.generate_key('signed_global_ids'))
Expand Down
17 changes: 17 additions & 0 deletions test/cases/railtie_test.rb
Expand Up @@ -29,6 +29,12 @@ def setup
assert_equal 'foo', GlobalID.app
end

test 'SignedGlobalID.expires_in can be explicitly set to nil with config.global_id.expires_in' do
@app.config.global_id.expires_in = nil
@app.initialize!
assert_nil SignedGlobalID.expires_in
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
Expand All @@ -41,6 +47,17 @@ def setup
assert_equal 1.year, SignedGlobalID.expires_in
end

test 'config.global_id can be used to explicitly set SignedGlobalID.expires_in to nil after the railtie has been loaded' do
@app.config.eager_load = true
@app.config.before_eager_load do
@app.config.global_id.expires_in = nil
end

@app.initialize!
assert_nil SignedGlobalID.expires_in
end


test 'SignedGlobalID.verifier defaults to Blog::Application.message_verifier(:signed_global_ids) when secret_key_base is present' do
@app.initialize!
message = {id: 42}
Expand Down

0 comments on commit 332ed94

Please sign in to comment.