1
1
# frozen_string_literal: true
2
2
3
- require "request_store "
3
+ require "paper_trail/request/current_attributes "
4
4
5
5
module PaperTrail
6
6
# Manages variables that affect the current HTTP request, such as `whodunnit`.
@@ -20,9 +20,7 @@ class << self
20
20
# PaperTrail.request.controller_info # => { ip: '127.0.0.1' }
21
21
#
22
22
# @api public
23
- def controller_info = ( value )
24
- store [ :controller_info ] = value
25
- end
23
+ delegate :controller_info= , to : :current_attributes
26
24
27
25
# Returns the data from the controller that you want PaperTrail to store.
28
26
# See also `PaperTrail::Rails::Controller#info_for_paper_trail`.
@@ -31,9 +29,7 @@ def controller_info=(value)
31
29
# PaperTrail.request.controller_info # => { ip: '127.0.0.1' }
32
30
#
33
31
# @api public
34
- def controller_info
35
- store [ :controller_info ]
36
- end
32
+ delegate :controller_info , to : :current_attributes
37
33
38
34
# Switches PaperTrail off for the given model.
39
35
# @api public
@@ -49,30 +45,29 @@ def enable_model(model_class)
49
45
50
46
# Sets whether PaperTrail is enabled or disabled for the current request.
51
47
# @api public
52
- def enabled = ( value )
53
- store [ :enabled ] = value
54
- end
48
+ delegate :enabled= , to : :current_attributes
55
49
56
50
# Returns `true` if PaperTrail is enabled for the request, `false` otherwise.
57
51
# See `PaperTrail::Rails::Controller#paper_trail_enabled_for_controller`.
58
52
# @api public
59
53
def enabled?
60
- !!store [ : enabled]
54
+ !!current_attributes . enabled
61
55
end
62
56
63
57
# Sets whether PaperTrail is enabled or disabled for this model in the
64
58
# current request.
65
59
# @api public
66
60
def enabled_for_model ( model , value )
67
- store [ :"enabled_for_ #{ model } " ] = value
61
+ current_attributes . enabled_for [ model ] = value
68
62
end
69
63
70
64
# Returns `true` if PaperTrail is enabled for this model in the current
71
65
# request, `false` otherwise.
72
66
# @api public
73
67
def enabled_for_model? ( model )
74
68
model . include? ( ::PaperTrail ::Model ::InstanceMethods ) &&
75
- !!store . fetch ( :"enabled_for_#{ model } " , true )
69
+ !!( current_attributes . enabled_for [ model ] ||
70
+ current_attributes . enabled_for [ model ] . nil? )
76
71
end
77
72
78
73
# Temporarily set `options` and execute a block.
@@ -97,15 +92,13 @@ def with(options)
97
92
# inserting a `Version` record.
98
93
#
99
94
# @api public
100
- def whodunnit = ( value )
101
- store [ :whodunnit ] = value
102
- end
95
+ delegate :whodunnit= , to : :current_attributes
103
96
104
- # Returns who is reponsible for any changes that occur during request.
97
+ # Returns who is responsible for any changes that occur during request.
105
98
#
106
99
# @api public
107
100
def whodunnit
108
- who = store [ : whodunnit]
101
+ who = current_attributes . whodunnit
109
102
who . respond_to? ( :call ) ? who . call : who
110
103
end
111
104
@@ -114,31 +107,36 @@ def whodunnit
114
107
# @api private
115
108
def merge ( options )
116
109
options . to_h . each do |k , v |
117
- store [ k ] = v
110
+ if k . start_with? ( "enabled_for_" )
111
+ model_klass = k . to_s . sub ( "enabled_for_" , "" ) . constantize
112
+ current_attributes . enabled_for [ model_klass ] = v
113
+ else
114
+ current_attributes . public_send ( "#{ k } =" , v )
115
+ end
118
116
end
119
117
end
120
118
121
119
# @api private
122
120
def set ( options )
123
- store . clear
121
+ current_attributes . reset
124
122
merge ( options )
125
123
end
126
124
127
- # Returns a Hash, initializing with default values if necessary.
125
+ # Returns the current request attributes with default values initialized if necessary.
128
126
# @api private
129
- def store
130
- RequestStore . store [ :paper_trail ] ||= {
131
- enabled : true
132
- }
127
+ def current_attributes
128
+ CurrentAttributes . tap do | attrs |
129
+ attrs . enabled = true if attrs . enabled . nil?
130
+ end
133
131
end
134
132
135
- # Returns a deep copy of the internal hash from our RequestStore . Keys are
133
+ # Returns a deep copy of the current attributes . Keys are
136
134
# all symbols. Values are mostly primitives, but whodunnit can be a Proc.
137
135
# We cannot use Marshal.dump here because it doesn't support Proc. It is
138
136
# unclear exactly how `deep_dup` handles a Proc, but it doesn't complain.
139
137
# @api private
140
138
def to_h
141
- store . deep_dup
139
+ current_attributes . attributes . deep_dup
142
140
end
143
141
144
142
# Provide a helpful error message if someone has a typo in one of their
0 commit comments