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)
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)
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)
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
|