2014-10-09 15:04:17 -04:00
require 'rails_helper'
2013-08-27 16:34:16 -04:00
2014-10-09 15:04:17 -04:00
describe Widget , :type = > :model do
2013-10-29 12:07:54 -04:00
describe '`be_versioned` matcher' do
2014-10-31 10:40:43 -04:00
it { is_expected . to be_versioned }
2013-10-29 12:07:54 -04:00
end
2014-04-16 12:44:21 -04:00
let ( :widget ) { Widget . create! :name = > 'Bob' , :an_integer = > 1 }
2013-10-29 12:07:54 -04:00
2014-07-09 08:47:15 -04:00
describe '`have_a_version_with` matcher' , :versioning = > true do
before do
widget . update_attributes! ( :name = > 'Leonard' , :an_integer = > 1 )
widget . update_attributes! ( :name = > 'Tom' )
widget . update_attributes! ( :name = > 'Bob' )
end
it " is possible to do assertions on versions " do
2014-10-09 15:04:17 -04:00
expect ( widget ) . to have_a_version_with :name = > 'Leonard' , :an_integer = > 1
expect ( widget ) . to have_a_version_with :an_integer = > 1
expect ( widget ) . to have_a_version_with :name = > 'Tom'
2014-07-09 08:47:15 -04:00
end
end
2014-02-21 18:20:40 -05:00
describe " `versioning` option " do
2013-10-29 12:07:54 -04:00
context :enabled , :versioning = > true do
it 'should enable versioning for models wrapped within a block' do
2014-10-09 15:04:17 -04:00
expect ( widget . versions . size ) . to eq ( 1 )
2013-10-29 12:07:54 -04:00
end
end
2013-08-27 16:34:16 -04:00
2013-10-29 12:07:54 -04:00
context '`disabled` (default)' do
it 'should not enable versioning for models wrapped within a block not marked to used versioning' do
2014-10-09 15:04:17 -04:00
expect ( widget . versions . size ) . to eq ( 0 )
2013-10-29 12:07:54 -04:00
end
2013-08-27 16:34:16 -04:00
end
end
2014-02-12 18:27:10 -05:00
2014-04-16 12:17:33 -04:00
describe " Callbacks " , :versioning = > true do
2014-04-16 16:33:10 -04:00
describe :before_save do
context ':on => :update' do
before { widget . update_attributes! ( :name = > 'Foobar' ) }
subject { widget . versions . last . reify }
it " should reset the value for the timestamp attrs for update so that value gets updated properly " do
2014-04-22 19:33:53 -04:00
expect { subject . save! } . to change ( subject , :updated_at )
2014-04-16 16:33:10 -04:00
end
end
end
2014-07-30 16:17:02 -04:00
describe :after_create do
let ( :widget ) { Widget . create! ( :name = > 'Foobar' , :created_at = > Time . now - 1 . week ) }
it " corresponding version should use the widget's `created_at` " do
2014-10-09 15:04:17 -04:00
expect ( widget . versions . last . created_at . to_i ) . to eq ( widget . created_at . to_i )
2014-07-30 16:17:02 -04:00
end
end
2014-04-16 12:44:21 -04:00
describe :after_update do
2014-05-30 17:38:23 -04:00
before { widget . update_attributes! ( :name = > 'Foobar' , :updated_at = > Time . now + 1 . week ) }
2014-04-16 12:44:21 -04:00
subject { widget . versions . last . reify }
2014-10-09 15:04:17 -04:00
it { expect ( subject ) . not_to be_live }
2014-04-16 12:44:21 -04:00
it " should clear the `versions_association_name` virtual attribute " do
subject . save!
2014-10-09 15:04:17 -04:00
expect ( subject ) . to be_live
2014-04-16 12:44:21 -04:00
end
2014-05-30 17:38:23 -04:00
2014-07-30 16:17:02 -04:00
it " corresponding version should use the widget updated_at " do
2014-10-09 15:04:17 -04:00
expect ( widget . versions . last . created_at . to_i ) . to eq ( widget . updated_at . to_i )
2014-05-30 17:38:23 -04:00
end
2014-04-16 12:44:21 -04:00
end
2014-04-16 12:17:33 -04:00
describe :after_destroy do
it " should create a version for that event " do
expect { widget . destroy } . to change ( widget . versions , :count ) . by ( 1 )
end
it " should assign the version into the `versions_association_name` " do
2014-10-09 15:04:17 -04:00
expect ( widget . version ) . to be_nil
2014-04-16 12:17:33 -04:00
widget . destroy
2014-10-09 15:04:17 -04:00
expect ( widget . version ) . not_to be_nil
expect ( widget . version ) . to eq ( widget . versions . last )
2014-04-16 12:17:33 -04:00
end
end
2014-11-10 19:12:50 -05:00
describe :after_rollback do
let ( :rolled_back_name ) { 'Big Moo' }
before do
widget . transaction do
widget . update_attributes! ( name : rolled_back_name )
widget . update_attributes! ( name : described_class :: EXCLUDED_NAME )
end
rescue ActiveRecord :: RecordInvalid
widget . name = nil
widget . save
end
it 'does not create an event for changes that did not happen' do
widget . versions . map ( & :changeset ) . each do | changeset |
expect ( changeset . fetch ( 'name' , [ ] ) ) . to_not include ( rolled_back_name )
end
end
end
2014-04-16 12:17:33 -04:00
end
2014-06-03 16:26:53 -04:00
describe " Association " , :versioning = > true do
describe " sort order " do
it " should sort by the timestamp order from the `VersionConcern` " do
2014-10-09 15:04:17 -04:00
expect ( widget . versions . to_sql ) . to eq (
widget . versions . reorder ( PaperTrail :: Version . timestamp_sort_order ) . to_sql )
2014-06-03 16:26:53 -04:00
end
end
end
2014-02-21 18:20:40 -05:00
describe " Methods " do
describe " Instance " , :versioning = > true do
2014-10-09 15:04:17 -04:00
describe '#originator' do
it { is_expected . to respond_to ( :originator ) }
2014-06-17 10:58:53 -04:00
describe " return value " do
let ( :orig_name ) { Faker :: Name . name }
let ( :new_name ) { Faker :: Name . name }
before { PaperTrail . whodunnit = orig_name }
context " accessed from live model instance " do
2014-10-09 15:04:17 -04:00
specify { expect ( widget ) . to be_live }
2014-06-17 10:58:53 -04:00
it " should return the originator for the model at a given state " do
2014-10-09 15:04:17 -04:00
expect ( widget . originator ) . to eq ( orig_name )
2014-06-17 10:58:53 -04:00
widget . whodunnit ( new_name ) { | w | w . update_attributes ( :name = > 'Elizabeth' ) }
2014-10-09 15:04:17 -04:00
expect ( widget . originator ) . to eq ( new_name )
2014-06-17 10:58:53 -04:00
end
end
context " accessed from a reified model instance " do
before do
widget . update_attributes ( :name = > 'Andy' )
PaperTrail . whodunnit = new_name
widget . update_attributes ( :name = > 'Elizabeth' )
end
2014-07-25 15:49:39 -04:00
context " default behavior (no `options[:dup]` option passed in) " do
2014-07-18 04:47:21 -04:00
let ( :reified_widget ) { widget . versions [ 1 ] . reify }
it " should return the appropriate originator " do
2014-10-09 15:04:17 -04:00
expect ( reified_widget . originator ) . to eq ( orig_name )
2014-07-18 04:47:21 -04:00
end
it " should not create a new model instance " do
2014-10-09 15:04:17 -04:00
expect ( reified_widget ) . not_to be_new_record
2014-07-18 04:47:21 -04:00
end
end
2014-07-25 15:49:39 -04:00
context " creating a new instance (`options[:dup] == true`) " do
let ( :reified_widget ) { widget . versions [ 1 ] . reify ( :dup = > true ) }
2014-07-18 04:47:21 -04:00
it " should return the appropriate originator " do
2014-10-09 15:04:17 -04:00
expect ( reified_widget . originator ) . to eq ( orig_name )
2014-07-18 04:47:21 -04:00
end
it " should not create a new model instance " do
2014-10-09 15:04:17 -04:00
expect ( reified_widget ) . to be_new_record
2014-07-18 04:47:21 -04:00
end
2014-06-17 10:58:53 -04:00
end
end
end
end
2014-10-09 15:04:17 -04:00
describe '#version_at' do
it { is_expected . to respond_to ( :version_at ) }
2014-06-18 11:01:38 -04:00
context " Timestamp argument is AFTER object has been destroyed " do
before do
widget . update_attribute ( :name , 'foobar' )
widget . destroy
end
it " should return `nil` " do
2014-10-09 15:04:17 -04:00
expect ( widget . version_at ( Time . now ) ) . to be_nil
2014-06-18 11:01:38 -04:00
end
end
end
2014-10-09 15:04:17 -04:00
describe '#whodunnit' do
it { is_expected . to respond_to ( :whodunnit ) }
2014-03-11 17:55:44 -04:00
context " no block given " do
it " should raise an error " do
expect { widget . whodunnit ( 'Ben' ) } . to raise_error ( ArgumentError , 'expected to receive a block' )
end
end
context " block given " do
let ( :orig_name ) { Faker :: Name . name }
let ( :new_name ) { Faker :: Name . name }
before do
PaperTrail . whodunnit = orig_name
2014-10-09 15:04:17 -04:00
expect ( widget . versions . last . whodunnit ) . to eq ( orig_name ) # persist `widget`
2014-03-11 17:55:44 -04:00
end
it " should modify value of `PaperTrail.whodunnit` while executing the block " do
widget . whodunnit ( new_name ) do
2014-10-09 15:04:17 -04:00
expect ( PaperTrail . whodunnit ) . to eq ( new_name )
2014-03-11 17:55:44 -04:00
widget . update_attributes ( :name = > 'Elizabeth' )
end
2014-10-09 15:04:17 -04:00
expect ( widget . versions . last . whodunnit ) . to eq ( new_name )
2014-03-11 17:55:44 -04:00
end
it " should revert the value of `PaperTrail.whodunnit` to it's previous value after executing the block " do
widget . whodunnit ( new_name ) { | w | w . update_attributes ( :name = > 'Elizabeth' ) }
2014-10-09 15:04:17 -04:00
expect ( PaperTrail . whodunnit ) . to eq ( orig_name )
2014-03-11 17:55:44 -04:00
end
2014-03-12 10:09:59 -04:00
context " error within block " do
it " should ensure that the whodunnit value still reverts to it's previous value " do
expect { widget . whodunnit ( new_name ) { raise } } . to raise_error
2014-10-09 15:04:17 -04:00
expect ( PaperTrail . whodunnit ) . to eq ( orig_name )
2014-03-12 10:09:59 -04:00
end
end
2014-03-11 17:55:44 -04:00
end
end
2014-10-09 15:04:17 -04:00
describe '#touch_with_version' do
it { is_expected . to respond_to ( :touch_with_version ) }
2014-02-21 18:20:40 -05:00
it " should generate a version " do
count = widget . versions . size
widget . touch_with_version
2014-10-09 15:04:17 -04:00
expect ( widget . versions . size ) . to eq ( count + 1 )
2014-02-21 18:20:40 -05:00
end
it " should increment the `:updated_at` timestamp " do
time_was = widget . updated_at
widget . touch_with_version
2014-10-09 15:04:17 -04:00
expect ( widget . updated_at ) . to be > time_was
2014-02-21 18:20:40 -05:00
end
2014-02-12 18:27:10 -05:00
end
end
2014-02-21 18:20:40 -05:00
describe " Class " do
subject { Widget }
2014-02-12 18:27:10 -05:00
2014-10-31 10:40:43 -04:00
describe " # paper_trail_enabled_for_model? " do
it { is_expected . to respond_to ( :paper_trail_enabled_for_model? ) }
it { expect ( subject . paper_trail_enabled_for_model? ) . to be true }
end
2014-10-09 15:04:17 -04:00
describe '#paper_trail_off!' do
it { is_expected . to respond_to ( :paper_trail_off! ) }
2014-02-12 18:27:10 -05:00
2014-02-21 18:20:40 -05:00
it 'should set the `paper_trail_enabled_for_model?` to `false`' do
2014-10-09 15:04:17 -04:00
expect ( subject . paper_trail_enabled_for_model? ) . to be true
2014-02-21 18:20:40 -05:00
subject . paper_trail_off!
2014-10-09 15:04:17 -04:00
expect ( subject . paper_trail_enabled_for_model? ) . to be false
2014-02-21 18:20:40 -05:00
end
2014-02-12 18:27:10 -05:00
end
2014-10-09 15:04:17 -04:00
describe '#paper_trail_off' do
it { is_expected . to respond_to ( :paper_trail_off ) }
2014-02-12 18:27:10 -05:00
2014-02-21 18:20:40 -05:00
it 'should set the invoke `paper_trail_off!`' do
2014-10-09 15:04:17 -04:00
is_expected . to receive ( :warn )
is_expected . to receive ( :paper_trail_off! )
2014-02-21 18:20:40 -05:00
subject . paper_trail_off
end
2014-02-12 18:27:10 -05:00
2014-02-21 18:20:40 -05:00
it 'should display a deprecation warning' do
2014-10-09 15:04:17 -04:00
is_expected . to receive ( :warn ) . with ( " DEPRECATED: use `paper_trail_on!` instead of `paper_trail_on`. Support for `paper_trail_on` will be removed in PaperTrail 3.1 " )
2014-02-21 18:20:40 -05:00
subject . paper_trail_on
end
2014-02-12 18:27:10 -05:00
end
2014-10-09 15:04:17 -04:00
describe '#paper_trail_on!' do
2014-02-21 18:20:40 -05:00
before { subject . paper_trail_off! }
2014-02-12 18:27:10 -05:00
2014-10-09 15:04:17 -04:00
it { is_expected . to respond_to ( :paper_trail_on! ) }
2014-02-12 18:27:10 -05:00
2014-02-21 18:20:40 -05:00
it 'should set the `paper_trail_enabled_for_model?` to `true`' do
2014-10-09 15:04:17 -04:00
expect ( subject . paper_trail_enabled_for_model? ) . to be false
2014-02-21 18:20:40 -05:00
subject . paper_trail_on!
2014-10-09 15:04:17 -04:00
expect ( subject . paper_trail_enabled_for_model? ) . to be true
2014-02-21 18:20:40 -05:00
end
2014-02-12 18:27:10 -05:00
end
2014-10-09 15:04:17 -04:00
describe '#paper_trail_on' do
2014-02-21 18:20:40 -05:00
before { subject . paper_trail_off! }
2014-10-09 15:04:17 -04:00
it { is_expected . to respond_to ( :paper_trail_on ) }
2014-02-21 18:20:40 -05:00
it 'should set the invoke `paper_trail_on!`' do
2014-10-09 15:04:17 -04:00
is_expected . to receive ( :warn )
is_expected . to receive ( :paper_trail_on! )
2014-02-21 18:20:40 -05:00
subject . paper_trail_on
end
it 'should display a deprecation warning' do
2014-10-09 15:04:17 -04:00
is_expected . to receive ( :warn ) . with ( " DEPRECATED: use `paper_trail_on!` instead of `paper_trail_on`. Support for `paper_trail_on` will be removed in PaperTrail 3.1 " )
2014-02-21 18:20:40 -05:00
subject . paper_trail_on
end
2014-02-12 18:27:10 -05:00
end
end
end
2013-08-27 16:34:16 -04:00
end