draftsman 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe Enumable, :type => :model do
3
+ RSpec.describe Enumable, type: :model do
4
4
  let(:enumable) { Enumable.new(status: :active) }
5
+
5
6
  describe '#draft' do
6
7
  describe '#reify' do
7
- before { enumable.draft_creation }
8
+ before { enumable.save_draft }
8
9
 
9
10
  it 'does not raise an exception' do
10
11
  expect { enumable.draft.reify }.to_not raise_error
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe OverriddenDraft, type: :model do
4
+ context 'overridden via `draft_class_name` setting' do
5
+ let(:vanilla) { Vanilla.new(name: 'Bob') }
6
+ let!(:class_name_was) { Draftsman.draft_class_name }
7
+ after { Draftsman.draft_class_name = class_name_was }
8
+
9
+ before do
10
+ Draftsman.draft_class_name = 'OverriddenDraft'
11
+
12
+ class Vanilla <ActiveRecord::Base
13
+ has_drafts
14
+ end
15
+ end
16
+
17
+ describe '#draft.class.name' do
18
+ it 'has an `OverriddenDraft` record as its draft' do
19
+ vanilla.save_draft
20
+ expect(vanilla.draft.class.name).to eql 'OverriddenDraft'
21
+ end
22
+ end
23
+ end
24
+
25
+ context 'with default `draft_class_name` setting' do
26
+ let(:vanilla) { Vanilla.new(name: 'Bob') }
27
+
28
+ before do
29
+ class Vanilla < ActiveRecord::Base
30
+ has_drafts
31
+ end
32
+ end
33
+
34
+ describe '#draft.class.name' do
35
+ it 'has the default `Draftsman::Draft` record as its draft' do
36
+ vanilla.save_draft
37
+ expect(vanilla.reload.draft.class.name).to eql 'Draftsman::Draft'
38
+ end
39
+ end
40
+ end
41
+ end
@@ -7,8 +7,8 @@ describe Parent do
7
7
  describe 'publish!' do
8
8
  context 'parent `create` draft with child `create` draft' do
9
9
  before do
10
- parent.draft_creation
11
- child.draft_creation
10
+ parent.save_draft
11
+ child.save_draft
12
12
  end
13
13
 
14
14
  subject { parent.draft.publish! }
@@ -68,8 +68,8 @@ describe Parent do
68
68
  describe 'revert!' do
69
69
  context 'parent `create` draft with child `create` draft' do
70
70
  before do
71
- parent.draft_creation
72
- child.draft_creation
71
+ parent.save_draft
72
+ child.save_draft
73
73
  end
74
74
 
75
75
  subject { parent.draft.revert! }
@@ -140,8 +140,8 @@ describe Parent do
140
140
  describe 'draft_publication_dependencies' do
141
141
  context 'parent `create` draft with child `create` draft' do
142
142
  before do
143
- parent.draft_creation
144
- child.draft_creation
143
+ parent.save_draft
144
+ child.save_draft
145
145
  end
146
146
 
147
147
  subject { parent.draft }
@@ -155,8 +155,8 @@ describe Parent do
155
155
  before do
156
156
  parent.save!
157
157
  parent.name = 'Selma'
158
- parent.draft_update
159
- child.draft_creation
158
+ parent.save_draft
159
+ child.save_draft
160
160
  end
161
161
 
162
162
  subject { parent.draft }
@@ -188,8 +188,8 @@ describe Parent do
188
188
  describe 'draft_reversion_dependencies' do
189
189
  context 'parent `create` draft with child `create` draft' do
190
190
  before do
191
- parent.draft_creation
192
- child.draft_creation
191
+ parent.save_draft
192
+ child.save_draft
193
193
  end
194
194
 
195
195
  subject { parent.draft }
@@ -1,58 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Skipper do
3
+ RSpec.describe Skipper, type: :model do
4
4
  let(:skipper) { Skipper.new :name => 'Bob', :skip_me => 'Skipped 1' }
5
5
 
6
6
  it 'is draftable' do
7
7
  expect(subject.class.draftable?).to eql true
8
8
  end
9
9
 
10
- describe 'draft_creation' do
11
- subject do
12
- skipper.draft_creation
13
- skipper.reload
14
- end
15
-
16
- it 'is persisted' do
17
- expect(subject).to be_persisted
18
- end
19
-
20
- it 'is a draft' do
21
- expect(subject.draft?).to eql true
22
- end
23
-
24
- it 'has a `draft_id`' do
25
- expect(subject.draft_id).to be_present
26
- end
27
-
28
- it 'has a draft' do
29
- expect(subject.draft).to be_present
30
- end
31
-
32
- it 'has a `create` draft' do
33
- expect(subject.draft.create?).to eql true
34
- end
35
-
36
- it 'has a `name`' do
37
- expect(subject.name).to eql 'Bob'
38
- end
39
-
40
- it 'has a value for `skip_me`' do
41
- expect(subject.skip_me).to eql 'Skipped 1'
42
- end
43
- end
44
-
45
- describe 'draft_update' do
46
- subject do
47
- skipper.draft_update
48
- skipper.reload
49
- end
50
-
51
- context 'without existing draft' do
52
- before do
53
- skipper.save!
54
- skipper.name = 'Sam'
55
- skipper.skip_me = 'Skipped 2'
10
+ describe '#save_draft' do
11
+ context 'on create' do
12
+ subject do
13
+ skipper.save_draft
14
+ skipper.reload
56
15
  end
57
16
 
58
17
  it 'is persisted' do
@@ -67,68 +26,32 @@ describe Skipper do
67
26
  expect(subject.draft_id).to be_present
68
27
  end
69
28
 
70
- it 'has a `draft`' do
29
+ it 'has a draft' do
71
30
  expect(subject.draft).to be_present
72
31
  end
73
32
 
74
- it 'identifies as an `update` draft' do
75
- expect(subject.draft.update?).to eql true
33
+ it 'has a `create` draft' do
34
+ expect(subject.draft.create?).to eql true
76
35
  end
77
36
 
78
- it 'has the original name' do
37
+ it 'has a `name`' do
79
38
  expect(subject.name).to eql 'Bob'
80
39
  end
81
40
 
82
- it 'has the updated skipped attribute' do
83
- expect(subject.skip_me).to eql 'Skipped 2'
84
- end
85
-
86
- it 'creates a new draft' do
87
- expect { subject }.to change(Draftsman::Draft, :count).by(1)
41
+ it 'has a value for `skip_me`' do
42
+ expect(subject.skip_me).to eql 'Skipped 1'
88
43
  end
89
44
  end
90
45
 
91
- describe 'changing back to initial state' do
92
- before do
93
- skipper.published_at = Time.now
94
- skipper.save!
95
- skipper.name = 'Sam'
96
- skipper.draft_update
46
+ context 'on update' do
47
+ subject do
48
+ skipper.save_draft
97
49
  skipper.reload
98
- skipper.name = 'Bob'
99
- skipper.skip_me = 'Skipped 2'
100
50
  end
101
51
 
102
- it 'is no longer a draft' do
103
- expect(subject.draft?).to eql false
104
- end
105
-
106
- it 'no longer has a `draft_id`' do
107
- expect(subject.draft_id).to be_nil
108
- end
109
-
110
- it 'no longer has a `draft`' do
111
- expect(subject.draft).to be_nil
112
- end
113
-
114
- it 'has its original `name`' do
115
- expect(subject.name).to eql 'Bob'
116
- end
117
-
118
- it "retains the updated skipped attribute's value" do
119
- expect(subject.skip_me).to eql 'Skipped 2'
120
- end
121
-
122
- it 'destroys the draft' do
123
- expect { subject }.to change(Draftsman::Draft.where(:id => skipper.draft_id), :count).by(-1)
124
- end
125
- end
126
-
127
- context 'with existing `create` draft' do
128
- before { skipper.draft_creation }
129
-
130
- context 'with changes' do
52
+ context 'without existing draft' do
131
53
  before do
54
+ skipper.save!
132
55
  skipper.name = 'Sam'
133
56
  skipper.skip_me = 'Skipped 2'
134
57
  end
@@ -149,196 +72,275 @@ describe Skipper do
149
72
  expect(subject.draft).to be_present
150
73
  end
151
74
 
152
- it 'has a `create` draft' do
153
- expect(subject.draft.create?).to eql true
75
+ it 'identifies as an `update` draft' do
76
+ expect(subject.draft.update?).to eql true
154
77
  end
155
78
 
156
- it 'has the updated `name`' do
157
- expect(subject.name).to eql 'Sam'
79
+ it 'has the original name' do
80
+ expect(subject.name).to eql 'Bob'
158
81
  end
159
82
 
160
- it "retains the updated skipped attribute's value" do
83
+ it 'has the updated skipped attribute' do
161
84
  expect(subject.skip_me).to eql 'Skipped 2'
162
85
  end
163
86
 
164
- it 'updates the existing draft' do
165
- expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
166
- end
167
-
168
- it "updates the draft's `name`" do
169
- expect(subject.draft.reify.name).to eql 'Sam'
87
+ it 'creates a new draft' do
88
+ expect { subject }.to change(Draftsman::Draft, :count).by(1)
170
89
  end
171
90
  end
172
91
 
173
- context 'with no changes' do
174
- it 'is persisted' do
175
- expect(subject).to be_persisted
176
- end
177
-
178
- it 'is a draft' do
179
- expect(subject.draft?).to eql true
92
+ describe 'changing back to initial state' do
93
+ before do
94
+ skipper.published_at = Time.now
95
+ skipper.save!
96
+ skipper.name = 'Sam'
97
+ skipper.save_draft
98
+ skipper.reload
99
+ skipper.name = 'Bob'
100
+ skipper.skip_me = 'Skipped 2'
180
101
  end
181
102
 
182
- it 'has a `draft_id`' do
183
- expect(subject.draft_id).to be_present
103
+ it 'is no longer a draft' do
104
+ expect(subject.draft?).to eql false
184
105
  end
185
106
 
186
- it 'has a `draft`' do
187
- expect(subject.draft).to be_present
107
+ it 'no longer has a `draft_id`' do
108
+ expect(subject.draft_id).to be_nil
188
109
  end
189
110
 
190
- it 'has a `create` draft' do
191
- expect(subject.draft.create?).to eql true
111
+ it 'no longer has a `draft`' do
112
+ expect(subject.draft).to be_nil
192
113
  end
193
114
 
194
- it 'has the original `name`' do
115
+ it 'has its original `name`' do
195
116
  expect(subject.name).to eql 'Bob'
196
117
  end
197
118
 
198
- it "has the original skipped attribute's value" do
199
- expect(subject.skip_me).to eql 'Skipped 1'
119
+ it "retains the updated skipped attribute's value" do
120
+ expect(subject.skip_me).to eql 'Skipped 2'
200
121
  end
201
122
 
202
- it "doesn't change the number of drafts" do
203
- expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
123
+ it 'destroys the draft' do
124
+ expect { subject }.to change(Draftsman::Draft.where(:id => skipper.draft_id), :count).by(-1)
204
125
  end
205
126
  end
206
- end
207
127
 
208
- context 'with existing `update` draft' do
209
- before do
210
- skipper.save!
211
- skipper.name = 'Sam'
212
- skipper.skip_me = 'Skipped 2'
213
- skipper.draft_update
214
- skipper.reload
215
- skipper.attributes = skipper.draft.reify.attributes
216
- end
128
+ context 'with existing `create` draft' do
129
+ before { skipper.save_draft }
217
130
 
218
- context 'with changes to drafted attribute' do
219
- before { skipper.name = 'Steve' }
131
+ context 'with changes' do
132
+ before do
133
+ skipper.name = 'Sam'
134
+ skipper.skip_me = 'Skipped 2'
135
+ end
220
136
 
221
- it 'is persisted' do
222
- expect(subject).to be_persisted
223
- end
137
+ it 'is persisted' do
138
+ expect(subject).to be_persisted
139
+ end
224
140
 
225
- it 'is a draft' do
226
- expect(subject.draft?).to eql true
227
- end
141
+ it 'is a draft' do
142
+ expect(subject.draft?).to eql true
143
+ end
228
144
 
229
- it 'has a `draft_id`' do
230
- expect(subject.draft_id).to be_present
231
- end
145
+ it 'has a `draft_id`' do
146
+ expect(subject.draft_id).to be_present
147
+ end
232
148
 
233
- it 'has a `draft`' do
234
- expect(subject.draft).to be_present
235
- end
149
+ it 'has a `draft`' do
150
+ expect(subject.draft).to be_present
151
+ end
236
152
 
237
- it 'has an `update` draft' do
238
- expect(subject.draft.update?).to eql true
239
- end
153
+ it 'has a `create` draft' do
154
+ expect(subject.draft.create?).to eql true
155
+ end
240
156
 
241
- it 'has the original `name`' do
242
- expect(subject.name).to eql 'Bob'
243
- end
157
+ it 'has the updated `name`' do
158
+ expect(subject.name).to eql 'Sam'
159
+ end
244
160
 
245
- it "has the updated skipped attribute's value" do
246
- expect(subject.skip_me).to eql 'Skipped 2'
247
- end
161
+ it "retains the updated skipped attribute's value" do
162
+ expect(subject.skip_me).to eql 'Skipped 2'
163
+ end
164
+
165
+ it 'updates the existing draft' do
166
+ expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
167
+ end
248
168
 
249
- it 'updates the existing draft' do
250
- expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
169
+ it "updates the draft's `name`" do
170
+ expect(subject.draft.reify.name).to eql 'Sam'
171
+ end
251
172
  end
252
173
 
253
- it "updates the draft's `name`" do
254
- expect(subject.draft.reify.name).to eql 'Steve'
174
+ context 'with no changes' do
175
+ it 'is persisted' do
176
+ expect(subject).to be_persisted
177
+ end
178
+
179
+ it 'is a draft' do
180
+ expect(subject.draft?).to eql true
181
+ end
182
+
183
+ it 'has a `draft_id`' do
184
+ expect(subject.draft_id).to be_present
185
+ end
186
+
187
+ it 'has a `draft`' do
188
+ expect(subject.draft).to be_present
189
+ end
190
+
191
+ it 'has a `create` draft' do
192
+ expect(subject.draft.create?).to eql true
193
+ end
194
+
195
+ it 'has the original `name`' do
196
+ expect(subject.name).to eql 'Bob'
197
+ end
198
+
199
+ it "has the original skipped attribute's value" do
200
+ expect(subject.skip_me).to eql 'Skipped 1'
201
+ end
202
+
203
+ it "doesn't change the number of drafts" do
204
+ expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
205
+ end
255
206
  end
256
207
  end
257
208
 
258
- context 'with changes to skipped attributes' do
209
+ context 'with existing `update` draft' do
259
210
  before do
260
- skipper.skip_me = 'Skip and save'
261
- skipper.draft_update
211
+ skipper.save!
212
+ skipper.name = 'Sam'
213
+ skipper.skip_me = 'Skipped 2'
214
+ skipper.save_draft
262
215
  skipper.reload
263
216
  skipper.attributes = skipper.draft.reify.attributes
264
217
  end
265
218
 
266
- it 'is persisted' do
267
- expect(subject).to be_persisted
268
- end
219
+ context 'with changes to drafted attribute' do
220
+ before { skipper.name = 'Steve' }
269
221
 
270
- it 'is a draft' do
271
- expect(subject.draft?).to eql true
272
- end
222
+ it 'is persisted' do
223
+ expect(subject).to be_persisted
224
+ end
273
225
 
274
- it 'has a `draft_id`' do
275
- expect(subject.draft_id).to be_present
276
- end
226
+ it 'is a draft' do
227
+ expect(subject.draft?).to eql true
228
+ end
277
229
 
278
- it 'has a `draft`' do
279
- expect(subject.draft).to be_present
280
- end
230
+ it 'has a `draft_id`' do
231
+ expect(subject.draft_id).to be_present
232
+ end
281
233
 
282
- it 'has an `update` draft' do
283
- expect(subject.draft.update?).to eql true
284
- end
234
+ it 'has a `draft`' do
235
+ expect(subject.draft).to be_present
236
+ end
285
237
 
286
- it 'has the original `name`' do
287
- expect(subject.name).to eql 'Bob'
288
- end
238
+ it 'has an `update` draft' do
239
+ expect(subject.draft.update?).to eql true
240
+ end
289
241
 
290
- it "updates skipped attribute's value" do
291
- expect(subject.skip_me).to eql 'Skip and save'
292
- end
242
+ it 'has the original `name`' do
243
+ expect(subject.name).to eql 'Bob'
244
+ end
293
245
 
294
- it 'updates the existing draft' do
295
- expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
296
- end
246
+ it "has the updated skipped attribute's value" do
247
+ expect(subject.skip_me).to eql 'Skipped 2'
248
+ end
297
249
 
298
- it "keeps the draft's `name`" do
299
- expect(subject.draft.reify.name).to eql 'Sam'
300
- end
250
+ it 'updates the existing draft' do
251
+ expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
252
+ end
301
253
 
302
- it 'updates skipped attribute on draft' do
303
- expect(subject.draft.reify.skip_me).to eql 'Skip and save'
254
+ it "updates the draft's `name`" do
255
+ expect(subject.draft.reify.name).to eql 'Steve'
256
+ end
304
257
  end
305
- end
306
258
 
307
- context 'with no changes' do
308
- it 'is persisted' do
309
- expect(subject).to be_persisted
310
- end
259
+ context 'with changes to skipped attributes' do
260
+ before do
261
+ skipper.skip_me = 'Skip and save'
262
+ skipper.save_draft
263
+ skipper.reload
264
+ skipper.attributes = skipper.draft.reify.attributes
265
+ end
311
266
 
312
- it 'is a draft' do
313
- expect(subject.draft?).to eql true
314
- end
267
+ it 'is persisted' do
268
+ expect(subject).to be_persisted
269
+ end
315
270
 
316
- it 'has a `draft_id`' do
317
- expect(subject.draft_id).to be_present
318
- end
271
+ it 'is a draft' do
272
+ expect(subject.draft?).to eql true
273
+ end
319
274
 
320
- it 'has a `draft`' do
321
- expect(subject.draft).to be_present
322
- end
275
+ it 'has a `draft_id`' do
276
+ expect(subject.draft_id).to be_present
277
+ end
323
278
 
324
- it 'has an `update` draft' do
325
- expect(subject.draft.update?).to eql true
326
- end
279
+ it 'has a `draft`' do
280
+ expect(subject.draft).to be_present
281
+ end
327
282
 
328
- it 'has the original `name`' do
329
- expect(subject.name).to eql 'Bob'
330
- end
283
+ it 'has an `update` draft' do
284
+ expect(subject.draft.update?).to eql true
285
+ end
331
286
 
332
- it "has the updated skipped attributes' value" do
333
- expect(subject.skip_me).to eql 'Skipped 2'
334
- end
287
+ it 'has the original `name`' do
288
+ expect(subject.name).to eql 'Bob'
289
+ end
290
+
291
+ it "updates skipped attribute's value" do
292
+ expect(subject.skip_me).to eql 'Skip and save'
293
+ end
294
+
295
+ it 'updates the existing draft' do
296
+ expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
297
+ end
298
+
299
+ it "keeps the draft's `name`" do
300
+ expect(subject.draft.reify.name).to eql 'Sam'
301
+ end
335
302
 
336
- it "doesn't change the number of drafts" do
337
- expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
303
+ it 'updates skipped attribute on draft' do
304
+ expect(subject.draft.reify.skip_me).to eql 'Skip and save'
305
+ end
338
306
  end
339
307
 
340
- it "does not update the draft's `name`" do
341
- expect(subject.draft.reify.name).to eql 'Sam'
308
+ context 'with no changes' do
309
+ it 'is persisted' do
310
+ expect(subject).to be_persisted
311
+ end
312
+
313
+ it 'is a draft' do
314
+ expect(subject.draft?).to eql true
315
+ end
316
+
317
+ it 'has a `draft_id`' do
318
+ expect(subject.draft_id).to be_present
319
+ end
320
+
321
+ it 'has a `draft`' do
322
+ expect(subject.draft).to be_present
323
+ end
324
+
325
+ it 'has an `update` draft' do
326
+ expect(subject.draft.update?).to eql true
327
+ end
328
+
329
+ it 'has the original `name`' do
330
+ expect(subject.name).to eql 'Bob'
331
+ end
332
+
333
+ it "has the updated skipped attributes' value" do
334
+ expect(subject.skip_me).to eql 'Skipped 2'
335
+ end
336
+
337
+ it "doesn't change the number of drafts" do
338
+ expect { subject }.to_not change(Draftsman::Draft.where(:id => skipper.draft_id), :count)
339
+ end
340
+
341
+ it "does not update the draft's `name`" do
342
+ expect(subject.draft.reify.name).to eql 'Sam'
343
+ end
342
344
  end
343
345
  end
344
346
  end