2014-10-09 15:04:17 -04:00
|
|
|
require 'rails_helper'
|
2013-10-11 10:42:12 -04:00
|
|
|
|
2014-10-09 15:04:17 -04:00
|
|
|
describe PaperTrail::Version, :type => :model do
|
2013-10-29 11:57:41 -04:00
|
|
|
it "should include the `VersionConcern` module to get base functionality" do
|
2014-10-09 15:04:17 -04:00
|
|
|
expect(PaperTrail::Version).to include(PaperTrail::VersionConcern)
|
2013-10-22 13:12:08 -04:00
|
|
|
end
|
|
|
|
|
2013-10-11 10:42:12 -04:00
|
|
|
describe "Attributes" do
|
2014-10-09 15:04:17 -04:00
|
|
|
it { is_expected.to have_db_column(:item_type).of_type(:string) }
|
|
|
|
it { is_expected.to have_db_column(:item_id).of_type(:integer) }
|
|
|
|
it { is_expected.to have_db_column(:event).of_type(:string) }
|
|
|
|
it { is_expected.to have_db_column(:whodunnit).of_type(:string) }
|
|
|
|
it { is_expected.to have_db_column(:object).of_type(:text) }
|
|
|
|
it { is_expected.to have_db_column(:created_at).of_type(:datetime) }
|
2014-10-21 15:05:16 -04:00
|
|
|
|
|
|
|
describe "object_changes column", :versioning => true do
|
|
|
|
let(:widget) { Widget.create!(:name => 'Dashboard') }
|
|
|
|
let(:value) { widget.versions.last.object_changes }
|
|
|
|
|
|
|
|
context "serializer is YAML" do
|
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML }
|
|
|
|
|
|
|
|
it "should store out as a plain hash" do
|
|
|
|
expect(value =~ /ActiveSupport::HashWithIndifferentAccess/).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "serializer is JSON" do
|
|
|
|
before(:all) { PaperTrail.serializer = PaperTrail::Serializers::JSON }
|
|
|
|
|
|
|
|
it "should store out as a plain hash" do
|
|
|
|
expect(value =~ /ActiveSupport::HashWithIndifferentAccess/).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) { PaperTrail.serializer = PaperTrail::Serializers::YAML }
|
|
|
|
end
|
|
|
|
end
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Indexes" do
|
2014-10-09 15:04:17 -04:00
|
|
|
it { is_expected.to have_db_index([:item_type, :item_id]) }
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Methods" do
|
|
|
|
describe "Instance" do
|
|
|
|
subject { PaperTrail::Version.new(attributes) rescue PaperTrail::Version.new }
|
|
|
|
|
2014-10-09 15:04:17 -04:00
|
|
|
describe '#terminator' do
|
|
|
|
it { is_expected.to respond_to(:terminator) }
|
2013-10-11 10:42:12 -04:00
|
|
|
|
|
|
|
let(:attributes) { {:whodunnit => Faker::Name.first_name} }
|
|
|
|
|
|
|
|
it "is an alias for the `whodunnit` attribute" do
|
2014-10-09 15:04:17 -04:00
|
|
|
expect(subject.whodunnit).to eq(attributes[:whodunnit])
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-09 15:04:17 -04:00
|
|
|
describe '#version_author' do
|
|
|
|
it { is_expected.to respond_to(:version_author) }
|
2013-10-11 10:42:12 -04:00
|
|
|
|
|
|
|
it "should be an alias for the `terminator` method" do
|
2014-10-09 15:04:17 -04:00
|
|
|
expect(subject.method(:version_author)).to eq(subject.method(:terminator))
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-06-26 17:29:09 -04:00
|
|
|
|
|
|
|
describe "Class" do
|
2014-10-09 15:04:17 -04:00
|
|
|
describe '#where_object' do
|
|
|
|
it { expect(PaperTrail::Version).to respond_to(:where_object) }
|
2014-06-26 17:29:09 -04:00
|
|
|
|
|
|
|
context "invalid arguments" do
|
|
|
|
it "should raise an error" do
|
|
|
|
expect { PaperTrail::Version.where_object(:foo) }.to raise_error(ArgumentError)
|
|
|
|
expect { PaperTrail::Version.where_object([]) }.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(10) + 1 }
|
|
|
|
|
|
|
|
before do
|
|
|
|
widget.update_attributes!(:name => name, :an_integer => int)
|
|
|
|
widget.update_attributes!(:name => 'foobar', :an_integer => 100)
|
|
|
|
widget.update_attributes!(:name => Faker::Name.last_name, :an_integer => 15)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "`serializer == YAML`" do
|
2014-10-09 15:04:17 -04:00
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML }
|
2014-06-26 17:29:09 -04:00
|
|
|
|
|
|
|
it "should be able to locate versions according to their `object` contents" do
|
2014-10-09 15:04:17 -04:00
|
|
|
expect(PaperTrail::Version.where_object(:name => name)).to eq([widget.versions[1]])
|
|
|
|
expect(PaperTrail::Version.where_object(:an_integer => 100)).to eq([widget.versions[2]])
|
2014-06-26 17:29:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "`serializer == JSON`" do
|
2014-10-21 15:05:16 -04:00
|
|
|
before(:all) { PaperTrail.serializer = PaperTrail::Serializers::JSON }
|
2014-10-09 15:04:17 -04:00
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::JSON }
|
2014-06-26 17:29:09 -04:00
|
|
|
|
|
|
|
it "should be able to locate versions according to their `object` contents" do
|
2014-10-09 15:04:17 -04:00
|
|
|
expect(PaperTrail::Version.where_object(:name => name)).to eq([widget.versions[1]])
|
|
|
|
expect(PaperTrail::Version.where_object(:an_integer => 100)).to eq([widget.versions[2]])
|
2014-06-26 17:29:09 -04:00
|
|
|
end
|
2014-10-21 15:05:16 -04:00
|
|
|
|
|
|
|
after(:all) { PaperTrail.serializer = PaperTrail::Serializers::YAML }
|
2014-06-26 17:29:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
end
|