Module: Mongoid::History

Defined in:
lib/mongoid/history.rb,
lib/mongoid/history/options.rb,
lib/mongoid/history/tracker.rb,
lib/mongoid/history/version.rb,
lib/mongoid/history/trackable.rb,
lib/mongoid/history/attributes/base.rb,
lib/mongoid/history/attributes/create.rb,
lib/mongoid/history/attributes/update.rb,
lib/mongoid/history/attributes/destroy.rb

Defined Under Namespace

Modules: Attributes, Trackable, Tracker Classes: Options

Constant Summary collapse

GLOBAL_TRACK_HISTORY_FLAG =
'mongoid_history_trackable_enabled'.freeze
VERSION =
'0.8.3'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_user_methodObject

Returns the value of attribute current_user_method


20
21
22
# File 'lib/mongoid/history.rb', line 20

def current_user_method
  @current_user_method
end

.modifier_class_nameObject

Returns the value of attribute modifier_class_name


19
20
21
# File 'lib/mongoid/history.rb', line 19

def modifier_class_name
  @modifier_class_name
end

.trackable_settingsObject

Returns the value of attribute trackable_settings


18
19
20
# File 'lib/mongoid/history.rb', line 18

def trackable_settings
  @trackable_settings
end

.tracker_class_nameObject

Returns the value of attribute tracker_class_name


17
18
19
# File 'lib/mongoid/history.rb', line 17

def tracker_class_name
  @tracker_class_name
end

Class Method Details

.default_settingsObject


49
50
51
# File 'lib/mongoid/history.rb', line 49

def default_settings
  @default_settings ||= { paranoia_field: 'deleted_at' }
end

.disableObject Also known as: disable!


22
23
24
25
26
27
28
# File 'lib/mongoid/history.rb', line 22

def disable
  original_flag = store[GLOBAL_TRACK_HISTORY_FLAG]
  store[GLOBAL_TRACK_HISTORY_FLAG] = false
  yield if block_given?
ensure
  store[GLOBAL_TRACK_HISTORY_FLAG] = original_flag if block_given?
end

.enableObject Also known as: enable!


30
31
32
33
34
35
36
# File 'lib/mongoid/history.rb', line 30

def enable
  original_flag = store[GLOBAL_TRACK_HISTORY_FLAG]
  store[GLOBAL_TRACK_HISTORY_FLAG] = true
  yield if block_given?
ensure
  store[GLOBAL_TRACK_HISTORY_FLAG] = original_flag if block_given?
end

.enabled?Boolean

Returns:

  • (Boolean)

41
42
43
# File 'lib/mongoid/history.rb', line 41

def enabled?
  store[GLOBAL_TRACK_HISTORY_FLAG] != false
end

.reset!Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongoid/history.rb', line 57

def reset!
  Mongoid::History.modifier_class_name = 'User'
  Mongoid::History.trackable_settings = {}
  Mongoid::History.current_user_method ||= :current_user

  Mongoid.models.each do |model|
    next unless model.included_modules.include? Mongoid::History::Trackable

    model.singleton_class.class_eval do
      # Inverse of class_attribute
      %i[mongoid_history_options
         mongoid_history_options=
         mongoid_history_options?].each { |m| remove_possible_method(m) }
    end
  end
end

.storeObject


45
46
47
# File 'lib/mongoid/history.rb', line 45

def store
  defined?(RequestStore) ? RequestStore.store : Thread.current
end

.trackable_class_settings(trackable_class) ⇒ Object


53
54
55
# File 'lib/mongoid/history.rb', line 53

def trackable_class_settings(trackable_class)
  trackable_settings[trackable_class.name.to_sym] || default_settings
end