From 9c6c62d77ea8716e977aab8cac6e9096167bd507 Mon Sep 17 00:00:00 2001 From: Jared Beck Date: Mon, 7 Dec 2015 12:12:02 -0500 Subject: [PATCH] Reduce duplication in Config#enabled --- lib/paper_trail/config.rb | 6 ++-- spec/paper_trail/config_spec.rb | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 spec/paper_trail/config_spec.rb 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