Raise error re: where_object_changes, YAML

Using where_object_changes to read YAML from a text column will now raise
error, was deprecated in 8.1.0.
This commit is contained in:
Jared Beck 2017-12-10 21:58:18 -05:00
parent e5193360f0
commit 325e368c4c
7 changed files with 42 additions and 100 deletions

View File

@ -14,8 +14,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
a warning. The default (false) will remain the same.
- Removed `warn_about_not_setting_whodunnit` controller method. Please remove
callbacks like `skip_after_action :warn_about_not_setting_whodunnit`.
- [#997](https://github.com/airblade/paper_trail/pull/997) -
where_object_changes reading YAML from a text column
- Using where_object_changes to read YAML from a text column will now raise
error, was deprecated in 8.1.0.
### Added

View File

@ -34,7 +34,7 @@ module PaperTrail
def where_object_changes_condition(*)
raise <<-STR.squish.freeze
where_object_changes no longer supports reading json from a text
where_object_changes no longer supports reading JSON from a text
column. The old implementation was inaccurate, returning more records
than you wanted. This feature was deprecated in 7.1.0 and removed in
8.0.0. The json and jsonb datatypes are still supported. See the

View File

@ -4,13 +4,6 @@ module PaperTrail
module Serializers
# The default serializer for, e.g. `versions.object`.
module YAML
E_WHERE_OBJ_CHANGES = <<-STR.squish.freeze
where_object_changes has a known issue. When reading YAML from a text
column, it may return more records than expected. Instead of a warning,
this method may raise an error in the future. Please join the discussion
at https://github.com/airblade/paper_trail/pull/997
STR
extend self # makes all instance methods become module methods as well
def load(string)
@ -29,13 +22,14 @@ module PaperTrail
# Returns a SQL LIKE condition to be used to match the given field and
# value in the serialized `object_changes`.
def where_object_changes_condition(arel_field, field, value)
::ActiveSupport::Deprecation.warn(E_WHERE_OBJ_CHANGES)
# Need to check first (before) and secondary (after) fields
m1 = "%\n#{field}:\n- #{value}\n%"
m2 = "%\n#{field}:\n-%\n- #{value}\n%"
arel_field.matches(m1).or(arel_field.matches(m2))
def where_object_changes_condition(*)
raise <<-STR.squish.freeze
where_object_changes no longer supports reading YAML from a text
column. The old implementation was inaccurate, returning more records
than you wanted. This feature was deprecated in 8.1.0 and removed in
9.0.0. The json and jsonb datatypes are still supported. See
discussion at https://github.com/airblade/paper_trail/pull/997
STR
end
end
end

View File

@ -9,17 +9,6 @@ RSpec.describe Document, type: :model, versioning: true do
end
end
describe "have_a_version_with_changes matcher" do
it "works with custom versions association" do
document = Document.create!(name: "Foo")
document.update_attributes!(name: "Bar")
allow(ActiveSupport::Deprecation).to receive(:warn)
expect(document).to have_a_version_with_changes(name: "Bar")
expect(ActiveSupport::Deprecation).to have_received(:warn).
once.with(/^where_object_changes/)
end
end
describe "#paper_trail.next_version" do
it "returns the expected document" do
doc = Document.create

18
spec/models/fruit_spec.rb Normal file
View File

@ -0,0 +1,18 @@
require "spec_helper"
if ENV["DB"] == "postgres" || JsonVersion.table_exists?
RSpec.describe Fruit, type: :model, versioning: true do
describe "have_a_version_with_changes matcher" do
it "works with Fruit because Fruit uses JsonVersion" do
# As of PT 9.0.0, with_version_changes only supports json(b) columns,
# so that's why were testing the have_a_version_with_changes matcher
# here.
banana = Fruit.create!(color: "Red", name: "Banana")
banana.update_attributes!(color: "Yellow")
expect(banana).to have_a_version_with_changes(color: "Yellow")
expect(banana).not_to have_a_version_with_changes(color: "Pink")
expect(banana).not_to have_a_version_with_changes(color: "Yellow", name: "Kiwi")
end
end
end
end

View File

@ -152,7 +152,7 @@ module PaperTrail
}.to raise_error(ArgumentError)
end
context "`serializer == YAML`" do
context "YAML serializer" do
it "locates versions according to their `object` contents" do
expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML
widget.update_attributes!(name: name, an_integer: int)
@ -191,12 +191,6 @@ module PaperTrail
end
describe "#where_object_changes", versioning: true do
before do
widget.update_attributes!(name: name, an_integer: 0)
widget.update_attributes!(name: "foobar", an_integer: 100)
widget.update_attributes!(name: FFaker::Name.last_name, an_integer: int)
end
it "requires its argument to be a Hash" do
expect {
PaperTrail::Version.where_object_changes(:foo)
@ -206,50 +200,13 @@ module PaperTrail
}.to raise_error(ArgumentError)
end
context "YAML serializer" do
before do
unless column_datatype_override
allow(ActiveSupport::Deprecation).to receive(:warn)
end
end
it "locates versions according to their `object_changes` contents" do
expect(
widget.versions.where_object_changes(name: name)
).to eq(widget.versions[0..1])
expect(
widget.versions.where_object_changes(an_integer: 100)
).to eq(widget.versions[1..2])
expect(
widget.versions.where_object_changes(an_integer: int)
).to eq([widget.versions.last])
unless column_datatype_override
expect(ActiveSupport::Deprecation).to have_received(:warn).
exactly(3).times.with(/^where_object_changes/)
end
end
it "handles queries for multiple attributes" do
expect(
widget.versions.where_object_changes(an_integer: 100, name: "foobar")
).to eq(widget.versions[1..2])
unless column_datatype_override
expect(ActiveSupport::Deprecation).to have_received(:warn).
at_least(:once).with(/^where_object_changes/)
end
end
end
# Only test the JSON serializer against where_object_changes
# for json and jsonb columns, not for text columns, which are
# no longer supported.
# Only test json and jsonb columns. where_object_changes no longer
# supports text columns.
if column_datatype_override
context "JSON serializer" do
before do
PaperTrail.serializer = PaperTrail::Serializers::JSON
end
it "locates versions according to their `object_changes` contents" do
it "locates versions according to their object_changes contents" do
widget.update_attributes!(name: name, an_integer: 0)
widget.update_attributes!(name: "foobar", an_integer: 100)
widget.update_attributes!(name: FFaker::Name.last_name, an_integer: int)
expect(
widget.versions.where_object_changes(name: name)
).to eq(widget.versions[0..1])
@ -259,13 +216,15 @@ module PaperTrail
expect(
widget.versions.where_object_changes(an_integer: int)
).to eq([widget.versions.last])
end
it "handles queries for multiple attributes" do
expect(
widget.versions.where_object_changes(an_integer: 100, name: "foobar")
).to eq(widget.versions[1..2])
end
else
it "raises error" do
expect {
widget.versions.where_object_changes(name: "foo").to_a
}.to(raise_error(/no longer supports reading YAML from a text column/))
end
end
end

View File

@ -21,24 +21,6 @@ RSpec.describe Widget, type: :model do
end
end
describe "`have_a_version_with_changes` matcher", versioning: true do
before do
widget.update_attributes!(name: "Leonard", an_integer: 2)
widget.update_attributes!(name: "Tom")
widget.update_attributes!(name: "Bob")
end
it "is possible to do assertions on version changes" do
allow(ActiveSupport::Deprecation).to receive(:warn)
expect(widget).to have_a_version_with_changes name: "Leonard", an_integer: 2
expect(widget).to have_a_version_with_changes an_integer: 2
expect(widget).to have_a_version_with_changes name: "Tom"
expect(widget).to have_a_version_with_changes name: "Bob"
expect(ActiveSupport::Deprecation).to have_received(:warn).
exactly(5).times.with(/^where_object_changes/)
end
end
describe "versioning option" do
context "enabled", versioning: true do
it "enables versioning" do