Reduce duplication in Config#enabled

This commit is contained in:
Jared Beck 2015-12-07 12:12:02 -05:00
parent cfab26b64d
commit 9c6c62d77e
2 changed files with 55 additions and 3 deletions

View File

@ -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

View File

@ -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