diff --git a/lib/paper_trail/record_trail.rb b/lib/paper_trail/record_trail.rb index 877acfd6..959e4df6 100644 --- a/lib/paper_trail/record_trail.rb +++ b/lib/paper_trail/record_trail.rb @@ -3,14 +3,37 @@ module PaperTrail # Represents the "paper trail" for a single record. class RecordTrail + DPR_TOUCH_WITH_VERSION = <<-STR.squish.freeze + my_model_instance.paper_trail.touch_with_version is deprecated, + please use my_model_instance.touch + STR DPR_WHODUNNIT = <<-STR.squish.freeze my_model_instance.paper_trail.whodunnit('John') is deprecated, please use PaperTrail.request(whodunnit: 'John') STR + DPR_WITHOUT_VERSIONING = <<-STR + my_model_instance.paper_trail.without_versioning is deprecated, without + an exact replacement. To disable versioning for a particular model, - DPR_TOUCH_WITH_VERSION = <<-STR.squish.freeze - my_model_instance.paper_trail.touch_with_version is deprecated, - please use my_model_instance.touch + ``` + PaperTrail.request.disable_model(Banana) + # changes to Banana model do not create versions, + # but eg. changes to Kiwi model do. + PaperTrail.request.enable_model(Banana) + ``` + + Or, you may want to disable all models, + + ``` + PaperTrail.request.enabled = false + # no versions created + PaperTrail.request.enabled = true + + # or, with a block, + PaperTrail.request(enabled: false) do + # no versions created + end + ``` STR RAILS_GTE_5_1 = ::ActiveRecord.gem_version >= ::Gem::Version.new("5.1.0.beta1") @@ -479,7 +502,7 @@ module PaperTrail attributes.each { |column| @record.send(:write_attribute, column, current_time) } - @record.paper_trail.without_versioning do + ::PaperTrail.request(enabled: false) do @record.save!(validate: false) end record_update(force: true, in_after_callback: false) @@ -523,7 +546,9 @@ module PaperTrail end # Executes the given method or block without creating a new version. + # @deprecated def without_versioning(method = nil) + ::ActiveSupport::Deprecation.warn(DPR_WITHOUT_VERSIONING, caller(1)) paper_trail_was_enabled = PaperTrail.request.enabled_for_model?(@record.class) PaperTrail.request.disable_model(@record.class) if method diff --git a/spec/models/widget_spec.rb b/spec/models/widget_spec.rb index d2514f85..02a8a30a 100644 --- a/spec/models/widget_spec.rb +++ b/spec/models/widget_spec.rb @@ -260,12 +260,14 @@ RSpec.describe Widget, type: :model do }.by(+1) end - it "does not create a version using without_versioning" do - count = widget.versions.count - widget.paper_trail.without_versioning do - widget.touch + context "request is disabled" do + it "does not create a version" do + count = widget.versions.count + PaperTrail.request(enabled: false) do + widget.touch + end + expect(count).to eq(count) end - expect(count).to eq(count) end end diff --git a/spec/paper_trail/model_spec.rb b/spec/paper_trail/model_spec.rb index 68aca8fd..b3037a8d 100644 --- a/spec/paper_trail/model_spec.rb +++ b/spec/paper_trail/model_spec.rb @@ -342,8 +342,10 @@ RSpec.describe(::PaperTrail, versioning: true) do context "when destroyed \"without versioning\"" do it "leave paper trail off after call" do + allow(::ActiveSupport::Deprecation).to receive(:warn) @widget.paper_trail.without_versioning(:destroy) expect(::PaperTrail.request.enabled_for_model?(Widget)).to eq(false) + expect(::ActiveSupport::Deprecation).to have_received(:warn).once end end @@ -361,28 +363,24 @@ RSpec.describe(::PaperTrail, versioning: true) do end context "when updated \"without versioning\"" do - before do + it "does not create new version" do + allow(::ActiveSupport::Deprecation).to receive(:warn) @widget.paper_trail.without_versioning do @widget.update_attributes(name: "Ford") end @widget.paper_trail.without_versioning do |w| w.update_attributes(name: "Nixon") end - end - - it "not create new version" do expect(@widget.versions.length).to(eq(@count)) - end - - it "enable paper trail after call" do expect(PaperTrail.request.enabled_for_model?(Widget)).to eq(true) + expect(::ActiveSupport::Deprecation).to have_received(:warn).twice end end context "given a symbol, specifying a method name" do it "does not create a new version" do allow(::ActiveSupport::Deprecation).to receive(:warn) - @widget.paper_trail.without_versioning(:touch_with_version) + @widget.paper_trail.without_versioning(:touch) expect(::ActiveSupport::Deprecation).to have_received(:warn).once expect(@widget.versions.length).to(eq(@count)) expect(::PaperTrail.request.enabled_for_model?(Widget)).to eq(true) diff --git a/spec/paper_trail/thread_safety_spec.rb b/spec/paper_trail/thread_safety_spec.rb index 7327627c..75ed1288 100644 --- a/spec/paper_trail/thread_safety_spec.rb +++ b/spec/paper_trail/thread_safety_spec.rb @@ -25,6 +25,7 @@ RSpec.describe PaperTrail do describe "#without_versioning" do it "is thread-safe" do + allow(::ActiveSupport::Deprecation).to receive(:warn) enabled = nil t1 = Thread.new do Widget.new.paper_trail.without_versioning do @@ -43,6 +44,7 @@ RSpec.describe PaperTrail do expect(t1.value).to eq(false) expect(t2.value).to eq(true) # see? unaffected by t1 expect(described_class.request.enabled_for_model?(Widget)).to eq(true) + expect(::ActiveSupport::Deprecation).to have_received(:warn).once end end end