rails_current 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +22 -7
- data/lib/rails_current.rb +56 -23
- data/rails_current.gemspec +1 -1
- data/test/rails_current_test.rb +82 -6
- metadata +2 -2
data/README.md
CHANGED
@@ -13,34 +13,49 @@ DESCRIPTION
|
|
13
13
|
--------------------------------
|
14
14
|
SYNOPSIS
|
15
15
|
--------------------------------
|
16
|
+
|
16
17
|
most rails apps scatter a bunch of @current_foobar vars everywhere. don't do
|
17
18
|
that. it's fugly. instead, do this.
|
18
19
|
|
20
|
+
declare the current_XXX variables you'll want tracked. you can pass a block
|
21
|
+
for lazy computation
|
22
|
+
|
19
23
|
class ApplicationController
|
20
24
|
|
21
25
|
Current(:user){ User.find session[:current_user }
|
22
26
|
Current(:account)
|
23
27
|
|
24
|
-
include Current
|
25
|
-
|
26
28
|
end
|
27
29
|
|
28
|
-
|
30
|
+
you can now access the current state two ways
|
31
|
+
|
29
32
|
|
33
|
+
1) globally from anywhere in your code base
|
34
|
+
|
30
35
|
|
31
|
-
if
|
36
|
+
if Current.user
|
32
37
|
|
33
38
|
...
|
34
39
|
|
35
40
|
end
|
36
41
|
|
42
|
+
Current.user = User.find(id)
|
43
|
+
|
44
|
+
2) or using the current_ methods that are added by including the Current
|
45
|
+
module into any class (ActionController::Base and ActionView::Base
|
46
|
+
automatically include it)
|
47
|
+
|
37
48
|
|
38
|
-
|
49
|
+
if current_user
|
50
|
+
|
51
|
+
...
|
52
|
+
|
53
|
+
end
|
39
54
|
|
55
|
+
self.current_user = User.find(id)
|
40
56
|
|
41
|
-
etc.
|
42
57
|
|
43
|
-
out
|
58
|
+
the Current module is cleared out before every request and is thread safe.
|
44
59
|
|
45
60
|
--------------------------------
|
46
61
|
INSTALL
|
data/lib/rails_current.rb
CHANGED
@@ -4,7 +4,7 @@ require 'map'
|
|
4
4
|
|
5
5
|
module Current
|
6
6
|
def Current.version
|
7
|
-
'1.
|
7
|
+
'1.4.0'
|
8
8
|
end
|
9
9
|
|
10
10
|
def Current.data
|
@@ -17,27 +17,45 @@ module Current
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def Current.reset
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
attribute_names.each{|name| undefine_attribute_method(name)}
|
21
|
+
attribute_names.clear
|
22
|
+
generators.clear
|
23
|
+
clear
|
24
|
+
end
|
25
|
+
|
26
|
+
def Current.generators
|
27
|
+
@generators ||= Map.new
|
24
28
|
end
|
25
29
|
|
26
30
|
def Current.attribute(name, *args, &block)
|
27
|
-
options = Map.options_for(args)
|
31
|
+
options = Map.options_for!(args)
|
28
32
|
|
29
33
|
name = name.to_s
|
30
|
-
default = options.has_key?(:default) ? options[:default] : args.shift
|
31
34
|
|
32
|
-
|
33
|
-
|
35
|
+
attribute_names.push(name)
|
36
|
+
attribute_names.uniq!
|
37
|
+
|
38
|
+
if options.has_key?(:default)
|
39
|
+
default = options[:default]
|
40
|
+
if default.respond_to?(:call)
|
41
|
+
block ||= default
|
42
|
+
else
|
43
|
+
data[name] = default
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
if !args.empty?
|
48
|
+
value = args.shift
|
49
|
+
data[name] = value
|
50
|
+
end
|
51
|
+
|
52
|
+
if block
|
53
|
+
generators[name] = block
|
54
|
+
end
|
34
55
|
|
35
56
|
define_attribute_method(name)
|
36
|
-
self
|
37
|
-
end
|
38
57
|
|
39
|
-
|
40
|
-
@calls ||= Map.new
|
58
|
+
self
|
41
59
|
end
|
42
60
|
|
43
61
|
def Current.define_attribute_method(name)
|
@@ -47,7 +65,10 @@ module Current
|
|
47
65
|
if data.has_key?(name)
|
48
66
|
data[name]
|
49
67
|
else
|
50
|
-
|
68
|
+
if generator = generators[name]
|
69
|
+
value = generator.call
|
70
|
+
data[name] = value
|
71
|
+
end
|
51
72
|
end
|
52
73
|
end
|
53
74
|
|
@@ -77,11 +98,11 @@ module Current
|
|
77
98
|
end
|
78
99
|
|
79
100
|
def Current.attribute?(name)
|
80
|
-
|
101
|
+
attribute_names.include?(name.to_s)
|
81
102
|
end
|
82
103
|
|
83
104
|
def Current.attribute_names
|
84
|
-
|
105
|
+
@attribute_names ||= []
|
85
106
|
end
|
86
107
|
|
87
108
|
def Current.attributes
|
@@ -93,7 +114,7 @@ module Current
|
|
93
114
|
when /^(.*)[=]$/
|
94
115
|
name = $1
|
95
116
|
value = args.shift
|
96
|
-
attribute(name
|
117
|
+
attribute(name, value)
|
97
118
|
value
|
98
119
|
|
99
120
|
when /^(.*)[?]$/
|
@@ -128,20 +149,33 @@ end
|
|
128
149
|
|
129
150
|
if defined?(Rails)
|
130
151
|
|
152
|
+
##
|
153
|
+
#
|
131
154
|
module Current
|
132
155
|
attribute(:controller)
|
133
156
|
attribute(:user)
|
157
|
+
end
|
134
158
|
|
159
|
+
##
|
160
|
+
#
|
161
|
+
module Current
|
135
162
|
def Current.install_before_filter!
|
136
|
-
::ActionController::Base
|
137
|
-
|
138
|
-
|
139
|
-
|
163
|
+
if defined?(::ActionController::Base)
|
164
|
+
::ActionController::Base.module_eval do
|
165
|
+
prepend_before_filter do |controller|
|
166
|
+
Current.clear
|
167
|
+
Current.controller = controller
|
168
|
+
end
|
169
|
+
|
170
|
+
include Current
|
171
|
+
helper{ include Current }
|
140
172
|
end
|
141
|
-
end
|
173
|
+
end
|
142
174
|
end
|
143
175
|
end
|
144
176
|
|
177
|
+
##
|
178
|
+
#
|
145
179
|
if defined?(Rails::Engine)
|
146
180
|
class Engine < Rails::Engine
|
147
181
|
config.before_initialize do
|
@@ -156,7 +190,6 @@ if defined?(Rails)
|
|
156
190
|
|
157
191
|
end
|
158
192
|
|
159
|
-
|
160
193
|
::Rails_current = ::Current
|
161
194
|
|
162
195
|
BEGIN {
|
data/rails_current.gemspec
CHANGED
data/test/rails_current_test.rb
CHANGED
@@ -122,6 +122,21 @@ Testing Current do
|
|
122
122
|
assert{ Current.bar == 'forty-two' }
|
123
123
|
end
|
124
124
|
|
125
|
+
##
|
126
|
+
#
|
127
|
+
test 'that dynamically added data clears cleverly' do
|
128
|
+
assert{ Current.foo = 42 }
|
129
|
+
assert{ Current.bar{ 42.0 } }
|
130
|
+
|
131
|
+
assert{ Current.foo == 42 }
|
132
|
+
assert{ Current.bar == 42.0 }
|
133
|
+
|
134
|
+
assert{ Current.clear }
|
135
|
+
|
136
|
+
assert{ Current.foo == nil }
|
137
|
+
assert{ Current.bar == 42.0 }
|
138
|
+
end
|
139
|
+
|
125
140
|
##
|
126
141
|
#
|
127
142
|
test 'that query methods on Current werky' do
|
@@ -132,21 +147,48 @@ Testing Current do
|
|
132
147
|
|
133
148
|
##
|
134
149
|
#
|
135
|
-
test 'that loading Current into
|
150
|
+
test 'that loading Current into an old skool rails app creates Current.user and Current.controller' do
|
136
151
|
mock_rails! do
|
137
152
|
assert{ Current.attributes =~ {:user => nil, :controller => nil} }
|
153
|
+
|
154
|
+
assert do
|
155
|
+
Current.user = :user
|
156
|
+
Current.controller = :controller
|
157
|
+
end
|
158
|
+
|
159
|
+
assert{ ActionController::Base.new.current_user == :user }
|
160
|
+
assert{ ActionController::Base.new.current_controller == :controller }
|
161
|
+
|
162
|
+
assert{ ActionView::Base.new.current_user == :user }
|
163
|
+
assert{ ActionView::Base.new.current_controller == :controller }
|
138
164
|
end
|
165
|
+
end
|
139
166
|
|
167
|
+
##
|
168
|
+
#
|
169
|
+
test 'that loading Current into a new skool rails app creates Current.user and Current.controller' do
|
140
170
|
mock_rails_engine! do
|
141
171
|
assert{ Current.attributes =~ {:user => nil, :controller => nil} }
|
172
|
+
|
142
173
|
assert{ $before_initialize_called }
|
143
|
-
assert{ $
|
174
|
+
assert{ $prepend_before_filter_called }
|
175
|
+
|
176
|
+
assert do
|
177
|
+
Current.user = :user
|
178
|
+
Current.controller = :controller
|
179
|
+
end
|
180
|
+
|
181
|
+
assert{ ActionController::Base.new.current_user == :user }
|
182
|
+
assert{ ActionController::Base.new.current_controller == :controller }
|
183
|
+
|
184
|
+
assert{ ActionView::Base.new.current_user == :user }
|
185
|
+
assert{ ActionView::Base.new.current_controller == :controller }
|
144
186
|
end
|
145
187
|
end
|
146
188
|
|
147
189
|
##
|
148
190
|
#
|
149
|
-
|
191
|
+
setup do
|
150
192
|
assert{ Current.reset }
|
151
193
|
end
|
152
194
|
|
@@ -156,6 +198,25 @@ private
|
|
156
198
|
Object.module_eval <<-__
|
157
199
|
module Rails
|
158
200
|
end
|
201
|
+
|
202
|
+
module ActionController
|
203
|
+
class Base
|
204
|
+
def Base.prepend_before_filter(*args, &block)
|
205
|
+
block.call
|
206
|
+
ensure
|
207
|
+
$prepend_before_filter_called = true
|
208
|
+
end
|
209
|
+
|
210
|
+
def Base.helper(&block)
|
211
|
+
ActionView::Base.module_eval(&block)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
module ActionView
|
217
|
+
class Base
|
218
|
+
end
|
219
|
+
end
|
159
220
|
__
|
160
221
|
$load.call()
|
161
222
|
yield
|
@@ -182,13 +243,28 @@ private
|
|
182
243
|
|
183
244
|
module ActionController
|
184
245
|
class Base
|
185
|
-
def Base.
|
186
|
-
block.call
|
246
|
+
def Base.prepend_before_filter(*args, &block)
|
247
|
+
block.call
|
187
248
|
ensure
|
188
|
-
$
|
249
|
+
$prepend_before_filter_called = true
|
250
|
+
end
|
251
|
+
|
252
|
+
def Base.helper(&block)
|
253
|
+
ActionView::Base.module_eval(&block)
|
189
254
|
end
|
190
255
|
end
|
191
256
|
end
|
257
|
+
|
258
|
+
module ActionView
|
259
|
+
class Base
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
module ActiveSupport
|
264
|
+
def ActiveSupport.on_load(*args, &block)
|
265
|
+
block.call()
|
266
|
+
end
|
267
|
+
end
|
192
268
|
__
|
193
269
|
$load.call()
|
194
270
|
yield
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_current
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-15 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'description: rails_current kicks the ass'
|
15
15
|
email: ara.t.howard@gmail.com
|