mongoid-undo 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +7 -1
- data/Gemfile +3 -0
- data/README.md +70 -2
- data/lib/mongoid/undo/version.rb +1 -1
- data/lib/mongoid/undo.rb +4 -4
- data/mongoid-undo.gemspec +2 -4
- data/spec/spec_helper.rb +0 -2
- data/spec/undo_spec.rb +105 -82
- metadata +5 -21
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,76 @@
|
|
1
1
|
# mongoid-undo
|
2
2
|
|
3
|
-
Super simple
|
3
|
+
Super simple undo for your Mongoid app, based on both great modules
|
4
|
+
[Mongoid::Paranoia](http://mongoid.org/en/mongoid/docs/extras.html#paranoia) and
|
5
|
+
[Mongoid::Versioning](http://mongoid.org/en/mongoid/docs/extras.html#versioning).
|
4
6
|
|
5
|
-
|
7
|
+
## How does it work?
|
8
|
+
|
9
|
+
* `Mongoid::Paranoia` is used to mark documents as deleted, instead of deleting them really, otherwise restoring would be impossible ;).
|
10
|
+
* `Mongoid::Versioning` is used to keep the older versions of your document, so we can restore them.
|
11
|
+
* `Mongoid::Undo` adds an `_action` field to your documents, so we can easily determine whether it was created, updated, or destroyed.
|
12
|
+
|
13
|
+
But instead of explaining all the details, you should get the idea by looking at the Usage section.
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class Document
|
19
|
+
include Mongoid::Document
|
20
|
+
include Mongoid::Undo
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
### Creating (and undoing)
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
document = Document.create
|
28
|
+
document.persisted? #=> true
|
29
|
+
|
30
|
+
document.undo
|
31
|
+
document.persisted? #=> false
|
32
|
+
|
33
|
+
document.redo # A nice alias for undo ;)
|
34
|
+
document.persisted? #=> true
|
35
|
+
```
|
36
|
+
|
37
|
+
### Updating (and undoing)
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
document = Document.create(name: 'foo')
|
41
|
+
|
42
|
+
document.update_attributes(name: 'bar')
|
43
|
+
document.name #=> 'bar'
|
44
|
+
|
45
|
+
document.undo
|
46
|
+
document.name #=> 'foo'
|
47
|
+
|
48
|
+
document.redo
|
49
|
+
document.name #=> 'bar'
|
50
|
+
```
|
51
|
+
|
52
|
+
### Destroying (and undoing)
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
document = Document.first
|
56
|
+
|
57
|
+
document.destroy
|
58
|
+
document.persisted? #=> false
|
59
|
+
|
60
|
+
document.undo
|
61
|
+
document.persisted? #=> true
|
62
|
+
|
63
|
+
document.redo
|
64
|
+
document.persisted? #=> false
|
65
|
+
```
|
66
|
+
|
67
|
+
## Installation
|
68
|
+
|
69
|
+
In your Gemfile:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
gem 'mongoid-undo'
|
73
|
+
```
|
6
74
|
|
7
75
|
## Contributing
|
8
76
|
|
data/lib/mongoid/undo/version.rb
CHANGED
data/lib/mongoid/undo.rb
CHANGED
@@ -22,19 +22,19 @@ module Mongoid
|
|
22
22
|
include Mongoid::Versioning
|
23
23
|
|
24
24
|
included do
|
25
|
-
field :
|
25
|
+
field :_action, type: Symbol, versioned: false
|
26
26
|
index deleted_at: 1
|
27
27
|
|
28
28
|
[:create, :update, :destroy].each do |action|
|
29
29
|
set_callback action, :after do
|
30
|
-
collection.find(atomic_selector).update
|
30
|
+
collection.find(atomic_selector).update('$set' => { _action: action })
|
31
31
|
reload
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
def undo
|
37
|
-
case
|
37
|
+
case _action
|
38
38
|
when :create, :destroy
|
39
39
|
deleted_at.present? ? restore : delete
|
40
40
|
when :update
|
@@ -45,7 +45,7 @@ module Mongoid
|
|
45
45
|
|
46
46
|
private
|
47
47
|
def retrieve
|
48
|
-
update_attributes
|
48
|
+
update_attributes(versions.last.versioned_attributes.except('version'))
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
data/mongoid-undo.gemspec
CHANGED
@@ -6,13 +6,11 @@ Gem::Specification.new do |gem|
|
|
6
6
|
gem.authors = 'Mario Uher'
|
7
7
|
gem.email = 'uher.mario@gmail.com'
|
8
8
|
gem.homepage = 'https://github.com/haihappen/mongoid-undo'
|
9
|
-
gem.summary = 'Super simple
|
10
|
-
gem.description = 'mongoid-undo provides a super simple and easy to use
|
9
|
+
gem.summary = 'Super simple undo for your Mongoid app.'
|
10
|
+
gem.description = 'mongoid-undo provides a super simple and easy to use undo system for Mongo apps.'
|
11
11
|
|
12
12
|
gem.files = `git ls-files`.split("\n")
|
13
13
|
gem.require_path = 'lib'
|
14
14
|
|
15
15
|
gem.add_dependency 'mongoid'
|
16
|
-
|
17
|
-
gem.add_development_dependency 'minitest'
|
18
16
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/undo_spec.rb
CHANGED
@@ -3,8 +3,6 @@ require File.expand_path('../spec_helper', __FILE__)
|
|
3
3
|
module Mongoid
|
4
4
|
module Undo
|
5
5
|
describe self do
|
6
|
-
include ActiveSupport::Testing::Assertions
|
7
|
-
|
8
6
|
class Document
|
9
7
|
include Mongoid::Document
|
10
8
|
include Mongoid::Undo
|
@@ -13,105 +11,140 @@ module Mongoid
|
|
13
11
|
end
|
14
12
|
|
15
13
|
|
16
|
-
subject { Document.
|
14
|
+
subject { Document.new(name: 'foo') }
|
15
|
+
let(:name) { subject.name }
|
16
|
+
let(:action) { subject.send(:_action) }
|
17
17
|
|
18
18
|
|
19
|
-
describe '
|
20
|
-
before { subject.
|
19
|
+
describe 'creating' do
|
20
|
+
before { subject.save }
|
21
21
|
|
22
22
|
|
23
|
-
it '
|
24
|
-
|
25
|
-
subject.deleted_at.wont_be_nil
|
23
|
+
it 'sets action to :create' do
|
24
|
+
action.must_equal :create
|
26
25
|
end
|
27
26
|
|
28
27
|
|
29
|
-
describe '
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
subject.
|
28
|
+
describe 'undoing' do
|
29
|
+
before { subject.undo }
|
30
|
+
|
31
|
+
|
32
|
+
it 'deletes' do
|
33
|
+
subject.persisted?.wont_equal true
|
35
34
|
end
|
36
|
-
end
|
37
35
|
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
subject.undo
|
42
|
-
subject.action.must_equal :create
|
37
|
+
it 'keeps :create action' do
|
38
|
+
action.must_equal :create
|
43
39
|
end
|
44
|
-
end
|
45
|
-
end
|
46
40
|
|
47
41
|
|
48
|
-
|
49
|
-
|
42
|
+
describe 'redoing' do
|
43
|
+
before { subject.redo }
|
50
44
|
|
51
45
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
# Ensure the new version is saved to the db
|
58
|
-
subject.persisted?.must_equal true
|
59
|
-
subject.name.must_equal 'Version 1'
|
60
|
-
end
|
46
|
+
it 'restores' do
|
47
|
+
subject.persisted?.must_equal true
|
48
|
+
end
|
61
49
|
|
62
50
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
subject.action.must_equal :update
|
51
|
+
it 'keeps :create action' do
|
52
|
+
action.must_equal :create
|
53
|
+
end
|
67
54
|
end
|
68
55
|
end
|
69
|
-
end
|
70
56
|
|
71
57
|
|
72
|
-
|
73
|
-
|
74
|
-
before { subject.destroy }
|
58
|
+
describe 'updating' do
|
59
|
+
before { subject.update_attributes(name: 'bar') }
|
75
60
|
|
76
61
|
|
77
|
-
it '
|
78
|
-
|
79
|
-
|
80
|
-
subject.persisted?.must_equal true
|
81
|
-
subject.deleted_at.must_be_nil
|
62
|
+
it 'sets action to :update' do
|
63
|
+
action.must_equal :update
|
82
64
|
end
|
83
65
|
|
84
66
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
67
|
+
describe 'undoing' do
|
68
|
+
before { subject.undo }
|
69
|
+
|
70
|
+
|
71
|
+
it 'retrieves' do
|
72
|
+
name.must_equal 'foo'
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
it 'saves a new version' do
|
77
|
+
subject.version.must_equal 3
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
it 'keeps :update action' do
|
82
|
+
action.must_equal :update
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
describe 'redoing' do
|
87
|
+
before { subject.redo }
|
88
|
+
|
89
|
+
|
90
|
+
it 'retrieves' do
|
91
|
+
name.must_equal 'bar'
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
it 'saves a new version' do
|
96
|
+
subject.version.must_equal 4
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
it 'keeps :update action' do
|
101
|
+
action.must_equal :update
|
102
|
+
end
|
89
103
|
end
|
90
104
|
end
|
91
105
|
end
|
92
106
|
|
93
107
|
|
94
|
-
describe '
|
95
|
-
before
|
96
|
-
|
97
|
-
|
108
|
+
describe 'destroying' do
|
109
|
+
before { subject.destroy }
|
110
|
+
|
111
|
+
|
112
|
+
it 'sets action to :destroy' do
|
113
|
+
action.must_equal :destroy
|
98
114
|
end
|
99
115
|
|
100
116
|
|
101
|
-
it '
|
102
|
-
|
103
|
-
subject.undo
|
104
|
-
end
|
105
|
-
|
106
|
-
subject.persisted?.must_equal true
|
107
|
-
subject.deleted_at.must_be_nil
|
117
|
+
it 'marks as destroyed' do
|
118
|
+
subject.persisted?.must_equal false
|
108
119
|
end
|
109
120
|
|
110
121
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
122
|
+
describe 'undoing' do
|
123
|
+
before { subject.undo }
|
124
|
+
|
125
|
+
|
126
|
+
it 'restores' do
|
127
|
+
subject.persisted?.wont_equal false
|
128
|
+
end
|
129
|
+
|
130
|
+
|
131
|
+
it 'keeps :destroy action' do
|
132
|
+
action.must_equal :destroy
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
describe 'redoing' do
|
137
|
+
before { subject.redo }
|
138
|
+
|
139
|
+
|
140
|
+
it 'deletes' do
|
141
|
+
subject.persisted?.must_equal false
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
it 'keeps :destroy action' do
|
146
|
+
action.must_equal :destroy
|
147
|
+
end
|
115
148
|
end
|
116
149
|
end
|
117
150
|
end
|
@@ -125,19 +158,19 @@ module Mongoid
|
|
125
158
|
end
|
126
159
|
|
127
160
|
|
128
|
-
describe :
|
161
|
+
describe :_action do
|
129
162
|
it 'is a symbol' do
|
130
|
-
subject.fields['
|
163
|
+
subject.fields['_action'].options[:type].must_equal Symbol
|
131
164
|
end
|
132
165
|
|
133
166
|
|
134
167
|
it 'is versionless' do
|
135
|
-
subject.fields['
|
168
|
+
subject.fields['_action'].options[:versioned].must_equal false
|
136
169
|
end
|
137
170
|
end
|
138
171
|
|
139
172
|
|
140
|
-
describe
|
173
|
+
describe 'localization' do
|
141
174
|
class Localized
|
142
175
|
include Mongoid::Document
|
143
176
|
include Mongoid::Undo
|
@@ -146,26 +179,16 @@ module Mongoid
|
|
146
179
|
end
|
147
180
|
|
148
181
|
|
149
|
-
subject { Localized.new }
|
150
|
-
|
151
|
-
|
152
182
|
it 'works too with localized fields' do
|
153
|
-
subject.
|
154
|
-
subject.update_attributes language: 'English Updated'
|
183
|
+
subject = Localized.create(language: 'English')
|
155
184
|
|
156
|
-
|
157
|
-
|
158
|
-
end
|
185
|
+
subject.update_attributes(language: 'English Updated')
|
186
|
+
subject.undo
|
159
187
|
subject.language.must_equal 'English'
|
160
188
|
|
161
|
-
|
162
|
-
subject.send(:retrieve)
|
163
|
-
end
|
189
|
+
subject.redo
|
164
190
|
subject.language.must_equal 'English Updated'
|
165
191
|
end
|
166
|
-
|
167
|
-
|
168
|
-
after { I18n.locale = I18n.default_locale }
|
169
192
|
end
|
170
193
|
end
|
171
194
|
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: mongoid-undo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mario Uher
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,24 +27,8 @@ dependencies:
|
|
27
27
|
- !ruby/object:Gem::Version
|
28
28
|
version: '0'
|
29
29
|
none: false
|
30
|
-
-
|
31
|
-
|
32
|
-
requirements:
|
33
|
-
- - ! '>='
|
34
|
-
- !ruby/object:Gem::Version
|
35
|
-
version: '0'
|
36
|
-
none: false
|
37
|
-
name: minitest
|
38
|
-
type: :development
|
39
|
-
prerelease: false
|
40
|
-
requirement: !ruby/object:Gem::Requirement
|
41
|
-
requirements:
|
42
|
-
- - ! '>='
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
version: '0'
|
45
|
-
none: false
|
46
|
-
description: mongoid-undo provides a super simple and easy to use versioning system
|
47
|
-
for Mongo apps.
|
30
|
+
description: mongoid-undo provides a super simple and easy to use undo system for
|
31
|
+
Mongo apps.
|
48
32
|
email: uher.mario@gmail.com
|
49
33
|
executables: []
|
50
34
|
extensions: []
|
@@ -84,5 +68,5 @@ rubyforge_project:
|
|
84
68
|
rubygems_version: 1.8.23
|
85
69
|
signing_key:
|
86
70
|
specification_version: 3
|
87
|
-
summary: Super simple
|
71
|
+
summary: Super simple undo for your Mongoid app.
|
88
72
|
test_files: []
|