Module: Current
- Defined in:
- lib/rails_current.rb,
lib/rails_current.rb,
lib/rails_current.rb
Defined Under Namespace
Classes: BlankSlate, Proxy
Constant Summary
collapse
- VERSION =
'2.2.0'
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
154
155
156
157
158
159
160
161
162
|
# File 'lib/rails_current.rb', line 154
def method_missing(method, *args, &block)
case method.to_s
when /^current_(.*)$/
msg = $1
Current.send(msg, *args, &block)
else
super
end
end
|
Class Method Details
.attribute(name, *args, &block) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/rails_current.rb', line 51
def Current.attribute(name, *args, &block)
options = Map.options_for!(args)
name = name.to_s
attribute_names.push(name)
attribute_names.uniq!
if options.has_key?(:default)
default = options[:default]
if default.respond_to?(:call)
block ||= default
else
data[name] = default
end
end
if !args.empty?
value = args.shift
data[name] = value
end
if block
generators[name] = block
end
define_attribute_method(name)
self
end
|
.attribute?(name) ⇒ Boolean
120
121
122
|
# File 'lib/rails_current.rb', line 120
def Current.attribute?(name)
attribute_names.include?(name.to_s)
end
|
.attribute_names ⇒ Object
124
125
126
|
# File 'lib/rails_current.rb', line 124
def Current.attribute_names
@attribute_names ||= []
end
|
.attributes ⇒ Object
128
129
130
|
# File 'lib/rails_current.rb', line 128
def Current.attributes
attribute_names.inject(Map.new){|map, name| map.update(name => send(name))}
end
|
.clear ⇒ Object
35
36
37
38
|
# File 'lib/rails_current.rb', line 35
def Current.clear
data.clear
self
end
|
.data ⇒ Object
31
32
33
|
# File 'lib/rails_current.rb', line 31
def Current.data
Thread.current[:current] ||= Map.new
end
|
.define_attribute_method(name) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/rails_current.rb', line 82
def Current.define_attribute_method(name)
name = name.to_s
unless respond_to?(name)
singleton_class.module_eval do
define_method(name) do
value = data[name]
if value.nil? and generator = generators[name]
value = generator.call
data[name] = value
end
value
end
define_method(name + '=') do |value|
data[name] = value
end
define_method(name + '?') do |*args|
send(name)
end
end
end
end
|
.dependencies ⇒ Object
10
11
12
13
14
|
# File 'lib/rails_current.rb', line 10
def Current.dependencies
{
'map' => [ 'map', ' ~> 6.0' ]
}
end
|
.description ⇒ Object
16
17
18
|
# File 'lib/rails_current.rb', line 16
def Current.description
"track 'current_user' et all in a tidy, global, and thread-safe fashion for your rails apps"
end
|
.ensure_rails_application(&block) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/rails_current.rb', line 212
def Current.ensure_rails_application(&block)
require 'rails' unless defined?(Rails)
if Rails.application.nil?
mock = Class.new(Rails::Application)
Rails.application = mock.instance
if defined?(Rails.application.config.secret_key_base)
Rails.application.config.secret_key_base = '42'
end
begin
block.call()
ensure
Rails.application = nil
end
else
block.call()
end
end
|
.generators ⇒ Object
47
48
49
|
# File 'lib/rails_current.rb', line 47
def Current.generators
@generators ||= Map.new
end
|
.install_before_action! ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
# File 'lib/rails_current.rb', line 262
def Current.install_before_action!
if defined?(::ActionController::Base)
::ActionController::Base.module_eval do
prepend_before_action do |controller|
Current.clear
Current.controller = Current.proxy_for(controller)
Current.action = controller ? controller.send(:action_name) : nil
end
extend Current
include Current
helper{ include Current }
end
end
end
|
.method_missing(method, *args, &block) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/rails_current.rb', line 132
def Current.method_missing(method, *args, &block)
case method.to_s
when /^(.*)[=]$/
name = $1
value = args.shift
attribute(name, value)
value
when /^(.*)[?]$/
nil
else
if block
name = method.to_s
attribute(name, &block)
block
else
nil
end
end
end
|
.mock_controller(options = {}) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/rails_current.rb', line 164
def Current.mock_controller(options = {})
ensure_rails_application do
require 'action_controller'
require 'action_dispatch/testing/test_request.rb'
require 'action_dispatch/testing/test_response.rb'
store = ActiveSupport::Cache::MemoryStore.new
controller = mock_controller_class.new
controller.perform_caching = true
controller.cache_store = store
request = ActionDispatch::TestRequest.create
response = ActionDispatch::TestResponse.create
controller.request = request
controller.response = response
singleton_class =
class << controller
self
end
singleton_class.module_eval do
define_method(:default_url_options) do
@default_url_options ||= (
defined?(DefaultUrlOptions) ? DefaultUrlOptions.dup : {}
)
end
end
Current.proxy_for(controller)
end
end
|
.mock_controller_class ⇒ Object
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/rails_current.rb', line 199
def Current.mock_controller_class
unless const_defined?(:Controller)
controller_class =
if defined?(::ApplicationController)
Class.new(::ApplicationController)
else
Class.new(::ActionController::Base)
end
const_set(:Controller, controller_class)
end
return const_get(:Controller)
end
|
.proxy_for(object) ⇒ Object
244
245
246
|
# File 'lib/rails_current.rb', line 244
def Current.proxy_for(object)
Proxy.new(object)
end
|
.reset ⇒ Object
40
41
42
43
44
45
|
# File 'lib/rails_current.rb', line 40
def Current.reset
attribute_names.each{|name| undefine_attribute_method(name)}
attribute_names.clear
generators.clear
clear
end
|
.singleton_class ⇒ Object
116
117
118
|
# File 'lib/rails_current.rb', line 116
def Current.singleton_class
@singleton_class ||= class << self; self; end
end
|
.undefine_attribute_method(name) ⇒ Object
106
107
108
109
110
111
112
113
114
|
# File 'lib/rails_current.rb', line 106
def Current.undefine_attribute_method(name)
if respond_to?(name)
singleton_class.module_eval do
remove_method(name)
remove_method(name + '=')
remove_method(name + '?')
end
end
end
|
.version ⇒ Object
6
7
8
|
# File 'lib/rails_current.rb', line 6
def Current.version
VERSION
end
|