mongoid-simple-userstamps 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb3debfc6e1e3ff58dc489d52e8e39d1d103766f1aae5f22518d4921f59071d1
4
- data.tar.gz: 6c9ae82db5763d527e98017773877ca4fb89e6755f4f8e305ee7a5c8b4850465
3
+ metadata.gz: aab39d23775b21fb081e9ba7da53188179a0c61c4dc134d4fa048a8bc2cbd03a
4
+ data.tar.gz: e0c17a42b7ba147cdc12506c8b5f611c1acf17b0305bfbd6f1f1125c3575db14
5
5
  SHA512:
6
- metadata.gz: 2efcc441865a2a86443adc6a83daeeafde70a20c8ca9a6d4bfb7807b2807b5fe3883733757a6b8f00eac36fb79d4a67d9f40c06813815d5afd3f21863e13107b
7
- data.tar.gz: 0d9c86ec9d0933bd334051b75f4b820344f9250780cb4fa0010d9562c86caef635c161a24a4bf68415f17fd3c813ee15ba48d2000a3926aedb81f1974936ff16
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
- pòst = Post.create
44
- post.created_by # <User _id: 57305c268675c32e1c70a17e >
43
+ post = Post.create
44
+ post.created_by # <User _id: 1 >
45
45
  post.updated_by # nil
46
46
 
47
- post.title = 'title'
48
- post.save
49
- post.updated_by # <User _id: 57305c268675c32e1c70a17e >
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
- self.created_by = Mongoid::Userstamps::User.current unless created_by
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
- self.updated_by = Mongoid::Userstamps::User.current unless updated_by_id_changed?
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
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Userstamps
3
- VERSION = '1.1.0'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
@@ -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 = "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 = "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.1.0
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-07-04 00:00:00.000000000 Z
11
+ date: 2018-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler