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.
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
- # You would normally use this in a migration or on the console,
88
- # when working with models directly. In a controller, it is set
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] ||= { :draft_class_name => 'Draftsman::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 `app/models/draft.rb` that adds
2
- # extra attributes, validations, associations, methods, etc. Be sure that this new model class extends
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 use the JSON serializer, change to
7
- # `Draftsman::Serializers::Json`. You could implement your own serializer if you really wanted to. See files in
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 for the `object`,
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
@@ -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(subject).to be_a Module
5
+ expect(::Draftsman).to be_a Module
8
6
  end
9
7
 
10
- describe :whodunnit do
11
- before(:all) { ::Draftsman.whodunnit = 'foobar' }
8
+ describe '.whodunnit' do
9
+ before(:all) { ::Draftsman.whodunnit = :foobar }
12
10
 
13
11
  it 'clears out `whodunnit` before each test' do
14
- expect(subject.whodunnit).to be_nil
12
+ expect(::Draftsman.whodunnit).to be_nil
15
13
  end
16
14
  end
17
15
 
18
- describe :controller_info do
19
- before(:all) { ::Draftsman.controller_info = { foo: 'bar' } }
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(subject.controller_info).to eql Hash.new
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').draft_creation
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.draft_update
13
+ trashable.save_draft
14
14
  head :no_content
15
15
  end
16
16
 
@@ -0,0 +1,7 @@
1
+ require 'draftsman/draft'
2
+
3
+ class OverriddenDraft < Draftsman::Draft
4
+ def im_overridden
5
+ true
6
+ end
7
+ end
@@ -2,65 +2,43 @@ class Talkative < ActiveRecord::Base
2
2
  has_drafts
3
3
 
4
4
  # draft_creation callbacks
5
- before_draft_creation :do_this_before_draft_creation
6
- around_draft_creation :do_this_around_draft_creation
7
- after_draft_creation :do_this_after_draft_creation
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
- # draft_update callbacks
10
- before_draft_update :do_this_before_draft_update
11
- around_draft_update :do_this_around_draft_update
12
- after_draft_update :do_this_after_draft_update
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 do_this_before_draft_creation
22
- self.before_comment = "I changed before creation"
16
+ def do_this_before_save
17
+ self.before_comment = 'I changed before save'
23
18
  end
24
19
 
25
- def do_this_around_draft_creation
26
- self.around_early_comment = "I changed around creation (before yield)"
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 = "I changed around creation (after yield)"
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 do_this_before_draft_destruction
54
- self.before_comment = "I changed before destroy"
31
+ def do_this_before_destruction
32
+ self.before_comment = 'I changed before destroy'
55
33
  end
56
34
 
57
- def do_this_around_draft_destruction
58
- self.around_early_comment = "I changed around destroy (before yield)"
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 = "I changed around destroy (after yield)"
38
+ self.around_late_comment = 'I changed around destroy (after yield)'
61
39
  end
62
40
 
63
- def do_this_after_draft_destruction
64
- self.after_comment = "I changed after destroy"
41
+ def do_this_after_destruction
42
+ self.after_comment = 'I changed after destroy'
65
43
  end
66
44
  end
@@ -43,6 +43,7 @@ ActiveRecord::Schema.define(version: 20160328184419) do
43
43
  t.integer "item_id"
44
44
  t.string "event", null: false
45
45
  t.string "whodunnit"
46
+ t.integer "user_id"
46
47
  t.text "object"
47
48
  t.text "object_changes"
48
49
  t.text "previous_draft"
@@ -1,14 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Child, :type => :model do
4
- let(:parent) { Parent.new(:name => 'Marge') }
5
- let(:child) { Child.new(:name => 'Lisa', :parent => parent) }
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.draft_creation
11
- child.draft_creation
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.draft_creation
81
- child.draft_creation
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.draft_creation
157
- child.draft_creation
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.draft_creation
173
+ parent.save_draft
174
174
  child.save!
175
175
  child.name = 'Heather'
176
- child.draft_update
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.draft_creation
194
+ parent.save_draft
195
195
  child.save!
196
- new_parent.draft_creation
196
+ new_parent.save_draft
197
197
  child.parent = new_parent
198
- child.draft_update
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.draft_creation
235
- child.draft_creation
234
+ parent.save_draft
235
+ child.save_draft
236
236
  end
237
237
 
238
238
  subject { child.draft }