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.
- checksums.yaml +4 -4
- data/.travis.yml +12 -0
- data/CHANGELOG.md +34 -0
- data/README.md +244 -181
- data/lib/draftsman/config.rb +4 -1
- data/lib/draftsman/draft.rb +117 -80
- data/lib/draftsman/frameworks/rspec.rb +1 -1
- data/lib/draftsman/model.rb +253 -235
- data/lib/draftsman/version.rb +1 -1
- data/lib/draftsman.rb +26 -5
- data/lib/generators/draftsman/templates/config/initializers/draftsman.rb +16 -7
- data/spec/draftsman_spec.rb +7 -9
- data/spec/dummy/app/controllers/application_controller.rb +2 -2
- data/spec/dummy/app/models/overridden_draft.rb +7 -0
- data/spec/dummy/app/models/talkative.rb +21 -43
- data/spec/dummy/db/schema.rb +1 -0
- data/spec/models/child_spec.rb +17 -17
- data/spec/models/draft_spec.rb +893 -305
- data/spec/models/enumable_spec.rb +3 -2
- data/spec/models/overridden_draft_spec.rb +41 -0
- data/spec/models/parent_spec.rb +10 -10
- data/spec/models/skipper_spec.rb +221 -219
- data/spec/models/talkative_spec.rb +107 -108
- data/spec/models/trashable_spec.rb +6 -10
- data/spec/models/vanilla_spec.rb +570 -229
- data/spec/models/whitelister_spec.rb +489 -348
- metadata +7 -10
- data/spec/dummy/db/migrate/20110208155312_set_up_test_tables.rb +0 -95
- data/spec/dummy/db/migrate/20150404203627_add_talkatives_table_to_tests.rb +0 -18
- data/spec/dummy/db/migrate/20150408234937_add_only_children.rb +0 -16
- data/spec/dummy/db/migrate/20160328184419_create_enumables.rb +0 -9
data/lib/draftsman.rb
CHANGED
@@ -78,24 +78,45 @@ module Draftsman
|
|
78
78
|
Draftsman.config.serializer = value
|
79
79
|
end
|
80
80
|
|
81
|
+
# Sets whether or not `#save_draft` should stash drafted changes into the
|
82
|
+
# associated draft record or persist them to the main item.
|
83
|
+
def self.stash_drafted_changes=(value)
|
84
|
+
Draftsman.config.stash_drafted_changes = value
|
85
|
+
end
|
86
|
+
|
87
|
+
# Returns setting for whether or not `#save_draft` should stash drafted
|
88
|
+
# changes into the associated draft record.
|
89
|
+
def self.stash_drafted_changes?
|
90
|
+
Draftsman.config.stash_drafted_changes?
|
91
|
+
end
|
92
|
+
|
81
93
|
# Returns who is reponsible for any changes that occur.
|
82
94
|
def self.whodunnit
|
83
95
|
draftsman_store[:whodunnit]
|
84
96
|
end
|
85
97
|
|
86
|
-
# Sets who is responsible for any changes that occur.
|
87
|
-
#
|
88
|
-
#
|
89
|
-
# automatically to the `current_user`.
|
98
|
+
# Sets who is responsible for any changes that occur. You would normally use
|
99
|
+
# this in a migration or on the console when working with models directly. In
|
100
|
+
# a controller, it is set automatically to the `current_user`.
|
90
101
|
def self.whodunnit=(value)
|
91
102
|
draftsman_store[:whodunnit] = value
|
92
103
|
end
|
93
104
|
|
105
|
+
# Returns the field which records whodunnit data.
|
106
|
+
def self.whodunnit_field
|
107
|
+
Draftsman.config.whodunnit_field
|
108
|
+
end
|
109
|
+
|
110
|
+
# Sets global attribute name for `whodunnit` data.
|
111
|
+
def self.whodunnit_field=(field_name)
|
112
|
+
Draftsman.config.whodunnit_field = field_name
|
113
|
+
end
|
114
|
+
|
94
115
|
private
|
95
116
|
|
96
117
|
# Thread-safe hash to hold Draftman's data. Initializing with needed default values.
|
97
118
|
def self.draftsman_store
|
98
|
-
Thread.current[:draft] ||= { :
|
119
|
+
Thread.current[:draft] ||= { draft_class_name: 'Draftsman::Draft' }
|
99
120
|
end
|
100
121
|
|
101
122
|
# Returns Draftman's configuration object.
|
@@ -1,15 +1,24 @@
|
|
1
|
-
# Override global `draft` class. For example, perhaps you want your own class at
|
2
|
-
# extra attributes, validations, associations,
|
3
|
-
# `Draftsman::Draft`.
|
1
|
+
# Override global `draft` class. For example, perhaps you want your own class at
|
2
|
+
# `app/models/draft.rb` that adds extra attributes, validations, associations,
|
3
|
+
# methods, etc. Be sure that this new model class extends `Draftsman::Draft`.
|
4
4
|
# Draftsman.draft_class_name = 'Draftsman::Draft'
|
5
5
|
|
6
|
-
# Serializer for `object`, `object_changes`, and `previous_draft` columns. To
|
7
|
-
# `Draftsman::Serializers::Json`. You could
|
6
|
+
# Serializer for `object`, `object_changes`, and `previous_draft` columns. To
|
7
|
+
# use the JSON serializer, change to `Draftsman::Serializers::Json`. You could
|
8
|
+
# implement your own serializer if you really wanted to. See files in
|
8
9
|
# `lib/draftsman/serializers`.
|
9
10
|
#
|
10
|
-
# Note: this option is not needed if you're using the PostgreSQL JSON data type
|
11
|
-
# `object_changes`, and `previous_draft` columns.
|
11
|
+
# Note: this option is not needed if you're using the PostgreSQL JSON data type
|
12
|
+
# for the `object`, `object_changes`, and `previous_draft` columns.
|
12
13
|
# Draftsman.serializer = Draftsman::Serializers::Json
|
13
14
|
|
14
15
|
# Field which records when a draft was created.
|
15
16
|
# Draftsman.timestamp_field = :created_at
|
17
|
+
|
18
|
+
# Field which records who last recorded the draft.
|
19
|
+
# Draftsman.whodunnit_field = :whodunnit
|
20
|
+
|
21
|
+
# Whether or not to stash draft data in the `Draftsman::Draft` record. If set to
|
22
|
+
# `false`, all changes will be persisted to the main record and will not be
|
23
|
+
# persisted to the draft record's `object` column.
|
24
|
+
# Draftsman.stash_drafted_changes = true
|
data/spec/draftsman_spec.rb
CHANGED
@@ -1,25 +1,23 @@
|
|
1
1
|
require 'spec_helper.rb'
|
2
2
|
|
3
3
|
describe ::Draftsman do
|
4
|
-
subject { ::Draftsman }
|
5
|
-
|
6
4
|
it 'passes our sanity test' do
|
7
|
-
expect(
|
5
|
+
expect(::Draftsman).to be_a Module
|
8
6
|
end
|
9
7
|
|
10
|
-
describe
|
11
|
-
before(:all) { ::Draftsman.whodunnit =
|
8
|
+
describe '.whodunnit' do
|
9
|
+
before(:all) { ::Draftsman.whodunnit = :foobar }
|
12
10
|
|
13
11
|
it 'clears out `whodunnit` before each test' do
|
14
|
-
expect(
|
12
|
+
expect(::Draftsman.whodunnit).to be_nil
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
|
-
describe
|
19
|
-
before(:all) { ::Draftsman.controller_info = { foo:
|
16
|
+
describe '.controller_info' do
|
17
|
+
before(:all) { ::Draftsman.controller_info = { foo: :bar } }
|
20
18
|
|
21
19
|
it 'clears out `controller_info` before each test' do
|
22
|
-
expect(
|
20
|
+
expect(::Draftsman.controller_info).to eql Hash.new
|
23
21
|
end
|
24
22
|
end
|
25
23
|
end
|
@@ -3,14 +3,14 @@ class ApplicationController < ActionController::Base
|
|
3
3
|
before_action :set_draftsman_whodunnit
|
4
4
|
|
5
5
|
def create
|
6
|
-
Trashable.new(name: 'Bob').
|
6
|
+
Trashable.new(name: 'Bob').save_draft
|
7
7
|
head :no_content
|
8
8
|
end
|
9
9
|
|
10
10
|
def update
|
11
11
|
trashable = Trashable.last
|
12
12
|
trashable.name = 'Sam'
|
13
|
-
trashable.
|
13
|
+
trashable.save_draft
|
14
14
|
head :no_content
|
15
15
|
end
|
16
16
|
|
@@ -2,65 +2,43 @@ class Talkative < ActiveRecord::Base
|
|
2
2
|
has_drafts
|
3
3
|
|
4
4
|
# draft_creation callbacks
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
before_save_draft :do_this_before_save
|
6
|
+
around_save_draft :do_this_around_save
|
7
|
+
after_save_draft :do_this_after_save
|
8
8
|
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# # draft_destruction callbacks
|
15
|
-
before_draft_destruction :do_this_before_draft_destruction
|
16
|
-
around_draft_destruction :do_this_around_draft_destruction
|
17
|
-
after_draft_destruction :do_this_after_draft_destruction
|
9
|
+
# draft_destruction callbacks
|
10
|
+
before_draft_destruction :do_this_before_destruction
|
11
|
+
around_draft_destruction :do_this_around_destruction
|
12
|
+
after_draft_destruction :do_this_after_destruction
|
18
13
|
|
19
14
|
private
|
20
15
|
|
21
|
-
def
|
22
|
-
self.before_comment =
|
16
|
+
def do_this_before_save
|
17
|
+
self.before_comment = 'I changed before save'
|
23
18
|
end
|
24
19
|
|
25
|
-
def
|
26
|
-
self.around_early_comment =
|
20
|
+
def do_this_around_save
|
21
|
+
self.around_early_comment = 'I changed around save (before yield)'
|
27
22
|
yield
|
28
|
-
self.around_late_comment =
|
29
|
-
end
|
30
|
-
|
31
|
-
def do_this_after_draft_creation
|
32
|
-
self.after_comment = "I changed after creation"
|
23
|
+
self.around_late_comment = 'I changed around save (after yield)'
|
33
24
|
end
|
34
25
|
|
35
|
-
|
36
|
-
|
37
|
-
def do_this_before_draft_update
|
38
|
-
self.before_comment = "I changed before update"
|
26
|
+
def do_this_after_save
|
27
|
+
self.after_comment = 'I changed after save'
|
39
28
|
end
|
40
29
|
|
41
|
-
def do_this_around_draft_update
|
42
|
-
self.around_early_comment = "I changed around update (before yield)"
|
43
|
-
yield
|
44
|
-
self.around_late_comment = "I changed around update (after yield)"
|
45
|
-
end
|
46
|
-
|
47
|
-
def do_this_after_draft_update
|
48
|
-
self.after_comment = "I changed after update"
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
30
|
|
53
|
-
def
|
54
|
-
self.before_comment =
|
31
|
+
def do_this_before_destruction
|
32
|
+
self.before_comment = 'I changed before destroy'
|
55
33
|
end
|
56
34
|
|
57
|
-
def
|
58
|
-
self.around_early_comment =
|
35
|
+
def do_this_around_destruction
|
36
|
+
self.around_early_comment = 'I changed around destroy (before yield)'
|
59
37
|
yield
|
60
|
-
self.around_late_comment =
|
38
|
+
self.around_late_comment = 'I changed around destroy (after yield)'
|
61
39
|
end
|
62
40
|
|
63
|
-
def
|
64
|
-
self.after_comment =
|
41
|
+
def do_this_after_destruction
|
42
|
+
self.after_comment = 'I changed after destroy'
|
65
43
|
end
|
66
44
|
end
|
data/spec/dummy/db/schema.rb
CHANGED
data/spec/models/child_spec.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Child, :
|
4
|
-
let(:parent) { Parent.new(:
|
5
|
-
let(:child) { Child.new(:
|
3
|
+
RSpec.describe Child, type: :model do
|
4
|
+
let(:parent) { Parent.new(name: 'Marge') }
|
5
|
+
let(:child) { Child.new(name: 'Lisa', parent: parent) }
|
6
6
|
|
7
7
|
describe 'publish!' do
|
8
8
|
context 'parent `create` draft with child `create` draft' do
|
9
9
|
before do
|
10
|
-
parent.
|
11
|
-
child.
|
10
|
+
parent.save_draft
|
11
|
+
child.save_draft
|
12
12
|
end
|
13
13
|
|
14
14
|
subject { child.draft.publish! }
|
@@ -77,8 +77,8 @@ describe Child, :type => :model do
|
|
77
77
|
describe 'revert!' do
|
78
78
|
context 'parent `create` draft with child `create` draft' do
|
79
79
|
before do
|
80
|
-
parent.
|
81
|
-
child.
|
80
|
+
parent.save_draft
|
81
|
+
child.save_draft
|
82
82
|
end
|
83
83
|
|
84
84
|
subject { child.draft.revert! }
|
@@ -111,7 +111,7 @@ describe Child, :type => :model do
|
|
111
111
|
subject do
|
112
112
|
child.draft.revert!
|
113
113
|
parent.reload
|
114
|
-
child.reload
|
114
|
+
child.reload
|
115
115
|
end
|
116
116
|
|
117
117
|
it 'does not persist the child' do
|
@@ -153,8 +153,8 @@ describe Child, :type => :model do
|
|
153
153
|
describe 'draft_publication_dependencies' do
|
154
154
|
context 'parent `create` draft with child `create` draft' do
|
155
155
|
before do
|
156
|
-
parent.
|
157
|
-
child.
|
156
|
+
parent.save_draft
|
157
|
+
child.save_draft
|
158
158
|
end
|
159
159
|
|
160
160
|
subject { child.draft }
|
@@ -170,10 +170,10 @@ describe Child, :type => :model do
|
|
170
170
|
|
171
171
|
context 'parent `create` draft with child `update` draft' do
|
172
172
|
before do
|
173
|
-
parent.
|
173
|
+
parent.save_draft
|
174
174
|
child.save!
|
175
175
|
child.name = 'Heather'
|
176
|
-
child.
|
176
|
+
child.save_draft
|
177
177
|
end
|
178
178
|
|
179
179
|
subject { child.draft }
|
@@ -191,11 +191,11 @@ describe Child, :type => :model do
|
|
191
191
|
let(:new_parent) { Parent.new(:name => 'Patty') }
|
192
192
|
|
193
193
|
before do
|
194
|
-
parent.
|
194
|
+
parent.save_draft
|
195
195
|
child.save!
|
196
|
-
new_parent.
|
196
|
+
new_parent.save_draft
|
197
197
|
child.parent = new_parent
|
198
|
-
child.
|
198
|
+
child.save_draft
|
199
199
|
end
|
200
200
|
|
201
201
|
subject { child.draft }
|
@@ -231,8 +231,8 @@ describe Child, :type => :model do
|
|
231
231
|
describe 'draft_reversion_dependencies' do
|
232
232
|
context 'parent `create` draft with child `create` draft' do
|
233
233
|
before do
|
234
|
-
parent.
|
235
|
-
child.
|
234
|
+
parent.save_draft
|
235
|
+
child.save_draft
|
236
236
|
end
|
237
237
|
|
238
238
|
subject { child.draft }
|