Skip to content

Commit

Permalink
Merge pull request #208 from dblock/reparse-options
Browse files Browse the repository at this point in the history
Fix #174: defer preparing of history options.
  • Loading branch information
dblock authored Dec 4, 2017
2 parents 8b835b4 + c499de5 commit d6102a1
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 31 deletions.
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-04-09 18:25:15 -0400 using RuboCop version 0.48.1.
# on 2017-12-04 13:08:29 -0500 using RuboCop version 0.48.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -27,27 +27,27 @@ Lint/ParenthesesAsGroupedExpression:
Metrics/AbcSize:
Max: 50

# Offense count: 100
# Offense count: 104
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 801

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 124
Max: 128

# Offense count: 4
Metrics/CyclomaticComplexity:
Max: 10

# Offense count: 435
# Offense count: 448
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 688

# Offense count: 13
# Offense count: 14
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 23
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.7.1 (Next)

* [#208](https://github.com/mongoid/mongoid-history/pull/208): Fix: history tracks fields declared after `track_history` - [@mikwat](https://github.com/mikwat).
* Your contribution here.

### 0.7.0 (2017/11/14)
Expand Down
22 changes: 13 additions & 9 deletions lib/mongoid/history/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@ module History
class Options
attr_reader :trackable, :options

def initialize(trackable)
def initialize(trackable, opts = {})
@trackable = trackable
@options = default_options.merge(opts)
end

def scope
trackable.collection_name.to_s.singularize.to_sym
end

def parse(options = {})
@options = default_options.merge(options)
prepare_skipped_fields
prepare_formatted_fields
parse_tracked_fields_and_relations
@options
def prepared
@prepared ||= begin
prepare_skipped_fields
prepare_formatted_fields
parse_tracked_fields_and_relations
options
end
end

private

def default_options
@default_options ||=
{ on: :all,
{
on: :all,
except: %i[created_at updated_at],
tracker_class_name: nil,
modifier_field: :modifier,
Expand All @@ -33,7 +36,8 @@ def default_options
track_create: false,
track_update: true,
track_destroy: false,
format: nil }
format: nil
}
end

# Sets the :except attributes and relations in `options` to be an [ Array <String> ]
Expand Down
19 changes: 9 additions & 10 deletions lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@ module ClassMethods
def track_history(options = {})
extend EmbeddedMethods

options_parser = Mongoid::History::Options.new(self)
options = options_parser.parse(options)
history_options = Mongoid::History::Options.new(self, options)

field options[:version_field].to_sym, type: Integer
field history_options.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
belongs_to_modifier_options[:inverse_of] = options[:modifier_field_inverse_of] if history_options.options.key?(:modifier_field_inverse_of)
belongs_to history_options.options[:modifier_field].to_sym, belongs_to_modifier_options

include MyInstanceMethods
extend SingletonMethods

delegate :history_trackable_options, to: 'self.class'
delegate :track_history?, to: 'self.class'

around_update :track_update if options[:track_update]
around_create :track_create if options[:track_create]
around_destroy :track_destroy if options[:track_destroy]
around_update :track_update if history_options.options[:track_update]
around_create :track_create if history_options.options[:track_create]
around_destroy :track_destroy if history_options.options[:track_destroy]

Mongoid::History.trackable_class_options ||= {}
Mongoid::History.trackable_class_options[options_parser.scope] = options
Mongoid::History.trackable_class_options[history_options.scope] = history_options
end

def history_settings(options = {})
Expand Down Expand Up @@ -494,7 +493,7 @@ def trackable_scope
end

def history_trackable_options
@history_trackable_options ||= Mongoid::History.trackable_class_options[trackable_scope]
@history_trackable_options ||= Mongoid::History.trackable_class_options[trackable_scope].prepared
end

def clear_trackable_memoization
Expand Down
52 changes: 52 additions & 0 deletions spec/integration/track_history_order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'spec_helper'

describe Mongoid::History::Tracker do
it 'should not track fields when track_history not called' do
class NotModel
include Mongoid::Document
include Mongoid::History::Trackable

field :foo
end

expect(NotModel.respond_to?(:tracked?)).to be false
end

it 'should track fields when track_history called inside class and before fields' do
class InsideBeforeModel
include Mongoid::Document
include Mongoid::History::Trackable

track_history on: :fields

field :foo
end

expect(InsideBeforeModel.tracked?(:foo)).to be true
end

it 'should track fields when track_history called inside class and after fields' do
class InsideAfterModel
include Mongoid::Document
include Mongoid::History::Trackable

field :foo

track_history on: :fields
end

expect(InsideAfterModel.tracked?(:foo)).to be true
end

it 'should track fields when track_history called outside class' do
class OutsideModel
include Mongoid::Document
include Mongoid::History::Trackable

field :foo
end

OutsideModel.track_history on: :fields
expect(OutsideModel.tracked?(:foo)).to be true
end
end
11 changes: 6 additions & 5 deletions spec/unit/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
end
end

let(:service) { described_class.new(ModelOne) }
let(:options) { {} }
let(:service) { described_class.new(ModelOne, options) }

subject { service }

Expand Down Expand Up @@ -77,7 +78,7 @@

describe '#prepare_skipped_fields' do
let(:options) { { except: value } }
subject { service.parse(options) }
subject { service.prepared }

context 'with field' do
let(:value) { :foo }
Expand Down Expand Up @@ -107,7 +108,7 @@

describe '#prepare_formatted_fields' do
let(:options) { { format: value } }
subject { service.parse(options) }
subject { service.prepared }

context 'with non-hash' do
let(:value) { :foo }
Expand Down Expand Up @@ -143,11 +144,11 @@
relations: { embeds_one: {}, embeds_many: {} },
format: {} }
end
it { expect(service.parse).to eq expected_options }
it { expect(service.prepared).to eq expected_options }
end

context 'when options passed' do
subject { service.parse(options) }
subject { service.prepared }

describe '@options' do
let(:options) { { on: value } }
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/trackable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class HistoryTracker
let(:reserved_fields) { %w[_id version modifier_id] }

it 'should have default options' do
expect(Mongoid::History.trackable_class_options[:my_model]).to eq(expected_option)
expect(Mongoid::History.trackable_class_options[:my_model].prepared).to eq(expected_option)
end

it 'should define callback function #track_update' do
Expand Down Expand Up @@ -191,7 +191,7 @@ class MySubModel < MyModel
end

it 'should have default options' do
expect(Mongoid::History.trackable_class_options[:my_model]).to eq(expected_option)
expect(Mongoid::History.trackable_class_options[:my_model].prepared).to eq(expected_option)
end

it 'should define #history_trackable_options' do
Expand Down

0 comments on commit d6102a1

Please sign in to comment.