Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back fetching modifier from controller #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Lint/HandleExceptions:

# Offense count: 12
Metrics/AbcSize:
Max: 51
Max: 55

# Offense count: 4
Metrics/CyclomaticComplexity:
Expand All @@ -27,12 +27,12 @@ Metrics/LineLength:
# Offense count: 7
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 32
Max: 36

# 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
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ rvm:
- 2.1.1
- 2.0.0
- 1.9.3
- rbx-2.2.10
- rbx-2
- jruby-19mode

matrix:
allow_failures:
- rvm: rbx-2

env:
- MONGOID_VERSION=3
- MONGOID_VERSION=4
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
14 changes: 13 additions & 1 deletion lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def track_history(options = {})

belongs_to_modifier_options = { class_name: Mongoid::History.modifier_class_name }
belongs_to_modifier_options[:inverse_of] = options[:modifier_field_inverse_of] if options.key?(:modifier_field_inverse_of)
belongs_to options[:modifier_field].to_sym, belongs_to_modifier_options
if respond_to? :t_belongs_to
# Tenacity support https://github.com/jwood/tenacity
t_belongs_to options[:modifier_field].to_sym, belongs_to_modifier_options
else
belongs_to options[:modifier_field].to_sym, belongs_to_modifier_options
end

include MyInstanceMethods
extend SingletonMethods
Expand Down Expand Up @@ -238,6 +243,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