File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ Metrics/MethodLength:
3232# Offense count: 2
3333# Configuration parameters: CountComments.
3434Metrics/ModuleLength :
35- Max : 168
35+ Max : 174
3636
3737# Offense count: 4
3838Metrics/PerceivedComplexity :
@@ -42,6 +42,7 @@ Metrics/PerceivedComplexity:
4242Style/Documentation :
4343 Exclude :
4444 - ' lib/mongoid/history.rb'
45+ - ' lib/mongoid/history/hooks.rb'
4546 - ' lib/mongoid/history/trackable.rb'
4647 - ' lib/mongoid/history/tracker.rb'
4748 - ' lib/mongoid/history/version.rb'
Original file line number Diff line number Diff line change 1111 - 2.1.1
1212 - 2.0.0
1313 - 1.9.3
14- - rbx-2.2.10
14+ - rbx-2
1515 - jruby-19mode
1616
1717env :
Original file line number Diff line number Diff line change 110.5.1 (Next)
22------------
33
4+ * [ #149 ] ( https://github.com/aq1018/mongoid-history/pull/149 ) : Back fetching modifier from controller - [ @melnikaite ] ( https://github.com/melnikaite ) .
45* Your contribution here.
56
670.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.
4747Mongoid ::History .tracker_class_name = :history_tracker
4848```
4949
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+
5084** Create trackable classes and objects**
5185
5286``` ruby
Original file line number Diff line number Diff line change 33require 'mongoid/history/version'
44require 'mongoid/history/tracker'
55require 'mongoid/history/trackable'
6+ require 'mongoid/history/hooks'
67
78module Mongoid
89 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)
238238 modifier : send ( history_trackable_options [ :modifier_field ] )
239239 }
240240
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+
241248 original , modified = transform_changes ( modified_attributes_for_action ( action ) )
242249
243250 @history_tracker_attributes [ :original ] = original
Original file line number Diff line number Diff line change @@ -14,7 +14,12 @@ module Tracker
1414 field :version , type : Integer
1515 field :action , type : String
1616 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
1823
1924 index ( scope : 1 )
2025 index ( association_chain : 1 )
Original file line number Diff line number Diff line change @@ -71,6 +71,9 @@ class Tag
7171 class Foo < Comment
7272 end
7373
74+ class Controller
75+ end
76+
7477 @persisted_history_options = Mongoid ::History . trackable_class_options
7578 end
7679
@@ -536,6 +539,14 @@ class Foo < Comment
536539 expect ( tag_foo . history_tracks . last . association_chain . last [ 'name' ] ) . to eq ( 'tags' )
537540 expect { tag_foo . history_tracks . last . trackable } . not_to raise_error
538541 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
539550 end
540551
541552 describe 'non-embedded' do
Original file line number Diff line number Diff line change @@ -9,5 +9,6 @@ class Tracker
99 config . after :each do
1010 Mongoid ::History . tracker_class_name = nil
1111 Mongoid ::History . trackable_class_options = nil
12+ Thread . current [ :mongoid_history_controller ] = nil
1213 end
1314end
You can’t perform that action at this time.
0 commit comments