Skip to content

Commit 8556240

Browse files
committed
Back fetching modifier from controller
1 parent 6948018 commit 8556240

File tree

9 files changed

+76
-2
lines changed

9 files changed

+76
-2
lines changed

Diff for: .rubocop_todo.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Metrics/MethodLength:
3232
# Offense count: 2
3333
# Configuration parameters: CountComments.
3434
Metrics/ModuleLength:
35-
Max: 168
35+
Max: 174
3636

3737
# Offense count: 4
3838
Metrics/PerceivedComplexity:
@@ -42,6 +42,7 @@ Metrics/PerceivedComplexity:
4242
Style/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'

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
0.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

67
0.5.0 (2015/09/18)

Diff for: README.md

+34
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ The following example sets the tracker class name using a Rails initializer.
4747
Mongoid::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

Diff for: lib/mongoid/history.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'mongoid/history/version'
44
require 'mongoid/history/tracker'
55
require 'mongoid/history/trackable'
6+
require 'mongoid/history/hooks'
67

78
module Mongoid
89
module History

Diff for: lib/mongoid/history/hooks.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

Diff for: lib/mongoid/history/trackable.rb

+7
Original file line numberDiff line numberDiff 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

Diff for: lib/mongoid/history/tracker.rb

+6-1
Original file line numberDiff line numberDiff 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)

Diff for: spec/integration/integration_spec.rb

+11
Original file line numberDiff line numberDiff 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

Diff for: spec/support/mongoid_history.rb

+1
Original file line numberDiff line numberDiff 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
1314
end

0 commit comments

Comments
 (0)