Module: Mongoid::History::Trackable::ClassMethods

Defined in:
lib/mongoid/history/trackable.rb

Instance Method Summary collapse

Instance Method Details

#disable_tracking(&block) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/mongoid/history/trackable.rb', line 65

def disable_tracking(&block)
  begin
    Thread.current[track_history_flag] = false
    yield
  ensure
    Thread.current[track_history_flag] = true
  end
end

#track_history(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mongoid/history/trackable.rb', line 6

def track_history(options={})
  scope_name = self.collection_name.to_s.singularize.to_sym
  default_options = {
    :on                  =>  :all,
    :except              =>  [:created_at, :updated_at],
    :except_but_remember => [],
    :modifier_field      =>  :modifier,
    :version_field       =>  :version,
    :scope               =>  scope_name,
    :remember_attachment =>  true,
    :track_create        =>  false,
    :track_update        =>  true,
    :track_destroy       =>  false,
  }

  options = default_options.merge(options)

  # normalize except fields
  # manually ensure _id, id, version will not be tracked in history
  options[:except] = [options[:except]] unless options[:except].is_a? Array
  options[:except] << options[:version_field]
  options[:except] << "#{options[:modifier_field]}_id".to_sym
  options[:except] += [:_id, :id, :updated_at]
  options[:except] = options[:except].map(&:to_s).flatten.compact.uniq
  options[:except].map(&:to_s)

  # normalize except_but_remember fields
  options[:except_but_remember] = [options[:except_but_remember]] unless options[:except_but_remember].is_a? Array
  options[:except_but_remember] = options[:except_but_remember].map(&:to_s).flatten.compact.uniq
  options[:except_but_remember].map(&:to_s)

  # normalize fields to track to either :all or an array of strings
  if options[:on] != :all
    options[:on] = [options[:on]] unless options[:on].is_a? Array
    options[:on] = options[:on].map(&:to_s).flatten.uniq
  end

  field options[:version_field].to_sym, :type => Integer
  belongs_to options[:modifier_field].to_sym, :class_name => Mongoid::History.modifier_class_name

  include MyInstanceMethods
  extend SingletonMethods

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

  before_update :track_update if options[:track_update]
  before_create :track_create if options[:track_create]
  before_destroy :track_destroy if options[:track_destroy]

  Mongoid::History.trackable_class_options ||= {}
  Mongoid::History.trackable_class_options[scope_name] = options
end

#track_history?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/mongoid/history/trackable.rb', line 60

def track_history?
  enabled = Thread.current[track_history_flag]
  enabled.nil? ? true : enabled
end

#track_history_flagObject



74
75
76
# File 'lib/mongoid/history/trackable.rb', line 74

def track_history_flag
  "mongoid_history_#{self.name.underscore}_trackable_enabled".to_sym
end