Module: PaperTrail::Request Private

Defined in:
lib/mongo_trails/request.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Manages variables that affect the current HTTP request, such as ‘whodunnit`.

Please do not use ‘PaperTrail::Request` directly, use `PaperTrail.request`. Currently, `Request` is a `Module`, but in the future it is quite possible we may make it a `Class`. If we make such a choice, we will not provide any warning and will not treat it as a breaking change. You’ve been warned :)

Defined Under Namespace

Classes: InvalidOption

Class Method Summary collapse

Class Method Details

.controller_infoObject

Returns the data from the controller that you want PaperTrail to store. See also ‘PaperTrail::Rails::Controller#info_for_paper_trail`.

PaperTrail.request.controller_info = { ip: request_user_ip }
PaperTrail.request.controller_info # => { ip: '127.0.0.1' }


37
38
39
# File 'lib/mongo_trails/request.rb', line 37

def controller_info
  store[:controller_info]
end

.controller_info=(value) ⇒ Object

Sets any data from the controller that you want PaperTrail to store. See also ‘PaperTrail::Rails::Controller#info_for_paper_trail`.

PaperTrail.request.controller_info = { ip: request_user_ip }
PaperTrail.request.controller_info # => { ip: '127.0.0.1' }


26
27
28
# File 'lib/mongo_trails/request.rb', line 26

def controller_info=(value)
  store[:controller_info] = value
end

.disable_model(model_class) ⇒ Object

Switches PaperTrail off for the given model.



43
44
45
# File 'lib/mongo_trails/request.rb', line 43

def disable_model(model_class)
  enabled_for_model(model_class, false)
end

.enable_model(model_class) ⇒ Object

Switches PaperTrail on for the given model.



49
50
51
# File 'lib/mongo_trails/request.rb', line 49

def enable_model(model_class)
  enabled_for_model(model_class, true)
end

.enabled=(value) ⇒ Object

Sets whether PaperTrail is enabled or disabled for the current request.



55
56
57
# File 'lib/mongo_trails/request.rb', line 55

def enabled=(value)
  store[:enabled] = value
end

.enabled?Boolean

Returns ‘true` if PaperTrail is enabled for the request, `false` otherwise. See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.

Returns:

  • (Boolean)


62
63
64
# File 'lib/mongo_trails/request.rb', line 62

def enabled?
  !!store[:enabled]
end

.enabled_for_model(model, value) ⇒ Object

Sets whether PaperTrail is enabled or disabled for this model in the current request.



69
70
71
# File 'lib/mongo_trails/request.rb', line 69

def enabled_for_model(model, value)
  store[:"enabled_for_#{model}"] = value
end

.enabled_for_model?(model) ⇒ Boolean

Returns ‘true` if PaperTrail is enabled for this model in the current request, `false` otherwise.

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/mongo_trails/request.rb', line 76

def enabled_for_model?(model)
  model.include?(::PaperTrail::Model::InstanceMethods) &&
    !!store.fetch(:"enabled_for_#{model}", true)
end

.merge(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



82
83
84
85
86
# File 'lib/mongo_trails/request.rb', line 82

def merge(options)
  options.to_h.each do |k, v|
    store[k] = v
  end
end

.set(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



89
90
91
92
# File 'lib/mongo_trails/request.rb', line 89

def set(options)
  store.clear
  merge(options)
end

.to_hObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a deep copy of the internal hash from our RequestStore. Keys are all symbols. Values are mostly primitives, but whodunnit can be a Proc. We cannot use Marshal.dump here because it doesn’t support Proc. It is unclear exactly how ‘deep_dup` handles a Proc, but it doesn’t complain.



99
100
101
# File 'lib/mongo_trails/request.rb', line 99

def to_h
  store.deep_dup
end

.whodunnitObject

Returns who is reponsible for any changes that occur during request.



132
133
134
135
# File 'lib/mongo_trails/request.rb', line 132

def whodunnit
  who = store[:whodunnit]
  who.respond_to?(:call) ? who.call : who
end

.whodunnit=(value) ⇒ Object

Sets who is responsible for any changes that occur during request. You would normally use this in a migration or on the console, when working with models directly.

‘value` is usually a string, the name of a person, but you can set anything that responds to `to_s`. You can also set a Proc, which will not be evaluated until `whodunnit` is called later, usually right before inserting a `Version` record.



125
126
127
# File 'lib/mongo_trails/request.rb', line 125

def whodunnit=(value)
  store[:whodunnit] = value
end

.with(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Temporarily set ‘options` and execute a block.



105
106
107
108
109
110
111
112
113
# File 'lib/mongo_trails/request.rb', line 105

def with(options)
  return unless block_given?
  validate_public_options(options)
  before = to_h
  merge(options)
  yield
ensure
  set(before)
end