Skip to content

Commit

Permalink
allow modifier field to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
yads committed Dec 1, 2017
1 parent 8b835b4 commit 4bebf51
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.7.1 (Next)

* Your contribution here.
* [#205](https://github.com/mongoid/mongoid-history/pull/205): Allow modifier field to be optional - [@yads](https://github.com/yads).

### 0.7.0 (2017/11/14)

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class Post
# telling Mongoid::History how you want to track changes
# dynamic fields will be tracked automatically (for MongoId 4.0+ you should include Mongoid::Attributes::Dynamic to your model)
track_history :on => [:title, :body], # track title and body fields only, default is :all
:modifier_field => :modifier, # adds "belongs_to :modifier" to track who made the change, default is :modifier
:modifier_field => :modifier, # adds "belongs_to :modifier" to track who made the change, default is :modifier, set to nil to not create modifier_field
:modifier_field_inverse_of => :nil, # adds an ":inverse_of" option to the "belongs_to :modifier" relation, default is not set
:modifier_field_optional => true, # marks the modifier relationship as optional
:version_field => :version, # adds "field :version, :type => Integer" to track current version, default is :version
:track_create => false, # track document creation, default is false
:track_update => true, # track document updates, default is true
Expand Down
19 changes: 14 additions & 5 deletions lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ def track_history(options = {})

field options[:version_field].to_sym, type: Integer

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
unless options[:modifier_field].nil?
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_modifier_options[:optional] = true if options[:modifier_field_optional]
belongs_to options[:modifier_field].to_sym, belongs_to_modifier_options
end

include MyInstanceMethods
extend SingletonMethods
Expand Down Expand Up @@ -219,11 +222,12 @@ def modified_attributes_for_destroy
def history_tracker_attributes(action)
return @history_tracker_attributes if @history_tracker_attributes

modifier_field = history_trackable_options[:modifier_field]
@history_tracker_attributes = {
association_chain: traverse_association_chain,
scope: related_scope,
modifier: send(history_trackable_options[:modifier_field])
}
@history_tracker_attributes[:modifier] = send(modifier_field) if modifier_field

original, modified = transform_changes(modified_attributes_for_action(action))

Expand Down Expand Up @@ -425,7 +429,12 @@ def tracked_fields
#
# @return [ Array < String > ] the list of reserved database field names
def reserved_tracked_fields
@reserved_tracked_fields ||= ['_id', history_trackable_options[:version_field].to_s, "#{history_trackable_options[:modifier_field]}_id"]
@reserved_tracked_fields ||= begin
fields = ['_id', history_trackable_options[:version_field].to_s]
modifier_field = history_trackable_options[:modifier_field]
fields << "#{modifier_field}_id" if modifier_field
fields
end
end

def field_formats
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/history/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def undo_attr(modifier)
undo_hash = affected.easy_unmerge(modified)
undo_hash.easy_merge!(original)
modifier_field = trackable.history_trackable_options[:modifier_field]
undo_hash[modifier_field] = modifier
undo_hash[modifier_field] = modifier if modifier_field
(modified.keys - undo_hash.keys).each do |k|
undo_hash[k] = nil
end
Expand All @@ -61,7 +61,7 @@ def redo_attr(modifier)
redo_hash = affected.easy_unmerge(original)
redo_hash.easy_merge!(modified)
modifier_field = trackable.history_trackable_options[:modifier_field]
redo_hash[modifier_field] = modifier
redo_hash[modifier_field] = modifier if modifier_field
localize_keys(redo_hash)
end

Expand Down
17 changes: 17 additions & 0 deletions spec/unit/trackable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ class MyModel
field :foo
end

class MyModelWithNoModifier
include Mongoid::Document
include Mongoid::History::Trackable
field :foo
end

class MyDynamicModel
include Mongoid::Document
include Mongoid::History::Trackable
Expand All @@ -31,6 +37,7 @@ class HistoryTracker
before :all do
MyModel.track_history
@persisted_history_options = Mongoid::History.trackable_class_options
MyModelWithNoModifier.track_history modifier_field: nil
end
before(:each) { Mongoid::History.trackable_class_options = @persisted_history_options }
let(:expected_option) do
Expand Down Expand Up @@ -72,6 +79,12 @@ class HistoryTracker
expect(MyModel.history_trackable_options).to eq(expected_option)
end

context 'modifier_field set to nil' do
it 'should not have a modifier relationship' do
expect(MyModelWithNoModifier.reflect_on_association(:modifier)).to be_nil
end
end

describe '#tracked_fields' do
it 'should return the tracked field list' do
expect(MyModel.tracked_fields).to eq(regular_fields)
Expand All @@ -82,6 +95,10 @@ class HistoryTracker
it 'should return the protected field list' do
expect(MyModel.reserved_tracked_fields).to eq(reserved_fields)
end

it 'should not include modifier_field if not specified' do
expect(MyModelWithNoModifier.reserved_tracked_fields).not_to include('modifier')
end
end

describe '#tracked_fields_for_action' do
Expand Down

0 comments on commit 4bebf51

Please sign in to comment.