Skip to content

Skip track without modifier #140

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

Open
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Gemfile.lock

.rvmrc
.idea

# rcov generated
coverage
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Post
:modifier_field => :modifier, # adds "belongs_to :modifier" to track who made the change, default is :modifier
:modifier_field_inverse_of => :nil, # adds an ":inverse_of" option to the "belongs_to :modifier" relation, default is not set
:version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
:track_without_modifier => true # save tracks when modifier is not set
:track_create => false, # track document creation, default is false
:track_update => true, # track document updates, default is true
:track_destroy => false # track document destruction, default is false
Expand Down Expand Up @@ -263,6 +264,27 @@ Or perhaps you are namespacing to a module:
Mongoid::History.modifier_class_name = 'CMS::Author'
```

**Skipping tracks without modifier set**

If you don't want to save tracks when modifier for a model is not set, you can set `track_without_modifier` option to `false` either globally:

```ruby
Mongoid::History.track_without_modifier = false
```

or per-model:

```ruby
class Post
include Mongoid::Document
include Mongoid::History::Trackable
field :title
field :body

track_history :on => [:title, :body], :track_without_modifier => false
end
```

**Using an alternate changes method**

Sometimes you may wish to provide an alternate method for determining which changes should be tracked. For example, if you are using embedded documents
Expand Down
3 changes: 3 additions & 0 deletions lib/mongoid/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module History
mattr_accessor :trackable_class_options
mattr_accessor :modifier_class_name
mattr_accessor :current_user_method
mattr_accessor :track_without_modifier

def self.tracker_class
@tracker_class ||= tracker_class_name.to_s.classify.constantize
Expand All @@ -34,3 +35,5 @@ def self.enabled?
Mongoid::History.modifier_class_name = 'User'
Mongoid::History.trackable_class_options = {}
Mongoid::History.current_user_method ||= :current_user
Mongoid::History.track_without_modifier = true

5 changes: 4 additions & 1 deletion lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def track_history(options = {})
version_field: :version,
changes_method: :changes,
scope: scope_name,
track_without_modifier: Mongoid::History.track_without_modifier,
track_create: false,
track_update: true,
track_destroy: false
Expand Down Expand Up @@ -276,7 +277,9 @@ def transform_changes(changes)
protected

def track_history_for_action?(action)
track_history? && !(action.to_sym == :update && modified_attributes_for_update.blank?)
track_history? &&
!(action.to_sym == :update && modified_attributes_for_update.blank?) &&
(history_trackable_options[:track_without_modifier] || !!send(history_trackable_options[:modifier_field]))
end

def track_history_for_action(action)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/history/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Tracker
field :version, type: Integer
field :action, type: String
field :scope, type: String
belongs_to :modifier, class_name: Mongoid::History.modifier_class_name
belongs_to :modifier, class_name: Mongoid::History.modifier_class_name

index(scope: 1)
index(association_chain: 1)
Expand Down