Add tests and refactor where_object_changes with JSONB
This commit is contained in:
parent
e52c53605e
commit
6d15205488
|
@ -110,7 +110,8 @@ module PaperTrail
|
|||
def where_object_changes(args = {})
|
||||
raise ArgumentError, 'expected to receive a Hash' unless args.is_a?(Hash)
|
||||
|
||||
if columns_hash['object'].type == :jsonb
|
||||
if columns_hash['object_changes'].type == :jsonb
|
||||
args.each { |field, value| args[field] = [value] }
|
||||
where_conditions = "object_changes @> '#{args.to_json}'::jsonb"
|
||||
else
|
||||
arel_field = arel_table[:object_changes]
|
||||
|
|
|
@ -66,15 +66,17 @@ describe PaperTrail::Version, :type => :model do
|
|||
|
||||
describe "Class" do
|
||||
column_overrides = [false]
|
||||
column_overrides.concat(['jsonb']) if ENV['DB'] == 'postgres'
|
||||
column_overrides.concat(%w[jsonb]) if ENV['DB'] == 'postgres'
|
||||
|
||||
column_overrides.each do |override|
|
||||
context "with a #{override || 'text'} column" do
|
||||
before do
|
||||
if override
|
||||
ActiveRecord::Base.connection.execute("SAVEPOINT pgtest;")
|
||||
ActiveRecord::Base.connection.execute("ALTER TABLE versions DROP COLUMN object;")
|
||||
ActiveRecord::Base.connection.execute("ALTER TABLE versions ADD COLUMN object #{override};")
|
||||
%w[object object_changes].each do |column|
|
||||
ActiveRecord::Base.connection.execute("ALTER TABLE versions DROP COLUMN #{column};")
|
||||
ActiveRecord::Base.connection.execute("ALTER TABLE versions ADD COLUMN #{column} #{override};")
|
||||
end
|
||||
PaperTrail::Version.reset_column_information
|
||||
end
|
||||
end
|
||||
|
@ -128,60 +130,60 @@ describe PaperTrail::Version, :type => :model do
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#where_object_changes' do
|
||||
it { expect(PaperTrail::Version).to respond_to(:where_object_changes) }
|
||||
describe '#where_object_changes' do
|
||||
it { expect(PaperTrail::Version).to respond_to(:where_object_changes) }
|
||||
|
||||
context "invalid arguments" do
|
||||
it "should raise an error" do
|
||||
expect { PaperTrail::Version.where_object_changes(:foo) }.to raise_error(ArgumentError)
|
||||
expect { PaperTrail::Version.where_object_changes([]) }.to raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
context "valid arguments", :versioning => true do
|
||||
let(:widget) { Widget.new }
|
||||
let(:name) { Faker::Name.first_name }
|
||||
let(:int) { rand(5) + 2 }
|
||||
|
||||
before do
|
||||
widget.update_attributes!(:name => name, :an_integer => 0)
|
||||
widget.update_attributes!(:name => 'foobar', :an_integer => 77)
|
||||
widget.update_attributes!(:name => Faker::Name.last_name, :an_integer => int)
|
||||
end
|
||||
|
||||
context "`serializer == YAML`" do
|
||||
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML }
|
||||
|
||||
it "should be able to locate 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 => 77)).to eq(widget.versions[1..2])
|
||||
expect(widget.versions.where_object_changes(:an_integer => int)).to eq([widget.versions.last])
|
||||
context "invalid arguments" do
|
||||
it "should raise an error" do
|
||||
expect { PaperTrail::Version.where_object_changes(:foo) }.to raise_error(ArgumentError)
|
||||
expect { PaperTrail::Version.where_object_changes([]) }.to raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
it "should be able to handle queries for multiple attributes" do
|
||||
expect(widget.versions.where_object_changes(:an_integer => 77, :name => 'foobar')).to eq(widget.versions[1..2])
|
||||
context "valid arguments", :versioning => true do
|
||||
let(:widget) { Widget.new }
|
||||
let(:name) { Faker::Name.first_name }
|
||||
let(:int) { rand(5) + 2 }
|
||||
|
||||
before do
|
||||
widget.update_attributes!(:name => name, :an_integer => 0)
|
||||
widget.update_attributes!(:name => 'foobar', :an_integer => 77)
|
||||
widget.update_attributes!(:name => Faker::Name.last_name, :an_integer => int)
|
||||
end
|
||||
|
||||
context "`serializer == YAML`" do
|
||||
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML }
|
||||
|
||||
it "should be able to locate 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 => 77)).to eq(widget.versions[1..2])
|
||||
expect(widget.versions.where_object_changes(:an_integer => int)).to eq([widget.versions.last])
|
||||
end
|
||||
|
||||
it "should be able to handle queries for multiple attributes" do
|
||||
expect(widget.versions.where_object_changes(:an_integer => 77, :name => 'foobar')).to eq(widget.versions[1..2])
|
||||
end
|
||||
end
|
||||
|
||||
context "`serializer == JSON`" do
|
||||
before(:all) { PaperTrail.serializer = PaperTrail::Serializers::JSON }
|
||||
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::JSON }
|
||||
|
||||
it "should be able to locate 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 => 77)).to eq(widget.versions[1..2])
|
||||
expect(widget.versions.where_object_changes(:an_integer => int)).to eq([widget.versions.last])
|
||||
end
|
||||
|
||||
it "should be able to handle queries for multiple attributes" do
|
||||
expect(widget.versions.where_object_changes(:an_integer => 77, :name => 'foobar')).to eq(widget.versions[1..2])
|
||||
end
|
||||
|
||||
after(:all) { PaperTrail.serializer = PaperTrail::Serializers::YAML }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "`serializer == JSON`" do
|
||||
before(:all) { PaperTrail.serializer = PaperTrail::Serializers::JSON }
|
||||
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::JSON }
|
||||
|
||||
it "should be able to locate 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 => 77)).to eq(widget.versions[1..2])
|
||||
expect(widget.versions.where_object_changes(:an_integer => int)).to eq([widget.versions.last])
|
||||
end
|
||||
|
||||
it "should be able to handle queries for multiple attributes" do
|
||||
expect(widget.versions.where_object_changes(:an_integer => 77, :name => 'foobar')).to eq(widget.versions[1..2])
|
||||
end
|
||||
|
||||
after(:all) { PaperTrail.serializer = PaperTrail::Serializers::YAML }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue