Skip to content

Add global defaults for nonce, nonce_timeout, and mutate (v0.3.1) - #4

Merged
cedric merged 1 commit into
masterfrom
cedric/global-config-defaults
Jul 8, 2026
Merged

cedric merged 1 commit into
masterfrom
cedric/global-config-defaults

Conversation

@cedric

@cedric cedric commented Jul 8, 2026

Copy link
Copy Markdown
Owner

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.

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.
Copilot AI review requested due to automatic review settings July 8, 2026 15:57
@cedric
cedric merged commit 703da8f into master Jul 8, 2026
1 check passed
@cedric
cedric deleted the cedric/global-config-defaults branch July 8, 2026 15:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 false per-call overrides global true.
  • 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 thread lib/spamtrap.rb
Comment on lines 11 to 13
def nonce_timeout
@nonce_timeout || 1800
end
Comment thread lib/spamtrap/helper.rb
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 thread README.rdoc
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 thread README.rdoc
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 thread test/controller_test.rb
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants