Module: PaperTrail::Request Private

Defined in:
lib/paper_trail/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 :)

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' }


34
35
36
# File 'lib/paper_trail/request.rb', line 34

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' }


23
24
25
# File 'lib/paper_trail/request.rb', line 23

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

.disable_model(model_class) ⇒ Object

Switches PaperTrail off for the given model.



40
41
42
# File 'lib/paper_trail/request.rb', line 40

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

.enable_model(model_class) ⇒ Object

Switches PaperTrail on for the given model.



46
47
48
# File 'lib/paper_trail/request.rb', line 46

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.



52
53
54
# File 'lib/paper_trail/request.rb', line 52

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)


59
60
61
# File 'lib/paper_trail/request.rb', line 59

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.



66
67
68
# File 'lib/paper_trail/request.rb', line 66

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)


73
74
75
76
# File 'lib/paper_trail/request.rb', line 73

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

.whodunnitObject

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



107
108
109
110
# File 'lib/paper_trail/request.rb', line 107

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.



100
101
102
# File 'lib/paper_trail/request.rb', line 100

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.



80
81
82
83
84
85
86
87
88
# File 'lib/paper_trail/request.rb', line 80

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