File tree 9 files changed +76
-2
lines changed
9 files changed +76
-2
lines changed Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ Metrics/MethodLength:
32
32
# Offense count: 2
33
33
# Configuration parameters: CountComments.
34
34
Metrics/ModuleLength :
35
- Max : 168
35
+ Max : 174
36
36
37
37
# Offense count: 4
38
38
Metrics/PerceivedComplexity :
@@ -42,6 +42,7 @@ Metrics/PerceivedComplexity:
42
42
Style/Documentation :
43
43
Exclude :
44
44
- ' lib/mongoid/history.rb'
45
+ - ' lib/mongoid/history/hooks.rb'
45
46
- ' lib/mongoid/history/trackable.rb'
46
47
- ' lib/mongoid/history/tracker.rb'
47
48
- ' lib/mongoid/history/version.rb'
Original file line number Diff line number Diff line change 1
1
0.5.1 (Next)
2
2
------------
3
3
4
+ * [ #149 ] ( https://github.com/aq1018/mongoid-history/pull/149 ) : Back fetching modifier from controller - [ @melnikaite ] ( https://github.com/melnikaite ) .
4
5
* Your contribution here.
5
6
6
7
0.5.0 (2015/09/18)
Original file line number Diff line number Diff line change @@ -47,6 +47,40 @@ The following example sets the tracker class name using a Rails initializer.
47
47
Mongoid ::History .tracker_class_name = :history_tracker
48
48
```
49
49
50
+ ** Set controller**
51
+
52
+ Add hooks to ApplicationController to fetch modifier automatically.
53
+
54
+ ``` ruby
55
+ # app/controllers/application_controller.rb
56
+ class ApplicationController
57
+ include Mongoid ::History ::Hooks
58
+ end
59
+ ```
60
+
61
+ ** Set ` #current_user ` method name**
62
+
63
+ You can set the name of the method that returns currently logged in user if you don't want to set ` modifier ` explicitly on every update.
64
+
65
+ The following example sets the ` current_user_method ` using a Rails initializer
66
+
67
+ ``` ruby
68
+ # config/initializers/mongoid-history.rb
69
+ # initializer for mongoid-history
70
+ # assuming you're using devise/authlogic
71
+ Mongoid ::History .current_user_method = :current_user
72
+ ```
73
+
74
+ When ` current_user_method ` is set, mongoid-history will invoke this method on each update and set its result as the instance modifier.
75
+
76
+ ``` ruby
77
+ # assume that current_user return #<User _id: 1>
78
+ post = Post .first
79
+ post.update_attributes(:title => ' New title' )
80
+
81
+ post.history_tracks.last.modifier # => #<User _id: 1>
82
+ ```
83
+
50
84
** Create trackable classes and objects**
51
85
52
86
``` ruby
Original file line number Diff line number Diff line change 3
3
require 'mongoid/history/version'
4
4
require 'mongoid/history/tracker'
5
5
require 'mongoid/history/trackable'
6
+ require 'mongoid/history/hooks'
6
7
7
8
module Mongoid
8
9
module History
Original file line number Diff line number Diff line change
1
+ module Mongoid
2
+ module History
3
+ module Hooks
4
+ extend ActiveSupport ::Concern
5
+
6
+ included do
7
+ before_action do |controller |
8
+ Thread . current [ :mongoid_history_controller ] = controller
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
Original file line number Diff line number Diff line change @@ -238,6 +238,13 @@ def history_tracker_attributes(action)
238
238
modifier : send ( history_trackable_options [ :modifier_field ] )
239
239
}
240
240
241
+ unless @history_tracker_attributes [ :modifier ]
242
+ controller = Thread . current [ :mongoid_history_controller ]
243
+ if controller && controller . respond_to? ( Mongoid ::History . current_user_method , true )
244
+ @history_tracker_attributes [ :modifier ] = controller . send ( Mongoid ::History . current_user_method )
245
+ end
246
+ end
247
+
241
248
original , modified = transform_changes ( modified_attributes_for_action ( action ) )
242
249
243
250
@history_tracker_attributes [ :original ] = original
Original file line number Diff line number Diff line change @@ -14,7 +14,12 @@ module Tracker
14
14
field :version , type : Integer
15
15
field :action , type : String
16
16
field :scope , type : String
17
- belongs_to :modifier , class_name : Mongoid ::History . modifier_class_name
17
+ if respond_to? :t_belongs_to
18
+ # Tenacity support https://github.com/jwood/tenacity
19
+ t_belongs_to :modifier , class_name : Mongoid ::History . modifier_class_name
20
+ else
21
+ belongs_to :modifier , class_name : Mongoid ::History . modifier_class_name
22
+ end
18
23
19
24
index ( scope : 1 )
20
25
index ( association_chain : 1 )
Original file line number Diff line number Diff line change @@ -71,6 +71,9 @@ class Tag
71
71
class Foo < Comment
72
72
end
73
73
74
+ class Controller
75
+ end
76
+
74
77
@persisted_history_options = Mongoid ::History . trackable_class_options
75
78
end
76
79
@@ -536,6 +539,14 @@ class Foo < Comment
536
539
expect ( tag_foo . history_tracks . last . association_chain . last [ 'name' ] ) . to eq ( 'tags' )
537
540
expect { tag_foo . history_tracks . last . trackable } . not_to raise_error
538
541
end
542
+
543
+ it 'should save modifier' do
544
+ Thread . current [ :mongoid_history_controller ] = Controller . new
545
+ allow_any_instance_of ( Controller ) . to receive ( :current_user ) . and_return ( user )
546
+ expect ( Thread . current [ :mongoid_history_controller ] . current_user ) . to eq user
547
+ expect ( tag_foo . history_tracks . last . modifier ) . to eq user
548
+ expect ( tag_bar . history_tracks . last . modifier ) . to eq user
549
+ end
539
550
end
540
551
541
552
describe 'non-embedded' do
Original file line number Diff line number Diff line change @@ -9,5 +9,6 @@ class Tracker
9
9
config . after :each do
10
10
Mongoid ::History . tracker_class_name = nil
11
11
Mongoid ::History . trackable_class_options = nil
12
+ Thread . current [ :mongoid_history_controller ] = nil
12
13
end
13
14
end
You can’t perform that action at this time.
0 commit comments