Module: SnailTrail::Request Private

Defined in:
lib/snail_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` and `performance_mode`.

Please do not use ‘SnailTrail::Request` directly, use `SnailTrail.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

.clear_performance_mode_dataObject

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.



132
133
134
135
136
137
# File 'lib/snail_trail/request.rb', line 132

def clear_performance_mode_data
  self.performance_mode_data =
    if performance_mode?
      []
    end
end

.clear_transaction_idObject

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.



17
18
19
# File 'lib/snail_trail/request.rb', line 17

def clear_transaction_id
  self.transaction_id = nil
end

.controller_infoObject

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

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


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

def controller_info
  store[:controller_info]
end

.controller_info=(value) ⇒ Object

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

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


38
39
40
# File 'lib/snail_trail/request.rb', line 38

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

.disable_model(model_class) ⇒ Object

Switches SnailTrail off for the given model.



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

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

.enable_model(model_class) ⇒ Object

Switches SnailTrail on for the given model.



61
62
63
# File 'lib/snail_trail/request.rb', line 61

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

.enabled=(value) ⇒ Object

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



67
68
69
# File 'lib/snail_trail/request.rb', line 67

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

.enabled?Boolean

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

Returns:

  • (Boolean)


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

def enabled?
  !!store[:enabled]
end

.enabled_for_model(model, value) ⇒ Object

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



81
82
83
# File 'lib/snail_trail/request.rb', line 81

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

.enabled_for_model?(model) ⇒ Boolean

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

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/snail_trail/request.rb', line 88

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

.performance_mode!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.

Enables performance mode for this request



128
129
130
# File 'lib/snail_trail/request.rb', line 128

def performance_mode!
  self.performance_mode = true
end

.performance_mode=(val) ⇒ 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.



139
140
141
142
143
144
145
146
147
148
# File 'lib/snail_trail/request.rb', line 139

def performance_mode= val
  store[:performance_mode] = !!val

  self.performance_mode_data =
    if performance_mode?
      []
    end

  performance_mode?
end

.performance_mode?Boolean

Returns whether or not performance mode is enabled for this request Default: disabled

Returns:

  • (Boolean)


155
156
157
# File 'lib/snail_trail/request.rb', line 155

def performance_mode?
  !!store[:performance_mode]
end

.performance_mode_dataObject

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.



159
160
161
# File 'lib/snail_trail/request.rb', line 159

def performance_mode_data
  store[:performance_mode_data]
end

.transaction_idObject

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.



22
23
24
# File 'lib/snail_trail/request.rb', line 22

def transaction_id
  store[:transaction_id]
end

.transaction_id=(id) ⇒ 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.



27
28
29
# File 'lib/snail_trail/request.rb', line 27

def transaction_id=(id)
  store[:transaction_id] = id
end

.whodunnitObject

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



122
123
124
125
# File 'lib/snail_trail/request.rb', line 122

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.



115
116
117
# File 'lib/snail_trail/request.rb', line 115

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.



95
96
97
98
99
100
101
102
103
# File 'lib/snail_trail/request.rb', line 95

def with(options)
  return unless block_given?
  validate_public_options(options)
  before = to_h
  merge({ performance_mode: false, performance_mode_data: nil }.merge(options))
  yield
ensure
  set(before)
end