Skip to content

Commit c3821b8

Browse files
jathaydeclaude
andcommitted
fix(loader): require "guardrails/configuration" from the top-level entry
Local-build verification (gem build → install in sandbox → require "guardrails") surfaced that `Guardrails.configure` was undefined unless a caller also `require`d `guardrails/configuration` themselves. That's exactly what an embedded user's `config/initializers/guardrails.rb` would NOT do — Rails autoloads the initializer after `require "guardrails"` and no other code path pulls in Configuration unless the rake task does (which doesn't run at boot). So `Guardrails.configure { |c| c.visual_diff.enabled = true }` in an initializer NoMethodErrors on first app boot. Fix: require "guardrails/configuration" from `lib/guardrails.rb`. Two-line addition, no behavior change for anyone who was already requiring detector files directly. Regression spec in `spec/guardrails_spec.rb` locks the contract: asserts both Guardrails.configure and .configuration are callable after `require "guardrails"` alone, and the block actually sets the values. Worth noting this slipped past 459 specs because every previous visual_diff/configuration spec explicitly `require`d both files. The actual user code path (initializer-only require) wasn't covered until now. Full suite 461/461 (up from 459). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 6bce923 commit c3821b8

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

lib/guardrails.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# frozen_string_literal: true
22

33
require_relative "guardrails/version"
4+
# Configuration carries the `Guardrails.configure { |c| ... }` block
5+
# users place in `config/initializers/guardrails.rb`. Loaded eagerly
6+
# at `require "guardrails"` time so the initializer doesn't NoMethodError
7+
# on first use — caught by the local-build verification of 1.0.0
8+
# before the third publish attempt.
9+
require_relative "guardrails/configuration"
410
require_relative "guardrails/railtie" if defined?(Rails::Railtie)
511

612
module Guardrails

spec/guardrails_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,27 @@
44
it "has a version number" do
55
expect(Guardrails::VERSION).not_to be_nil
66
end
7+
8+
# The `Guardrails.configure { |c| c.visual_diff.enabled = true }` block
9+
# lives in `config/initializers/guardrails.rb` for embedded installs.
10+
# That initializer is invoked after `require "guardrails"` and nothing
11+
# else — if configuration.rb isn't auto-loaded by the top-level entry,
12+
# the initializer NoMethodErrors on first boot. Caught by 1.0.0's
13+
# local-build verification; locking in here so a future refactor of
14+
# `lib/guardrails.rb` doesn't silently regress.
15+
describe "require \"guardrails\"" do
16+
it "exposes Guardrails.configure (Configuration auto-loaded)" do
17+
expect(described_class).to respond_to(:configure)
18+
expect(described_class).to respond_to(:configuration)
19+
end
20+
21+
it "yields a usable Configuration in the block" do
22+
expect {
23+
described_class.configure { |c| c.visual_diff.enabled = true }
24+
}.not_to raise_error
25+
expect(described_class.configuration.visual_diff.enabled).to be true
26+
ensure
27+
described_class.reset_configuration!
28+
end
29+
end
730
end

0 commit comments

Comments
 (0)