Skip to content

Commit

Permalink
Re-parse options at end of class definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikwat committed Nov 28, 2017
1 parent 8b835b4 commit bcea418
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/mongoid/history/trackable.rb
Expand Up @@ -3,8 +3,26 @@ module History
module Trackable
extend ActiveSupport::Concern

def self.included(mod)
# re-parse options at end of class definition
TracePoint.trace(:end) do |tp|
if mod == tp.self && mod.track_history_options
options_parser = Mongoid::History::Options.new(mod)
options = options_parser.parse(mod.track_history_options)
Mongoid::History.trackable_class_options[options_parser.scope] = options
end
end
end

module ClassMethods
def track_history_options
@track_history_options
end

def track_history(options = {})
# save options so they can be re-parsed at end of class definition
@track_history_options = options

extend EmbeddedMethods

options_parser = Mongoid::History::Options.new(self)
Expand Down
18 changes: 18 additions & 0 deletions spec/integration/track_history_order_spec.rb
@@ -0,0 +1,18 @@
require 'spec_helper'

describe Mongoid::History::Tracker do
before :all do
class SpecModel
include Mongoid::Document
include Mongoid::History::Trackable

track_history on: :fields

field :foo
end
end

it 'should track all fields when field added after track_history' do
expect(SpecModel.tracked?(:foo)).to be true
end
end

0 comments on commit bcea418

Please sign in to comment.