mongoid-simple-userstamps 1.1.0 → 1.2.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/README.md +25 -5
- data/lib/mongoid-simple-userstamps/model.rb +17 -2
- data/lib/mongoid-simple-userstamps/version.rb +1 -1
- data/spec/userstamps_spec.rb +14 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aab39d23775b21fb081e9ba7da53188179a0c61c4dc134d4fa048a8bc2cbd03a
|
4
|
+
data.tar.gz: e0c17a42b7ba147cdc12506c8b5f611c1acf17b0305bfbd6f1f1125c3575db14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3c83b0bd230dccb46428a0623f324806f94045e2427948750c06083e106eb443b8c2e02b62ce37a110a9c1086f22e6c77f87d724a71bee2aa9be96bbae1cdf5
|
7
|
+
data.tar.gz: e6e954d1a673270fa9dcfffc319d7da666d8c7eb85a8474678c50cc8d49286378652e33610ff058d170aec18b8c0af1425f0033761a5c60ae28f28c5fd6c0ca9
|
data/README.md
CHANGED
@@ -40,15 +40,35 @@ Or install it yourself as:
|
|
40
40
|
user = User.create
|
41
41
|
Mongoid::Userstamps::User.current = user
|
42
42
|
|
43
|
-
|
44
|
-
post.created_by # <User _id:
|
43
|
+
post = Post.create
|
44
|
+
post.created_by # <User _id: 1 >
|
45
45
|
post.updated_by # nil
|
46
46
|
|
47
|
-
post.title
|
48
|
-
post.
|
49
|
-
|
47
|
+
post.update(title: 'title')
|
48
|
+
post.updated_by # <User _id: 1 >
|
49
|
+
|
50
|
+
# set manually
|
51
|
+
other_user = User.create
|
52
|
+
post.update(updated_by: other_user)
|
53
|
+
post.updated_by # <User _id: 2 >
|
54
|
+
|
55
|
+
# skip userstamps
|
56
|
+
post.skip_userstamps.update(title: 'new_title')
|
57
|
+
post.updated_by # <User _id: 2 >
|
50
58
|
```
|
51
59
|
|
60
|
+
## Preservation of Manually-set Values
|
61
|
+
|
62
|
+
* The `creator` is only set during the creation of new models (`before_create` callback).
|
63
|
+
Mongoid::Userstamps will not overwrite the `creator` field if it already contains a value
|
64
|
+
(i.e. was manually set.)
|
65
|
+
|
66
|
+
* The `updater` is set each time the model is updated (`before_update` callback).
|
67
|
+
Mongoid::Userstamps will not overwrite the `updater` field if it been modified since the last save,
|
68
|
+
as per Mongoid's built-in "dirty tracking" feature.
|
69
|
+
|
70
|
+
> When the `updater` is the same and we need to preserve, we need to use `skip_userstamps` method.
|
71
|
+
|
52
72
|
### Rails
|
53
73
|
|
54
74
|
- Define current user in your `application_controller.rb`
|
@@ -6,6 +6,8 @@ module Mongoid
|
|
6
6
|
module Model
|
7
7
|
def self.included(base)
|
8
8
|
base.class_eval do
|
9
|
+
attr_reader :skip_userstamps_flag
|
10
|
+
|
9
11
|
if Mongoid::VERSION.to_f < 6
|
10
12
|
belongs_to :created_by, polymorphic: true
|
11
13
|
belongs_to :updated_by, polymorphic: true
|
@@ -15,11 +17,24 @@ module Mongoid
|
|
15
17
|
end
|
16
18
|
|
17
19
|
before_create do
|
18
|
-
|
20
|
+
unless @skip_userstamps_flag || created_by
|
21
|
+
self.created_by = Mongoid::Userstamps::User.current
|
22
|
+
end
|
19
23
|
end
|
20
24
|
|
21
25
|
before_update do
|
22
|
-
|
26
|
+
unless @skip_userstamps_flag || updated_by_id_changed?
|
27
|
+
self.updated_by = Mongoid::Userstamps::User.current
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
after_save do
|
32
|
+
@skip_userstamps_flag = false
|
33
|
+
end
|
34
|
+
|
35
|
+
def skip_userstamps
|
36
|
+
@skip_userstamps_flag = true
|
37
|
+
self
|
23
38
|
end
|
24
39
|
end
|
25
40
|
end
|
data/spec/userstamps_spec.rb
CHANGED
@@ -42,19 +42,27 @@ describe 'Mongoid::Userstamp' do
|
|
42
42
|
context 'when update Post' do
|
43
43
|
it 'should save user stamps relations' do
|
44
44
|
post = Post.create
|
45
|
-
post.title
|
46
|
-
post.save
|
45
|
+
post.update(title: 'title')
|
47
46
|
expect(post.updated_by).to eq(@user)
|
48
47
|
end
|
49
48
|
|
50
|
-
it 'should not overwrite updated_by' do
|
49
|
+
it 'should not overwrite updated_by when it changes' do
|
51
50
|
user = User.create
|
52
51
|
post = Post.create
|
53
|
-
post.title
|
54
|
-
post.updated_by = user
|
55
|
-
post.save
|
52
|
+
post.update(title: 'title', updated_by: user)
|
56
53
|
expect(post.updated_by).to eq(user)
|
57
54
|
end
|
55
|
+
|
56
|
+
it 'should not overwrite updated_by when use skip_userstamps' do
|
57
|
+
user = User.create
|
58
|
+
post = Post.create(updated_by: user)
|
59
|
+
post.skip_userstamps.update(title: 'title')
|
60
|
+
expect(post.updated_by).to eq(user)
|
61
|
+
|
62
|
+
# after saved skip is removed
|
63
|
+
post.update(title: 'new_title')
|
64
|
+
expect(post.updated_by).to eq(@user)
|
65
|
+
end
|
58
66
|
end
|
59
67
|
|
60
68
|
context 'when check thread safe' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-simple-userstamps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Jurado
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|