Skip to content

Commit

Permalink
Back fetching modifier from controller
Browse files Browse the repository at this point in the history
  • Loading branch information
melnikaite committed Mar 28, 2016
1 parent 6948018 commit d295a99
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Metrics/MethodLength:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 168
Max: 174

# Offense count: 4
Metrics/PerceivedComplexity:
Expand All @@ -42,6 +42,7 @@ Metrics/PerceivedComplexity:
Style/Documentation:
Exclude:
- 'lib/mongoid/history.rb'
- 'lib/mongoid/history/hooks.rb'
- 'lib/mongoid/history/trackable.rb'
- 'lib/mongoid/history/tracker.rb'
- 'lib/mongoid/history/version.rb'
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rvm:
- 2.1.1
- 2.0.0
- 1.9.3
- rbx-2.2.10
- rbx-2
- jruby-19mode

env:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.5.1 (Next)
------------

* [#149](https://github.com/aq1018/mongoid-history/pull/149): Back fetching modifier from controller - [@melnikaite](https://github.com/melnikaite).
* Your contribution here.

0.5.0 (2015/09/18)
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,40 @@ The following example sets the tracker class name using a Rails initializer.
Mongoid::History.tracker_class_name = :history_tracker
```

**Set controller**

Add hooks to ApplicationController to fetch modifier automatically.

```ruby
# app/controllers/application_controller.rb
class ApplicationController
include Mongoid::History::Hooks
end
```

**Set `#current_user` method name**

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.

The following example sets the `current_user_method` using a Rails initializer

```ruby
# config/initializers/mongoid-history.rb
# initializer for mongoid-history
# assuming you're using devise/authlogic
Mongoid::History.current_user_method = :current_user
```

When `current_user_method` is set, mongoid-history will invoke this method on each update and set its result as the instance modifier.

```ruby
# assume that current_user return #<User _id: 1>
post = Post.first
post.update_attributes(:title => 'New title')

post.history_tracks.last.modifier #=> #<User _id: 1>
```

**Create trackable classes and objects**

```ruby
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'mongoid/history/version'
require 'mongoid/history/tracker'
require 'mongoid/history/trackable'
require 'mongoid/history/hooks'

module Mongoid
module History
Expand Down
13 changes: 13 additions & 0 deletions lib/mongoid/history/hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Mongoid
module History
module Hooks
extend ActiveSupport::Concern

included do
before_action do |controller|
Thread.current[:mongoid_history_controller] = controller
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ def history_tracker_attributes(action)
modifier: send(history_trackable_options[:modifier_field])
}

unless @history_tracker_attributes[:modifier]
controller = Thread.current[:mongoid_history_controller]
if controller && controller.respond_to?(Mongoid::History.current_user_method, true)
@history_tracker_attributes[:modifier] = controller.send(Mongoid::History.current_user_method)
end
end

original, modified = transform_changes(modified_attributes_for_action(action))

@history_tracker_attributes[:original] = original
Expand Down
7 changes: 6 additions & 1 deletion lib/mongoid/history/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ module Tracker
field :version, type: Integer
field :action, type: String
field :scope, type: String
belongs_to :modifier, class_name: Mongoid::History.modifier_class_name
if respond_to? :t_belongs_to
# Tenacity support https://github.com/jwood/tenacity
t_belongs_to :modifier, class_name: Mongoid::History.modifier_class_name
else
belongs_to :modifier, class_name: Mongoid::History.modifier_class_name
end

index(scope: 1)
index(association_chain: 1)
Expand Down
11 changes: 11 additions & 0 deletions spec/integration/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class Tag
class Foo < Comment
end

class Controller
end

@persisted_history_options = Mongoid::History.trackable_class_options
end

Expand Down Expand Up @@ -536,6 +539,14 @@ class Foo < Comment
expect(tag_foo.history_tracks.last.association_chain.last['name']).to eq('tags')
expect { tag_foo.history_tracks.last.trackable }.not_to raise_error
end

it 'should save modifier' do
Thread.current[:mongoid_history_controller] = Controller.new
allow_any_instance_of(Controller).to receive(:current_user).and_return(user)
expect(Thread.current[:mongoid_history_controller].current_user).to eq user
expect(tag_foo.history_tracks.last.modifier).to eq user
expect(tag_bar.history_tracks.last.modifier).to eq user
end
end

describe 'non-embedded' do
Expand Down
1 change: 1 addition & 0 deletions spec/support/mongoid_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ class Tracker
config.after :each do
Mongoid::History.tracker_class_name = nil
Mongoid::History.trackable_class_options = nil
Thread.current[:mongoid_history_controller] = nil
end
end

0 comments on commit d295a99

Please sign in to comment.