draft_approve 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/.yardopts +6 -0
- data/Appraisals +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +91 -0
- data/LICENSE.md +21 -0
- data/README.md +329 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/draft_approve.gemspec +56 -0
- data/lib/draft_approve.rb +5 -0
- data/lib/draft_approve/draft_changes_proxy.rb +242 -0
- data/lib/draft_approve/draftable/base_class_methods.rb +33 -0
- data/lib/draft_approve/draftable/class_methods.rb +119 -0
- data/lib/draft_approve/draftable/instance_methods.rb +80 -0
- data/lib/draft_approve/errors.rb +19 -0
- data/lib/draft_approve/models/draft.rb +75 -0
- data/lib/draft_approve/models/draft_transaction.rb +109 -0
- data/lib/draft_approve/persistor.rb +167 -0
- data/lib/draft_approve/serialization/json.rb +16 -0
- data/lib/draft_approve/serialization/json/constants.rb +21 -0
- data/lib/draft_approve/serialization/json/draft_changes_proxy.rb +317 -0
- data/lib/draft_approve/serialization/json/serializer.rb +181 -0
- data/lib/draft_approve/transaction.rb +125 -0
- data/lib/draft_approve/version.rb +3 -0
- data/lib/generators/draft_approve/migration/migration_generator.rb +41 -0
- data/lib/generators/draft_approve/migration/templates/create_draft_approve_tables.rb +25 -0
- metadata +253 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 778e1169104c109857ae10ab5be887080d7cd6128dbffe51e8a339fd49fff197
|
4
|
+
data.tar.gz: c744acb2e0f8c2262407864860edb42c5153e498d651071f63a4fb4f0fca423d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: da0f2ee7cca8abb87de7448178fa86fbe5e083b832c89b3d604d557602c18026c062c12bf80a5148d24507e41eec728ed4fd92780b02ae95c8724cb9c5dd2d2e
|
7
|
+
data.tar.gz: d72607ca1a9dd39a82fce60863b1e2a368afbc9f5d6c3ae4ce315d51de5625bc01318a5f11d446f8af26cd456991d6236b9cbd2b9ed3417e304babfc78415acb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Appraisals
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at TODO: Write your email address. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
draft_approve (0.1.0)
|
5
|
+
activerecord (~> 5.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (5.2.2)
|
11
|
+
activesupport (= 5.2.2)
|
12
|
+
activerecord (5.2.2)
|
13
|
+
activemodel (= 5.2.2)
|
14
|
+
activesupport (= 5.2.2)
|
15
|
+
arel (>= 9.0)
|
16
|
+
activesupport (5.2.2)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 0.7, < 2)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
appraisal (2.2.0)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
thor (>= 0.14.0)
|
25
|
+
arel (9.0.0)
|
26
|
+
codecov (0.1.14)
|
27
|
+
json
|
28
|
+
simplecov
|
29
|
+
url
|
30
|
+
coderay (1.1.2)
|
31
|
+
concurrent-ruby (1.1.4)
|
32
|
+
database_cleaner (1.7.0)
|
33
|
+
diff-lcs (1.3)
|
34
|
+
docile (1.3.1)
|
35
|
+
factory_bot (4.11.1)
|
36
|
+
activesupport (>= 3.0.0)
|
37
|
+
i18n (1.5.3)
|
38
|
+
concurrent-ruby (~> 1.0)
|
39
|
+
json (2.1.0)
|
40
|
+
method_source (0.9.2)
|
41
|
+
minitest (5.11.3)
|
42
|
+
pg (1.1.4)
|
43
|
+
pry (0.12.2)
|
44
|
+
coderay (~> 1.1.0)
|
45
|
+
method_source (~> 0.9.0)
|
46
|
+
rake (10.5.0)
|
47
|
+
rspec (3.8.0)
|
48
|
+
rspec-core (~> 3.8.0)
|
49
|
+
rspec-expectations (~> 3.8.0)
|
50
|
+
rspec-mocks (~> 3.8.0)
|
51
|
+
rspec-core (3.8.0)
|
52
|
+
rspec-support (~> 3.8.0)
|
53
|
+
rspec-expectations (3.8.2)
|
54
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
55
|
+
rspec-support (~> 3.8.0)
|
56
|
+
rspec-mocks (3.8.0)
|
57
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
58
|
+
rspec-support (~> 3.8.0)
|
59
|
+
rspec-support (3.8.0)
|
60
|
+
simplecov (0.16.1)
|
61
|
+
docile (~> 1.1)
|
62
|
+
json (>= 1.8, < 3)
|
63
|
+
simplecov-html (~> 0.10.0)
|
64
|
+
simplecov-html (0.10.2)
|
65
|
+
sqlite3 (1.3.13)
|
66
|
+
thor (0.20.3)
|
67
|
+
thread_safe (0.3.6)
|
68
|
+
tzinfo (1.2.5)
|
69
|
+
thread_safe (~> 0.1)
|
70
|
+
url (0.3.2)
|
71
|
+
yard (0.9.18)
|
72
|
+
|
73
|
+
PLATFORMS
|
74
|
+
ruby
|
75
|
+
|
76
|
+
DEPENDENCIES
|
77
|
+
appraisal (~> 2.2)
|
78
|
+
bundler (~> 1.17)
|
79
|
+
codecov (~> 0.1)
|
80
|
+
database_cleaner (~> 1.7)
|
81
|
+
draft_approve!
|
82
|
+
factory_bot (~> 4.11)
|
83
|
+
pg (>= 0.18, < 2.0)
|
84
|
+
pry (~> 0.12)
|
85
|
+
rake (~> 10.0)
|
86
|
+
rspec (~> 3.0)
|
87
|
+
sqlite3 (~> 1.3)
|
88
|
+
yard (~> 0.9.18)
|
89
|
+
|
90
|
+
BUNDLED WITH
|
91
|
+
1.17.0
|
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019, 38 Degrees Ltd
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,329 @@
|
|
1
|
+
# DraftApprove
|
2
|
+
|
3
|
+
DraftApprove is a Ruby gem which lets you save draft changes of your ActiveRecord models to your database. It allows grouping of related changes into a 'Draft Transaction' which must be approved or rejected as a whole, rather than allowing individual draft changes to be applied independently.
|
4
|
+
|
5
|
+
There are a number of other similar Ruby gems available for drafting changes to ActiveRecord models. Depending upon your projects needs, another gem may be more suitable. See the [Alternative Drafting Gems](#alternative-drafting-gems) section for full details.
|
6
|
+
|
7
|
+
The specific features / functionality offered by DraftApprove are:
|
8
|
+
|
9
|
+
* No changes are needed to your existing database tables
|
10
|
+
* No updates are required to your existing ActiveRecord queries or raw SQL queries
|
11
|
+
* It is possible to save drafts of new records, save draft changes to existing records, and save draft deletions of records
|
12
|
+
* Multiple related draft changes (new records, updates, deletions) may be grouped together in a 'Draft Transaction' which must then be approved or rejected as a whole
|
13
|
+
* This includes being able to save a draft of a model which references an _unsaved_ model - as long as that unsaved model already has a draft
|
14
|
+
* Each model may only have one pending draft at a time
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'draft_approve'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install draft_approve
|
31
|
+
|
32
|
+
Once installed, you must generate the migration to create the required draft tables in your database, and run the migration:
|
33
|
+
|
34
|
+
```
|
35
|
+
$ rails generate draft_approve:migration
|
36
|
+
$ rails db:migrate
|
37
|
+
```
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
### Make your Models draftable
|
42
|
+
|
43
|
+
Add `acts_as_draftable` to all models you'd like to be draftable. For example:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
class Person < ActiveRecord::Base
|
47
|
+
has_many :contact_addresses
|
48
|
+
acts_as_draftable
|
49
|
+
end
|
50
|
+
|
51
|
+
class ContactAddress < ActiveRecord::Base
|
52
|
+
belongs_to :person
|
53
|
+
acts_as_draftable
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
### Create a draft for a single object
|
58
|
+
|
59
|
+
Call `draft_save!` to save a draft of a new model, or save draft changes to an existing model.
|
60
|
+
|
61
|
+
Call `draft_destroy!` to draft the deletion of the model.
|
62
|
+
|
63
|
+
There are also convenience methods `draft_create!` and `draft_update!`.
|
64
|
+
|
65
|
+
For example:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
### CREATE EXAMPLES
|
69
|
+
|
70
|
+
# Save draft of a new model
|
71
|
+
person = Person.new(name: 'new person')
|
72
|
+
draft = person.draft_save!
|
73
|
+
|
74
|
+
# Short-hand to save draft of a new model
|
75
|
+
draft = Person.draft_create!(name: 'new person')
|
76
|
+
|
77
|
+
### UPDATE EXAMPLES
|
78
|
+
|
79
|
+
# Save draft changes to an existing person
|
80
|
+
person = Person.find(1)
|
81
|
+
person.name = 'update existing person'
|
82
|
+
draft = person.draft_save!
|
83
|
+
|
84
|
+
# Short-hand to save draft changes to an existing person
|
85
|
+
draft = person.draft_update!(name: 'update existing person')
|
86
|
+
|
87
|
+
### DELETE EXAMPLES
|
88
|
+
|
89
|
+
# Draft delete an existing person
|
90
|
+
person = Person.find(2)
|
91
|
+
draft = person.draft_destroy!
|
92
|
+
```
|
93
|
+
|
94
|
+
### Create multiple related drafts
|
95
|
+
|
96
|
+
If you want to ensure multiple related changes are all approved, or all rejected, as a single block, use a Draft Transaction. You do this by calling the `draft_transaction` method on any draftable model class, and passing it a block where all your drafts are saved. You use the same `draft_save!` and `draft_destroy!` methods within the Draft Transaction.
|
97
|
+
|
98
|
+
For example:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
draft_transaction = Person.draft_transaction do
|
102
|
+
# Want reference to person object, so don't use shorthand draft_create! method
|
103
|
+
person = Person.new(name: 'new person name')
|
104
|
+
person.draft_save!
|
105
|
+
|
106
|
+
existing_contact_address = ContactAddress.find(1)
|
107
|
+
existing_contact_address.draft_update!(person: person)
|
108
|
+
|
109
|
+
ContactAddress.find(2).draft_destroy!
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
This would create 3 drafts (one to create a new person, one to update an existing contact address, and one to delete a different contact address). These must all be applied together, or all be rejected.
|
114
|
+
|
115
|
+
### Approve drafts
|
116
|
+
|
117
|
+
Regardless of how a draft was created, a Draft Transaction is always created, and the Draft Transaction is what needs to be approved. This will apply the changes in all drafts within the Draft Transaction (which may only be one draft).
|
118
|
+
|
119
|
+
For example:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
# If you have reference to a Draft object
|
123
|
+
draft.draft_transaction.approve_changes!(reviewed_by: 'my_username', review_reason: 'Looks Good!')
|
124
|
+
|
125
|
+
# If you have reference to a DraftTransaction object
|
126
|
+
draft_transaction.approve_changes!(reviewed_by: 'my_username', review_reason: 'Looks Good!')
|
127
|
+
```
|
128
|
+
|
129
|
+
### Reject drafts
|
130
|
+
|
131
|
+
This will reject all changes in all drafts within the Draft Transaction (which may only be one draft).
|
132
|
+
|
133
|
+
For example:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
# If you have reference to a Draft object
|
137
|
+
draft.draft_transaction.reject_changes!(reviewed_by: 'my_username', review_reason: 'Nope!')
|
138
|
+
|
139
|
+
# If you have reference to a DraftTransaction object
|
140
|
+
draft_transaction.reject_changes!(reviewed_by: 'my_username', review_reason: 'Nope!')
|
141
|
+
```
|
142
|
+
|
143
|
+
### Find drafts pending approval
|
144
|
+
|
145
|
+
As discussed, all drafts are created inside a Draft Transaction, and it is these which must be approved or rejected.
|
146
|
+
|
147
|
+
You can find all Draft Transactions with a particular status using the following methods:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
pending_draft_transactions = DraftTransaction.pending_approval
|
151
|
+
|
152
|
+
approved_draft_transactions = DraftTransaction.approved
|
153
|
+
|
154
|
+
rejected_draft_transactions = DraftTransaction.rejected
|
155
|
+
```
|
156
|
+
|
157
|
+
### Errors
|
158
|
+
|
159
|
+
If an error occurs while approving a transaction, the error will cause the transaction to fail, so none of the draft changes will be applied. The Draft Transaction will have its `status` set to `approval_error`, and its `error` column will contain more information (the error and the backtrace).
|
160
|
+
|
161
|
+
All Draft Transactions with an error can be found using the following:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
errored_draft_transactions = DraftTransaction.approval_error
|
165
|
+
```
|
166
|
+
|
167
|
+
### Advanced usage
|
168
|
+
|
169
|
+
#### Who created a draft?
|
170
|
+
|
171
|
+
When creating a Draft Transaction, you may pass in a `created_by` string. This could be a username or the name of an automated process, and will be stored in the `DraftTransaction.created_by` column in the database. This option is only available when saving drafts within an explicit Draft Transaction.
|
172
|
+
|
173
|
+
For example:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
draft_transaction = Person.draft_transaction(created_by: 'UserA') do
|
177
|
+
Person.new(name: 'new person name').draft_save!
|
178
|
+
end
|
179
|
+
```
|
180
|
+
|
181
|
+
#### Extra metadata for drafts
|
182
|
+
|
183
|
+
When creating a Draft Transaction, you may pass in an `extra_data` hash. This can contain anything, and will be stored in the `DraftTransaction.extra_data` column in the database. This option is only available when saving drafts within an explicit Draft Transaction.
|
184
|
+
|
185
|
+
Possible use-cases for the extra data hash are storing which users or roles are allowed to approve these drafts, storing additional data about why or how the drafts were created, etc. The DraftApprove gem does not implement these features for you (eg. limiting who can approve drafts), but simply gives you a way to store generic metadata about a Draft Transaction should you wish to build such features within your application logic.
|
186
|
+
|
187
|
+
For example:
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
extra_data = {
|
191
|
+
'can_be_approved_by' => ['SuperAdminRole', 'UserB'],
|
192
|
+
'data_source_url' => 'https://en.wikipedia.org/wiki/RubyGems',
|
193
|
+
'data_scraped_at' => '2019-02-08 12:00:00'
|
194
|
+
}
|
195
|
+
|
196
|
+
draft_transaction = Person.draft_transaction(extra_data: extra_data) do
|
197
|
+
Person.new(name: 'new person name').draft_save!
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
#### Skipping validations when saving drafts
|
202
|
+
|
203
|
+
By default, models will have their ActiveRecords validations checked before a draft is saved. This prevents invalid drafts from being persisted, which would just fail validation when the Draft Transaction is approved anyway.
|
204
|
+
|
205
|
+
_Side note - when saving a draft, only ActiveRecord validations are checked. Since the draft data is not written to your application table, database-only validations cannot be checked!_
|
206
|
+
|
207
|
+
If you would like to skip checking ActiveRecord validations when saving a draft, you may pass the `validate: false` option to `draft_save`, for example:
|
208
|
+
|
209
|
+
```
|
210
|
+
person = Person.new
|
211
|
+
person.draft_save!(validate: false)
|
212
|
+
```
|
213
|
+
|
214
|
+
Validations will still run when the draft is approved, so this option is not especially useful unless combined with a custom method for creating or updating the record (see below).
|
215
|
+
|
216
|
+
#### Custom methods for creating, updating and deleting data
|
217
|
+
|
218
|
+
When a Draft Transaction is approved, all drafts within the transaction are applied, meaning the changes within the draft are made live on the database. This is acheived by calling suitable ActiveRecord methods. The default methods used by the DraftApprove gem are:
|
219
|
+
|
220
|
+
* `create!` for new models saved with `draft_save!`
|
221
|
+
* `update!` for existing models which have been modified and saved with `draft_save!`
|
222
|
+
* `destroy!` for models which have had `draft_destroy!` called on them
|
223
|
+
|
224
|
+
Note that `create!` is a _class_ level ActiveRecord method, while `update!` and `destroy!` are _instance_ level ActiveRecord methods.
|
225
|
+
|
226
|
+
When saving drafts, you may override the method used to save the changes by passing an options hash to the `draft_save!` or `draft_destroy!` methods. You are not able to do this with the convenience `draft_create!` or `draft_update!` methods.
|
227
|
+
|
228
|
+
For example:
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
draft_transaction = Person.draft_transaction do
|
232
|
+
# When approved, find or create Person A
|
233
|
+
person = Person.new(name: 'Person A')
|
234
|
+
person.draft_save!(create_method: :find_or_create_by!)
|
235
|
+
|
236
|
+
# When approved, update the record ignoring validations
|
237
|
+
existing_person = Person.find(1)
|
238
|
+
existing_person.birth_date = '1800-01-01'
|
239
|
+
existing_person.draft_save!(update_method: :update_columns)
|
240
|
+
|
241
|
+
# When approved, delete the record directly in the database without any ActiveRecord callbacks
|
242
|
+
Person.find(2).draft_destroy!(delete_method: :delete)
|
243
|
+
end
|
244
|
+
```
|
245
|
+
|
246
|
+
**CAUTION**
|
247
|
+
* No validation is done to check you are using sensible alternative methods, so use at your own risk!
|
248
|
+
* **It is strongly recommended to use methods which will raise an error if they fail**, otherwise one draft in a Draft Transaction may 'silently' fail, causing subsequent drafts to be applied, and the Draft Transaction as a whole may appear to have been successfully approved & applied
|
249
|
+
* Methods used as the `create_method` **must** be _class_ methods for the model you are drafting, which accept a hash of attribute names to attribute values (eg. `Person.create!`, `Person.find_or_create_by!`, etc)
|
250
|
+
* Methods used as the `update_method` **must** be _instance_ methods for the model you are drafting, which accept a hash of attribute names to attribute values (eg. `person.update!`, `person.update_attributes!`, etc)
|
251
|
+
* Methods used as the `delete_method` **must** be _instance_ methods for the model you are drafting, which requires no arguments (eg. `person.destroy!`, `person.delete`, etc)
|
252
|
+
|
253
|
+
### More examples
|
254
|
+
|
255
|
+
Further examples can be seen in the [integration tests](spec/integration).
|
256
|
+
|
257
|
+
## Frequently Asked Questions
|
258
|
+
|
259
|
+
### Why am I getting `ActiveRecord::RecordInvalid` errors when I save a draft?
|
260
|
+
|
261
|
+
If you wish to purposefully save drafts which do not pass validations, see the [Skipping validations when saving drafts](#skipping-validations-when-saving-drafts) section.
|
262
|
+
|
263
|
+
If you are unexpectedly getting `ActiveRecord::RecordInvalid` errors, a _possible_ reason is explicit validations on foreign key columns. For example, the following would fail:
|
264
|
+
|
265
|
+
```ruby
|
266
|
+
class Person < ActiveRecord::Base
|
267
|
+
has_many :contact_addresses
|
268
|
+
acts_as_draftable
|
269
|
+
end
|
270
|
+
|
271
|
+
class ContactAddress < ActiveRecord::Base
|
272
|
+
belongs_to :person
|
273
|
+
validates :person_id, presence: true # This validation is unnecessary and can cause errors
|
274
|
+
acts_as_draftable
|
275
|
+
end
|
276
|
+
|
277
|
+
draft_transaction = Person.draft_transaction do
|
278
|
+
# Create a new person, and save it as a draft (note, this means p.id is nil!)
|
279
|
+
p = Person.new(name: 'person name')
|
280
|
+
p.save_draft!
|
281
|
+
|
282
|
+
c = ContactAddress.new(person: p)
|
283
|
+
c.save_draft! # raises ActiveRecord::RecordInvalid because contact_address.person_id is nil
|
284
|
+
end
|
285
|
+
```
|
286
|
+
|
287
|
+
This can be fixed by removing the explicit `presence: true` validation of foreign key columns. Such validations should not be necessary anyway, because by default `belongs_to` relationships validate the associated object is not `nil`.
|
288
|
+
|
289
|
+
_Side note: the `belongs_to` validations do not cause such errors when saving a draft because they check the associated object (eg. `person`) is not `nil` - rather than validating that the component attributes / columns of the association (eg. `person_id`) are not `nil`. In the example above, the `belongs_to` validation would check `contact_address.person` is not `nil`, which it is not - the `Person` object referred to has not been persisted, but it is not `nil`, so the validation passes._
|
290
|
+
|
291
|
+
## Alternative Drafting Gems
|
292
|
+
|
293
|
+
* [Drafting](https://github.com/ledermann/drafting)
|
294
|
+
* [DraftPunk](https://github.com/stevehodges/draftpunk)
|
295
|
+
* [Draftsman](https://github.com/jmfederico/draftsman)
|
296
|
+
|
297
|
+
**DraftPunk** and **Draftsman** both require changes to your existing database tables. In itself, this is not a problem, however this also _potentially_ requires changes to your ActiveRecord Queries and any raw SQL you may be executing in order to ensure draft models or draft changes are not accidentally returned by queries or shown to end users.
|
298
|
+
|
299
|
+
This problem can be avoided using default scopes on your models. This may be a suitable solution for new projects, or projects which don't utilise much or any raw SQL queries.
|
300
|
+
|
301
|
+
See the [DraftPunk documentation](https://github.com/stevehodges/draftpunk#what-about-the-rest-of-the-application-people-are-seeing-draft-businesses) and [Draftsman documentation](https://github.com/jmfederico/draftsman#drafted-item-scopes) on using scopes.
|
302
|
+
|
303
|
+
**Drafting** does not require any modifications to existing tables, and therefore has no risk of existing queries accidentally returning draft data. However, [it only allows saving drafts on records which are not persisted yet](https://github.com/ledermann/drafting#hints). This may be suitable for projects where it is not necessary to create and approve draft updates to objects.
|
304
|
+
|
305
|
+
All the above gem also have other specific features / advantages unique to them, so before selecting the most suitable gem for your needs, it is recommended you read their documentation and trial them to find which is most suited to your project requirements.
|
306
|
+
|
307
|
+
## License
|
308
|
+
|
309
|
+
[MIT License](LICENSE.md)
|
310
|
+
|
311
|
+
Copyright (c) 2019, 38 Degrees Ltd
|
312
|
+
|
313
|
+
## Development
|
314
|
+
|
315
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
316
|
+
|
317
|
+
To install this gem onto your local machine, run `bundle exec rake install`. Alternatively you may run `gem build draft_approve.gemspec` to generate the `.gem` file, then run `gem install ./draft_approve-0.1.0.gem` (replace `0.1.0` with the correct gem version).
|
318
|
+
|
319
|
+
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
320
|
+
|
321
|
+
To generate the YARD documentation locally, run `yard doc`, which will install the documentation into the `doc/` folder.
|
322
|
+
|
323
|
+
## Contributing
|
324
|
+
|
325
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/38dgs/draft_approve. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
326
|
+
|
327
|
+
## Code of Conduct
|
328
|
+
|
329
|
+
Everyone interacting in the DraftApprove project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/38dgs/draft_approve/blob/master/CODE_OF_CONDUCT.md).
|