Deprecate RecordTrail#without_versioning
This commit is contained in:
parent
e91804afb7
commit
8730d7e5fc
|
@ -3,14 +3,37 @@
|
||||||
module PaperTrail
|
module PaperTrail
|
||||||
# Represents the "paper trail" for a single record.
|
# Represents the "paper trail" for a single record.
|
||||||
class RecordTrail
|
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
|
DPR_WHODUNNIT = <<-STR.squish.freeze
|
||||||
my_model_instance.paper_trail.whodunnit('John') is deprecated,
|
my_model_instance.paper_trail.whodunnit('John') is deprecated,
|
||||||
please use PaperTrail.request(whodunnit: 'John')
|
please use PaperTrail.request(whodunnit: 'John')
|
||||||
STR
|
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,
|
PaperTrail.request.disable_model(Banana)
|
||||||
please use my_model_instance.touch
|
# 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
|
STR
|
||||||
|
|
||||||
RAILS_GTE_5_1 = ::ActiveRecord.gem_version >= ::Gem::Version.new("5.1.0.beta1")
|
RAILS_GTE_5_1 = ::ActiveRecord.gem_version >= ::Gem::Version.new("5.1.0.beta1")
|
||||||
|
@ -479,7 +502,7 @@ module PaperTrail
|
||||||
attributes.each { |column|
|
attributes.each { |column|
|
||||||
@record.send(:write_attribute, column, current_time)
|
@record.send(:write_attribute, column, current_time)
|
||||||
}
|
}
|
||||||
@record.paper_trail.without_versioning do
|
::PaperTrail.request(enabled: false) do
|
||||||
@record.save!(validate: false)
|
@record.save!(validate: false)
|
||||||
end
|
end
|
||||||
record_update(force: true, in_after_callback: false)
|
record_update(force: true, in_after_callback: false)
|
||||||
|
@ -523,7 +546,9 @@ module PaperTrail
|
||||||
end
|
end
|
||||||
|
|
||||||
# Executes the given method or block without creating a new version.
|
# Executes the given method or block without creating a new version.
|
||||||
|
# @deprecated
|
||||||
def without_versioning(method = nil)
|
def without_versioning(method = nil)
|
||||||
|
::ActiveSupport::Deprecation.warn(DPR_WITHOUT_VERSIONING, caller(1))
|
||||||
paper_trail_was_enabled = PaperTrail.request.enabled_for_model?(@record.class)
|
paper_trail_was_enabled = PaperTrail.request.enabled_for_model?(@record.class)
|
||||||
PaperTrail.request.disable_model(@record.class)
|
PaperTrail.request.disable_model(@record.class)
|
||||||
if method
|
if method
|
||||||
|
|
|
@ -260,12 +260,14 @@ RSpec.describe Widget, type: :model do
|
||||||
}.by(+1)
|
}.by(+1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not create a version using without_versioning" do
|
context "request is disabled" do
|
||||||
count = widget.versions.count
|
it "does not create a version" do
|
||||||
widget.paper_trail.without_versioning do
|
count = widget.versions.count
|
||||||
widget.touch
|
PaperTrail.request(enabled: false) do
|
||||||
|
widget.touch
|
||||||
|
end
|
||||||
|
expect(count).to eq(count)
|
||||||
end
|
end
|
||||||
expect(count).to eq(count)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -342,8 +342,10 @@ RSpec.describe(::PaperTrail, versioning: true) do
|
||||||
|
|
||||||
context "when destroyed \"without versioning\"" do
|
context "when destroyed \"without versioning\"" do
|
||||||
it "leave paper trail off after call" do
|
it "leave paper trail off after call" do
|
||||||
|
allow(::ActiveSupport::Deprecation).to receive(:warn)
|
||||||
@widget.paper_trail.without_versioning(:destroy)
|
@widget.paper_trail.without_versioning(:destroy)
|
||||||
expect(::PaperTrail.request.enabled_for_model?(Widget)).to eq(false)
|
expect(::PaperTrail.request.enabled_for_model?(Widget)).to eq(false)
|
||||||
|
expect(::ActiveSupport::Deprecation).to have_received(:warn).once
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -361,28 +363,24 @@ RSpec.describe(::PaperTrail, versioning: true) do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when updated \"without versioning\"" do
|
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.paper_trail.without_versioning do
|
||||||
@widget.update_attributes(name: "Ford")
|
@widget.update_attributes(name: "Ford")
|
||||||
end
|
end
|
||||||
@widget.paper_trail.without_versioning do |w|
|
@widget.paper_trail.without_versioning do |w|
|
||||||
w.update_attributes(name: "Nixon")
|
w.update_attributes(name: "Nixon")
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
it "not create new version" do
|
|
||||||
expect(@widget.versions.length).to(eq(@count))
|
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(PaperTrail.request.enabled_for_model?(Widget)).to eq(true)
|
||||||
|
expect(::ActiveSupport::Deprecation).to have_received(:warn).twice
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "given a symbol, specifying a method name" do
|
context "given a symbol, specifying a method name" do
|
||||||
it "does not create a new version" do
|
it "does not create a new version" do
|
||||||
allow(::ActiveSupport::Deprecation).to receive(:warn)
|
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(::ActiveSupport::Deprecation).to have_received(:warn).once
|
||||||
expect(@widget.versions.length).to(eq(@count))
|
expect(@widget.versions.length).to(eq(@count))
|
||||||
expect(::PaperTrail.request.enabled_for_model?(Widget)).to eq(true)
|
expect(::PaperTrail.request.enabled_for_model?(Widget)).to eq(true)
|
||||||
|
|
|
@ -25,6 +25,7 @@ RSpec.describe PaperTrail do
|
||||||
|
|
||||||
describe "#without_versioning" do
|
describe "#without_versioning" do
|
||||||
it "is thread-safe" do
|
it "is thread-safe" do
|
||||||
|
allow(::ActiveSupport::Deprecation).to receive(:warn)
|
||||||
enabled = nil
|
enabled = nil
|
||||||
t1 = Thread.new do
|
t1 = Thread.new do
|
||||||
Widget.new.paper_trail.without_versioning do
|
Widget.new.paper_trail.without_versioning do
|
||||||
|
@ -43,6 +44,7 @@ RSpec.describe PaperTrail do
|
||||||
expect(t1.value).to eq(false)
|
expect(t1.value).to eq(false)
|
||||||
expect(t2.value).to eq(true) # see? unaffected by t1
|
expect(t2.value).to eq(true) # see? unaffected by t1
|
||||||
expect(described_class.request.enabled_for_model?(Widget)).to eq(true)
|
expect(described_class.request.enabled_for_model?(Widget)).to eq(true)
|
||||||
|
expect(::ActiveSupport::Deprecation).to have_received(:warn).once
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue