spec/models/vanilla_spec.rb in draftsman-0.6.0 vs spec/models/vanilla_spec.rb in draftsman-0.7.0
- old
+ new
@@ -3,10 +3,24 @@
# A Vanilla has a simple call to `has_drafts` without any options specified.
describe Vanilla do
let(:vanilla) { Vanilla.new(name: 'Bob') }
it { should be_draftable }
+ describe '#object_attrs_for_draft_record' do
+ it 'contains column name' do
+ expect(vanilla.object_attrs_for_draft_record).to include 'name'
+ end
+
+ it 'contains column updated_at' do
+ expect(vanilla.object_attrs_for_draft_record).to include 'updated_at'
+ end
+
+ it 'contains column created_at' do
+ expect(vanilla.object_attrs_for_draft_record).to include 'created_at'
+ end
+ end
+
describe '#save_draft' do
context 'on create' do
it 'is persisted' do
vanilla.save_draft
expect(vanilla).to be_persisted
@@ -34,10 +48,22 @@
it 'saves the `name`' do
vanilla.save_draft
expect(vanilla.name).to eql 'Bob'
end
+
+ it 'sets `updated_at`' do
+ time = Time.now
+ vanilla.save_draft
+ expect(vanilla.updated_at).to be > time
+ end
+
+ it 'sets `created_at`' do
+ time = Time.now
+ vanilla.save_draft
+ expect(vanilla.created_at).to be > time
+ end
end
context 'on update' do
context 'with stashed drafted changes' do
context 'without existing draft' do
@@ -82,10 +108,16 @@
end
it 'creates a new draft' do
expect { vanilla.save_draft }.to change(Draftsman::Draft, :count).by(1)
end
+
+ it 'has the original `updated_at`' do
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to eq vanilla.created_at
+ end
end
describe 'changing back to initial state' do
before do
vanilla.published_at = Time.now
@@ -121,10 +153,18 @@
end
it 'destroys the draft' do
expect { vanilla.save_draft }.to change(Draftsman::Draft.where(id: vanilla.draft_id), :count).by(-1)
end
+
+ it 'has the original `updated_at`' do
+ if activerecord_save_touch_option?
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to eq vanilla.created_at
+ end
+ end
end
context 'with existing `create` draft' do
before { vanilla.save_draft }
@@ -174,10 +214,17 @@
it 'has a `create` draft' do
vanilla.save_draft
vanilla.reload
expect(vanilla.draft.create?).to eql true
end
+
+ it 'has a new `updated_at`' do
+ time = vanilla.updated_at
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to be > time
+ end
end # with changes
context 'with no changes' do
it 'is persisted' do
vanilla.save_draft
@@ -215,10 +262,16 @@
end
it "doesn't change the number of drafts" do
expect { vanilla.save_draft }.to_not change(Draftsman::Draft.where(id: vanilla.draft_id), :count)
end
+
+ it 'has the original `updated_at`' do
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to eq vanilla.created_at
+ end
end
end # with no changes
context 'with existing `update` draft' do
before do
@@ -273,10 +326,16 @@
it 'has an `update` draft' do
vanilla.save_draft
expect(vanilla.draft.update?).to eql true
end
+
+ it 'has the original `updated_at`' do
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to eq vanilla.created_at
+ end
end # with changes
context 'with no changes' do
it 'is persisted' do
vanilla.save_draft
@@ -320,10 +379,16 @@
it "does not update the draft's `name`" do
vanilla.save_draft
vanilla.reload
expect(vanilla.draft.reify.name).to eql 'Sam'
end
+
+ it 'has the original `updated_at`' do
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to eq vanilla.created_at
+ end
end # with no changes
end # with existing `update` draft
end # with stashed drafted changes
context 'without stashed drafted changes' do
@@ -372,10 +437,17 @@
end
it 'creates a new draft' do
expect { vanilla.save_draft }.to change(Draftsman::Draft, :count).by(1)
end
+
+ it 'has a new `updated_at`' do
+ time = vanilla.updated_at
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to be > time
+ end
end
describe 'changing back to initial state' do
before do
vanilla.published_at = Time.now
@@ -411,10 +483,17 @@
end
it 'destroys the draft' do
expect { vanilla.save_draft }.to change(Draftsman::Draft.where(id: vanilla.draft_id), :count).by(-1)
end
+
+ it 'has a new `updated_at`' do
+ time = vanilla.updated_at
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to be > time
+ end
end
context 'with existing `create` draft' do
before { vanilla.save_draft }
@@ -457,10 +536,17 @@
it 'has a `create` draft' do
vanilla.save_draft
expect(vanilla.draft.create?).to eql true
end
+
+ it 'has a new `updated_at`' do
+ time = vanilla.updated_at
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to be > time
+ end
end
context 'with no changes' do
it 'is persisted' do
vanilla.save_draft
@@ -492,10 +578,15 @@
end
it "doesn't change the number of drafts" do
expect { vanilla.save_draft }.to_not change(Draftsman::Draft.where(id: vanilla.draft_id), :count)
end
+
+ it 'has the original `updated_at`' do
+ vanilla.save_draft
+ expect(vanilla.reload.updated_at).to eq vanilla.created_at
+ end
end
end
context 'with existing `update` draft' do
before do
@@ -551,10 +642,17 @@
it 'has an `update` draft' do
vanilla.save_draft
vanilla.reload
expect(vanilla.draft.update?).to eql true
end
+
+ it 'has a new `updated_at`' do
+ time = vanilla.updated_at
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to be > time
+ end
end # with changes
context 'with no changes' do
it 'is persisted' do
vanilla.save_draft
@@ -591,9 +689,16 @@
end
it "does not update the draft's `name`" do
vanilla.save_draft
expect(vanilla.draft.reify.name).to eql 'Sam'
+ end
+
+ it 'does not update `updated_at`' do
+ time = vanilla.updated_at
+ vanilla.save_draft
+ vanilla.reload
+ expect(vanilla.updated_at).to eq time
end
end # with no changes
end # with existing `update` draft
end # without stashed drafted changes
end # on update