diff --git a/lib/paper_trail/config.rb b/lib/paper_trail/config.rb index e9c311f0..2729b2fe 100644 --- a/lib/paper_trail/config.rb +++ b/lib/paper_trail/config.rb @@ -38,10 +38,10 @@ module PaperTrail end alias_method :track_associations?, :track_associations - # Indicates whether PaperTrail is on or off. + # Indicates whether PaperTrail is on or off. Default: true. def enabled - PaperTrail.paper_trail_store[:paper_trail_enabled].nil? || - PaperTrail.paper_trail_store[:paper_trail_enabled] + value = PaperTrail.paper_trail_store.fetch(:paper_trail_enabled, true) + value.nil? ? true : value end def enabled= enable diff --git a/spec/paper_trail/config_spec.rb b/spec/paper_trail/config_spec.rb new file mode 100644 index 00000000..88487f49 --- /dev/null +++ b/spec/paper_trail/config_spec.rb @@ -0,0 +1,52 @@ +require "rails_helper" + +module PaperTrail + RSpec.describe Config do + describe ".instance" do + it "returns the singleton instance" do + expect { described_class.instance }.to_not raise_error + end + end + + describe ".new" do + it "raises NoMethodError" do + expect { described_class.new }.to raise_error(NoMethodError) + end + end + + describe "#enabled" do + context "when paper_trail_enabled is true" do + it "returns true" do + store = double + allow(store).to receive(:fetch). + with(:paper_trail_enabled, true). + and_return(true) + allow(PaperTrail).to receive(:paper_trail_store).and_return(store) + expect(described_class.instance.enabled).to eq(true) + end + end + + context "when paper_trail_enabled is false" do + it "returns false" do + store = double + allow(store).to receive(:fetch). + with(:paper_trail_enabled, true). + and_return(false) + allow(PaperTrail).to receive(:paper_trail_store).and_return(store) + expect(described_class.instance.enabled).to eq(false) + end + end + + context "when paper_trail_enabled is nil" do + it "returns true" do + store = double + allow(store).to receive(:fetch). + with(:paper_trail_enabled, true). + and_return(nil) + allow(PaperTrail).to receive(:paper_trail_store).and_return(store) + expect(described_class.instance.enabled).to eq(true) + end + end + end + end +end