module Mongoid::History::Trackable::ClassMethods
Public Instance Methods
disable_tracking() { || ... }
click to toggle source
# 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={})
click to toggle source
# 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?()
click to toggle source
# File lib/mongoid/history/trackable.rb, line 60 def track_history? enabled = Thread.current[track_history_flag] enabled.nil? ? true : enabled end
track_history_flag()
click to toggle source
# File lib/mongoid/history/trackable.rb, line 74 def track_history_flag "mongoid_history_#{self.name.underscore}_trackable_enabled".to_sym end