Add global defaults for nonce, nonce_timeout, and mutate (v0.3.1) - #4
Merged
Merged
Conversation
All three options can now be configured in an initializer and overridden per-controller or per-action. Globals are resolved at request time so an explicit false at the call site correctly overrides a global true.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces globally configurable defaults for Spamtrap’s nonce, nonce timeout, and field-mutation features, while preserving the ability to override behavior per-controller/per-action and per-helper call site.
Changes:
- Add global configuration accessors (
Spamtrap.nonce,Spamtrap.nonce_timeout,Spamtrap.mutate) and use them as defaults when per-call options are not provided. - Update the controller macro to resolve global defaults at request time so explicit
falseper-call overrides globaltrue. - Add/extend tests and documentation, and bump gem version to
0.3.1.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| test/test_helper.rb | Adds routes for new global-default test controllers. |
| test/controller_test.rb | Adds controllers/tests covering global defaults and override behavior. |
| README.rdoc | Documents initializer-based global defaults and updates nonce/mutation sections. |
| lib/spamtrap/version.rb | Bumps gem version to 0.3.1. |
| lib/spamtrap/helper.rb | Makes FormBuilder#spamtrap default to global nonce/mutate when not specified. |
| lib/spamtrap/controller.rb | Resolves global defaults at request time inside before_action. |
| lib/spamtrap.rb | Adds global config writers and readers for nonce and mutate. |
| Gemfile.lock | Updates locked gem version to 0.3.1. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
11
to
13
| def nonce_timeout | ||
| @nonce_timeout || 1800 | ||
| end |
Comment on lines
+41
to
44
| mutate = options.key?(:mutate) ? options.delete(:mutate) : Spamtrap.mutate | ||
| nonce = options.key?(:nonce) ? options.delete(:nonce) : Spamtrap.nonce | ||
| options.reverse_merge!(class: 'spamtrap') | ||
|
|
Comment on lines
+70
to
+72
| Enable globally via the initializer, or per-controller: | ||
|
|
||
| class CommentsController < ApplicationController | ||
| spamtrap :sarah_palin_walks_with_dinosaurs, nonce: true, only: %i(create update) | ||
| end | ||
| spamtrap :sarah_palin_walks_with_dinosaurs, nonce: true, only: %i(create update) |
Comment on lines
+94
to
+96
| Enable globally via the initializer, or per-controller: | ||
|
|
||
| class CommentsController < ApplicationController | ||
| spamtrap :sarah_palin_walks_with_dinosaurs, mutate: true, only: %i(create update) | ||
| end | ||
| spamtrap :sarah_palin_walks_with_dinosaurs, mutate: true, only: %i(create update) |
Comment on lines
+298
to
+311
| def test_global_mutate_default_remaps_encrypted_fields | ||
| body_token = spamtrap_encrypt_field('body', MUTATION_SALT) | ||
| timestamp = Time.now.to_i | ||
| post :create, params: { | ||
| trap_field: '', | ||
| spamtrap_timestamp: timestamp, | ||
| spamtrap_nonce: generate_nonce(timestamp), | ||
| spamtrap_mutation_salt: MUTATION_SALT_HEX, | ||
| comment: { body_token => 'Hello' } | ||
| } | ||
| assert_response :ok | ||
| assert_equal 'body', response.body | ||
| end | ||
| end |
| # Capture explicit per-call values; use sentinel so globals are read | ||
| # at request time rather than at class definition time. | ||
| nonce_opt = options.key?(:nonce) ? options.delete(:nonce) : :global | ||
| timeout_opt = options.key?(:nonce_timeout) ? options.delete(:nonce_timeout) : :global |
cedric
added a commit
that referenced
this pull request
Jul 8, 2026
Add global defaults for nonce, nonce_timeout, and mutate (v0.3.1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All three options can now be configured in an initializer and overridden per-controller or per-action. Globals are resolved at request time so an explicit false at the call site correctly overrides a global true.