gon 6.3.2 → 6.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +1 -5
- data/lib/gon/base.rb +6 -3
- data/lib/gon/compatibility/old_rails.rb +2 -0
- data/lib/gon/env_finder.rb +2 -0
- data/lib/gon/escaper.rb +2 -0
- data/lib/gon/global.rb +2 -0
- data/lib/gon/helpers.rb +2 -0
- data/lib/gon/jbuilder/parser.rb +6 -4
- data/lib/gon/jbuilder.rb +3 -1
- data/lib/gon/json_dumper.rb +18 -1
- data/lib/gon/rabl.rb +3 -1
- data/lib/gon/request.rb +2 -0
- data/lib/gon/spec_helpers.rb +3 -1
- data/lib/gon/version.rb +3 -1
- data/lib/gon/watch.rb +2 -0
- data/lib/gon.rb +2 -0
- metadata +21 -38
- data/.github/FUNDING.yml +0 -1
- data/.gitignore +0 -7
- data/.travis.yml +0 -12
- data/Gemfile +0 -6
- data/Rakefile +0 -10
- data/doc/logo.png +0 -0
- data/doc/logo_small.png +0 -0
- data/doc/top_sample.png +0 -0
- data/gon.gemspec +0 -30
- data/spec/gon/basic_spec.rb +0 -304
- data/spec/gon/global_spec.rb +0 -146
- data/spec/gon/jbuilder_spec.rb +0 -75
- data/spec/gon/rabl_spec.rb +0 -90
- data/spec/gon/templates_spec.rb +0 -36
- data/spec/gon/thread_spec.rb +0 -39
- data/spec/gon/watch_spec.rb +0 -81
- data/spec/spec_helper.rb +0 -36
- data/spec/test_data/_sample_partial.json.jbuilder +0 -1
- data/spec/test_data/sample.json.jbuilder +0 -1
- data/spec/test_data/sample.rabl +0 -2
- data/spec/test_data/sample_rabl_rails.rabl +0 -2
- data/spec/test_data/sample_url_helpers.json.jbuilder +0 -1
- data/spec/test_data/sample_with_controller_method.json.jbuilder +0 -2
- data/spec/test_data/sample_with_helpers.json.jbuilder +0 -1
- data/spec/test_data/sample_with_helpers.rabl +0 -3
- data/spec/test_data/sample_with_helpers_rabl_rails.rabl +0 -3
- data/spec/test_data/sample_with_locals.json.jbuilder +0 -2
- data/spec/test_data/sample_with_partial.json.jbuilder +0 -1
data/spec/gon/basic_spec.rb
DELETED
@@ -1,304 +0,0 @@
|
|
1
|
-
describe Gon do
|
2
|
-
|
3
|
-
before(:each) do
|
4
|
-
Gon.clear
|
5
|
-
end
|
6
|
-
|
7
|
-
describe '#all_variables' do
|
8
|
-
|
9
|
-
it 'returns all variables in hash' do
|
10
|
-
Gon.a = 1
|
11
|
-
Gon.b = 2
|
12
|
-
Gon.c = Gon.a + Gon.b
|
13
|
-
expect(Gon.c).to eq(3)
|
14
|
-
expect(Gon.all_variables).to eq({ 'a' => 1, 'b' => 2, 'c' => 3 })
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'supports all data types' do
|
18
|
-
Gon.int = 1
|
19
|
-
Gon.float = 1.1
|
20
|
-
Gon.string = 'string'
|
21
|
-
Gon.symbol = :symbol
|
22
|
-
Gon.array = [1, 'string']
|
23
|
-
Gon.hash_var = { :a => 1, :b => '2' }
|
24
|
-
Gon.hash_w_array = { :a => [2, 3] }
|
25
|
-
Gon.klass = Hash
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'can be filled with dynamic named variables' do
|
29
|
-
check = {}
|
30
|
-
3.times do |i|
|
31
|
-
Gon.set_variable("variable#{i}", i)
|
32
|
-
check["variable#{i}"] = i
|
33
|
-
end
|
34
|
-
|
35
|
-
expect(Gon.all_variables).to eq(check)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'can set and get variable with dynamic name' do
|
39
|
-
var_name = "variable#{rand}"
|
40
|
-
|
41
|
-
Gon.set_variable(var_name, 1)
|
42
|
-
expect(Gon.get_variable(var_name)).to eq(1)
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'can be support new push syntax' do
|
46
|
-
Gon.push({ :int => 1, :string => 'string' })
|
47
|
-
expect(Gon.all_variables).to eq({ 'int' => 1, 'string' => 'string' })
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'push with wrong object' do
|
51
|
-
expect {
|
52
|
-
Gon.push(String.new('string object'))
|
53
|
-
}.to raise_error('Object must have each_pair method')
|
54
|
-
end
|
55
|
-
|
56
|
-
describe "#merge_variable" do
|
57
|
-
it 'deep merges the same key' do
|
58
|
-
Gon.merge_variable(:foo, { bar: { tar: 12 }, car: 23 })
|
59
|
-
Gon.merge_variable(:foo, { bar: { dar: 21 }, car: 12 })
|
60
|
-
expect(Gon.get_variable(:foo)).to eq(bar: { tar: 12, dar: 21 }, car: 12)
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'merges on push with a flag' do
|
64
|
-
Gon.push(foo: { bar: 1 })
|
65
|
-
Gon.push({ foo: { tar: 1 } }, :merge)
|
66
|
-
expect(Gon.get_variable("foo")).to eq(bar: 1, tar: 1)
|
67
|
-
end
|
68
|
-
|
69
|
-
context 'overrides key' do
|
70
|
-
specify "the previous value wasn't hash" do
|
71
|
-
Gon.merge_variable(:foo, 2)
|
72
|
-
Gon.merge_variable(:foo, { a: 1 })
|
73
|
-
expect(Gon.get_variable(:foo)).to eq(a: 1)
|
74
|
-
end
|
75
|
-
|
76
|
-
specify "the new value isn't a hash" do
|
77
|
-
Gon.merge_variable(:foo, { a: 1 })
|
78
|
-
Gon.merge_variable(:foo, 2)
|
79
|
-
expect(Gon.get_variable(:foo)).to eq(2)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
describe '#include_gon' do
|
87
|
-
|
88
|
-
before(:each) do
|
89
|
-
Gon::Request.
|
90
|
-
instance_variable_set(:@request_id, request.object_id)
|
91
|
-
expect(ActionView::Base.instance_methods).to include(:include_gon)
|
92
|
-
@base = ActionView::Base.new
|
93
|
-
@base.request = request
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'outputs correct js with an integer' do
|
97
|
-
Gon.int = 1
|
98
|
-
expect(@base.include_gon).to eq(wrap_script(
|
99
|
-
'window.gon={};' +
|
100
|
-
'gon.int=1;'))
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'outputs correct js with a string' do
|
104
|
-
Gon.str = %q(a'b"c)
|
105
|
-
expect(@base.include_gon).to eq(wrap_script(
|
106
|
-
'window.gon={};' +
|
107
|
-
%q(gon.str="a'b\"c";))
|
108
|
-
)
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'outputs correct js with a script string' do
|
112
|
-
Gon.str = %q(</script><script>alert('!')</script>)
|
113
|
-
escaped_str = "\\u003c/script\\u003e\\u003cscript\\u003ealert('!')\\u003c/script\\u003e"
|
114
|
-
expect(@base.include_gon).to eq(wrap_script(
|
115
|
-
'window.gon={};' +
|
116
|
-
%Q(gon.str="#{escaped_str}";))
|
117
|
-
)
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'outputs correct js with an integer and type' do
|
121
|
-
Gon.int = 1
|
122
|
-
expect(@base.include_gon(type: true)).to eq('<script type="text/javascript">' +
|
123
|
-
"\n//<![CDATA[\n" +
|
124
|
-
'window.gon={};' +
|
125
|
-
'gon.int=1;' +
|
126
|
-
"\n//]]>\n" +
|
127
|
-
'</script>')
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'outputs correct js with an integer, camel-case and namespace' do
|
131
|
-
Gon.int_cased = 1
|
132
|
-
expect(@base.include_gon(camel_case: true, namespace: 'camel_cased')).to eq(
|
133
|
-
wrap_script('window.camel_cased={};' +
|
134
|
-
'camel_cased.intCased=1;')
|
135
|
-
)
|
136
|
-
end
|
137
|
-
|
138
|
-
it 'outputs correct js with camel_depth = :recursive' do
|
139
|
-
Gon.test_hash = { test_depth_one: { test_depth_two: 1 } }
|
140
|
-
expect(@base.include_gon(camel_case: true, camel_depth: :recursive)).to eq(
|
141
|
-
wrap_script('window.gon={};' +
|
142
|
-
'gon.testHash={"testDepthOne":{"testDepthTwo":1}};')
|
143
|
-
)
|
144
|
-
end
|
145
|
-
|
146
|
-
it 'outputs correct js with camel_depth = 2' do
|
147
|
-
Gon.test_hash = { test_depth_one: { test_depth_two: 1 } }
|
148
|
-
expect(@base.include_gon(camel_case: true, camel_depth: 2)).to eq(
|
149
|
-
wrap_script('window.gon={};' +
|
150
|
-
'gon.testHash={"testDepthOne":{"test_depth_two":1}};')
|
151
|
-
)
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'outputs correct js for an array with camel_depth = :recursive' do
|
155
|
-
Gon.test_hash = { test_depth_one: [{ test_depth_two: 1 }, { test_depth_two: 2 }] }
|
156
|
-
expect(@base.include_gon(camel_case: true, camel_depth: :recursive)).to eq( \
|
157
|
-
wrap_script('window.gon={};' +
|
158
|
-
'gon.testHash={"testDepthOne":[{"testDepthTwo":1},{"testDepthTwo":2}]};')
|
159
|
-
)
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'outputs correct key with camel_case option set alternately ' do
|
163
|
-
Gon.test_hash = 1
|
164
|
-
@base.include_gon(camel_case: true)
|
165
|
-
|
166
|
-
expect(@base.include_gon(camel_case: false)).to eq(
|
167
|
-
wrap_script('window.gon={};' +
|
168
|
-
'gon.test_hash=1;')
|
169
|
-
)
|
170
|
-
end
|
171
|
-
|
172
|
-
it 'outputs correct js with an integer and without tag' do
|
173
|
-
Gon.int = 1
|
174
|
-
expect(@base.include_gon(need_tag: false)).to eq( \
|
175
|
-
'window.gon={};' +
|
176
|
-
'gon.int=1;'
|
177
|
-
)
|
178
|
-
end
|
179
|
-
|
180
|
-
it 'outputs correct js without variables, without tag and gon init if before there was data' do
|
181
|
-
Gon::Request.
|
182
|
-
instance_variable_set(:@request_id, 123)
|
183
|
-
Gon::Request.instance_variable_set(:@request_env, { 'gon' => { :a => 1 } })
|
184
|
-
expect(@base.include_gon(need_tag: false, init: true)).to eq( \
|
185
|
-
'window.gon={};'
|
186
|
-
)
|
187
|
-
end
|
188
|
-
|
189
|
-
it 'outputs correct js without variables, without tag and gon init' do
|
190
|
-
expect(@base.include_gon(need_tag: false, init: true)).to eq( \
|
191
|
-
'window.gon={};'
|
192
|
-
)
|
193
|
-
end
|
194
|
-
|
195
|
-
it 'outputs correct js without variables, without tag, gon init and an integer' do
|
196
|
-
Gon.int = 1
|
197
|
-
expect(@base.include_gon(need_tag: false, init: true)).to eq( \
|
198
|
-
'window.gon={};' +
|
199
|
-
'gon.int=1;'
|
200
|
-
)
|
201
|
-
end
|
202
|
-
|
203
|
-
it 'outputs correct js without cdata, without type, gon init and an integer' do
|
204
|
-
Gon.int = 1
|
205
|
-
expect(@base.include_gon(cdata: false, type: false)).to eq(
|
206
|
-
wrap_script(
|
207
|
-
"\n" +
|
208
|
-
'window.gon={};' +
|
209
|
-
'gon.int=1;' +
|
210
|
-
"\n", false)
|
211
|
-
)
|
212
|
-
end
|
213
|
-
|
214
|
-
it 'outputs correct js with type text/javascript' do
|
215
|
-
expect(@base.include_gon(need_type: true, init: true)).to eq(wrap_script('window.gon={};'))
|
216
|
-
end
|
217
|
-
|
218
|
-
it 'outputs correct js with namespace check' do
|
219
|
-
expect(@base.include_gon(namespace_check: true)).to eq(wrap_script('window.gon=window.gon||{};'))
|
220
|
-
end
|
221
|
-
|
222
|
-
it 'outputs correct js without namespace check' do
|
223
|
-
expect(@base.include_gon(namespace_check: false)).to eq(wrap_script('window.gon={};'))
|
224
|
-
end
|
225
|
-
|
226
|
-
context "without a current_gon instance" do
|
227
|
-
|
228
|
-
before(:each) do
|
229
|
-
RequestStore.store[:gon] = nil
|
230
|
-
allow(Gon).to receive(:current_gon).and_return(nil)
|
231
|
-
end
|
232
|
-
|
233
|
-
it "does not raise an exception" do
|
234
|
-
expect { @base.include_gon }.to_not raise_error
|
235
|
-
end
|
236
|
-
|
237
|
-
it 'outputs correct js' do
|
238
|
-
expect(@base.include_gon).to eq("")
|
239
|
-
end
|
240
|
-
|
241
|
-
it 'outputs correct js with init' do
|
242
|
-
expect(@base.include_gon(init: true)).to eq(wrap_script('window.gon={};'))
|
243
|
-
end
|
244
|
-
|
245
|
-
end
|
246
|
-
|
247
|
-
end
|
248
|
-
|
249
|
-
describe '#include_gon_amd' do
|
250
|
-
|
251
|
-
before(:each) do
|
252
|
-
Gon::Request.
|
253
|
-
instance_variable_set(:@request_id, request.object_id)
|
254
|
-
@base = ActionView::Base.new
|
255
|
-
@base.request = request
|
256
|
-
end
|
257
|
-
|
258
|
-
it 'is included in ActionView::Base as a helper' do
|
259
|
-
expect(ActionView::Base.instance_methods).to include(:include_gon_amd)
|
260
|
-
end
|
261
|
-
|
262
|
-
it 'outputs correct js without variables' do
|
263
|
-
expect(@base.include_gon_amd).to eq( wrap_script( \
|
264
|
-
'define(\'gon\',[],function(){'+
|
265
|
-
'var gon={};return gon;'+
|
266
|
-
'});')
|
267
|
-
)
|
268
|
-
end
|
269
|
-
|
270
|
-
it 'outputs correct js with an integer' do
|
271
|
-
Gon.int = 1
|
272
|
-
|
273
|
-
expect(@base.include_gon_amd).to eq( wrap_script(
|
274
|
-
'define(\'gon\',[],function(){'+
|
275
|
-
'var gon={};gon[\'int\']=1;return gon;'+
|
276
|
-
'});')
|
277
|
-
)
|
278
|
-
end
|
279
|
-
|
280
|
-
it 'outputs correct module name when given a namespace' do
|
281
|
-
expect(@base.include_gon_amd(namespace: 'data')).to eq(wrap_script(
|
282
|
-
'define(\'data\',[],function(){'+
|
283
|
-
'var gon={};return gon;'+
|
284
|
-
'});')
|
285
|
-
)
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
it 'returns exception if try to set public method as variable' do
|
290
|
-
expect { Gon.all_variables = 123 }.to raise_error(RuntimeError)
|
291
|
-
expect { Gon.rabl = 123 }.to raise_error(RuntimeError)
|
292
|
-
end
|
293
|
-
|
294
|
-
describe '#check_for_rabl_and_jbuilder' do
|
295
|
-
|
296
|
-
let(:controller) { ActionController::Base.new }
|
297
|
-
|
298
|
-
it 'should be able to handle constants array (symbols)' do
|
299
|
-
allow(Gon).to receive(:constants) { Gon.constants }
|
300
|
-
expect { Gon.rabl :template => 'spec/test_data/sample.rabl', :controller => controller }.not_to raise_error
|
301
|
-
expect { Gon.jbuilder :template => 'spec/test_data/sample.json.jbuilder', :controller => controller }.not_to raise_error
|
302
|
-
end
|
303
|
-
end
|
304
|
-
end
|
data/spec/gon/global_spec.rb
DELETED
@@ -1,146 +0,0 @@
|
|
1
|
-
describe Gon::Global do
|
2
|
-
|
3
|
-
before(:each) do
|
4
|
-
Gon::Global.clear
|
5
|
-
Gon::Request.instance_variable_set(:@request_env, nil)
|
6
|
-
end
|
7
|
-
|
8
|
-
describe '#all_variables' do
|
9
|
-
|
10
|
-
it 'returns all variables in hash' do
|
11
|
-
Gon.global.a = 1
|
12
|
-
Gon.global.b = 2
|
13
|
-
Gon.global.c = Gon.global.a + Gon.global.b
|
14
|
-
expect(Gon.global.c).to eq(3)
|
15
|
-
expect(Gon.global.all_variables).to eq({ 'a' => 1, 'b' => 2, 'c' => 3 })
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'supports all data types' do
|
19
|
-
Gon.global.int = 1
|
20
|
-
Gon.global.float = 1.1
|
21
|
-
Gon.global.string = 'string'
|
22
|
-
Gon.global.symbol = :symbol
|
23
|
-
Gon.global.array = [1, 'string']
|
24
|
-
Gon.global.hash_var = { :a => 1, :b => '2' }
|
25
|
-
Gon.global.hash_w_array = { :a => [2, 3] }
|
26
|
-
Gon.global.klass = Hash
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
describe '#include_gon' do
|
32
|
-
|
33
|
-
before(:each) do
|
34
|
-
Gon.clear
|
35
|
-
expect(ActionView::Base.instance_methods).to include(:include_gon)
|
36
|
-
@base = ActionView::Base.new
|
37
|
-
@base.request = request
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'outputs correct js with an integer' do
|
41
|
-
Gon.global.int = 1
|
42
|
-
expect(@base.include_gon).to eq("<script>" +
|
43
|
-
"\n//<![CDATA[\n" +
|
44
|
-
"window.gon={};" +
|
45
|
-
"gon.global={\"int\":1};" +
|
46
|
-
"\n//]]>\n" +
|
47
|
-
"</script>")
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'outputs correct js with an integer and integer in Gon' do
|
51
|
-
Gon.int = 1
|
52
|
-
Gon.global.int = 1
|
53
|
-
expect(@base.include_gon).to eq("<script>" +
|
54
|
-
"\n//<![CDATA[\n" +
|
55
|
-
"window.gon={};" +
|
56
|
-
"gon.global={\"int\":1};" +
|
57
|
-
"gon.int=1;" +
|
58
|
-
"\n//]]>\n" +
|
59
|
-
"</script>")
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'outputs correct js with a string' do
|
63
|
-
Gon.global.str = %q(a'b"c)
|
64
|
-
expect(@base.include_gon).to eq("<script>" +
|
65
|
-
"\n//<![CDATA[\n" +
|
66
|
-
"window.gon={};" +
|
67
|
-
"gon.global={\"str\":\"a'b\\\"c\"};" +
|
68
|
-
"\n//]]>\n" +
|
69
|
-
"</script>")
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'outputs correct js with a script string' do
|
73
|
-
Gon.global.str = %q(</script><script>alert('!')</script>)
|
74
|
-
escaped_str = "\\u003c/script\\u003e\\u003cscript\\u003ealert('!')\\u003c/script\\u003e"
|
75
|
-
expect(@base.include_gon).to eq("<script>" +
|
76
|
-
"\n//<![CDATA[\n" +
|
77
|
-
"window.gon={};" +
|
78
|
-
"gon.global={\"str\":\"#{escaped_str}\"};" +
|
79
|
-
"\n//]]>\n" +
|
80
|
-
"</script>")
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'outputs correct js with a unicode line separator' do
|
84
|
-
Gon.global.str = "\u2028"
|
85
|
-
expect(@base.include_gon).to eq("<script>" +
|
86
|
-
"\n//<![CDATA[\n" +
|
87
|
-
"window.gon={};" +
|
88
|
-
"gon.global={\"str\":\"
\"};" +
|
89
|
-
"\n//]]>\n" +
|
90
|
-
"</script>")
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'outputs locally overridden value' do
|
94
|
-
Gon.str = 'local value'
|
95
|
-
Gon.global.str = 'global value'
|
96
|
-
expect(@base.include_gon(global_root: '')).to eq("<script>" +
|
97
|
-
"\n//<![CDATA[\n" +
|
98
|
-
"window.gon={};" +
|
99
|
-
"gon.str=\"local value\";" +
|
100
|
-
"\n//]]>\n" +
|
101
|
-
"</script>")
|
102
|
-
end
|
103
|
-
|
104
|
-
it "includes the tag attributes in the script tag" do
|
105
|
-
Gon.global.int = 1
|
106
|
-
expect(@base.include_gon(nonce: 'test')).to eq("<script nonce=\"test\">" +
|
107
|
-
"\n//<![CDATA[\n" +
|
108
|
-
"window.gon={};" +
|
109
|
-
"gon.global={\"int\":1};" +
|
110
|
-
"\n//]]>\n" +
|
111
|
-
"</script>")
|
112
|
-
end
|
113
|
-
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'returns exception if try to set public method as variable' do
|
117
|
-
expect { Gon.global.all_variables = 123 }.to raise_error(RuntimeError)
|
118
|
-
expect { Gon.global.rabl = 123 }.to raise_error(RuntimeError)
|
119
|
-
end
|
120
|
-
|
121
|
-
context 'with jbuilder and rabl' do
|
122
|
-
|
123
|
-
before :each do
|
124
|
-
controller.instance_variable_set('@objects', objects)
|
125
|
-
end
|
126
|
-
|
127
|
-
let(:controller) { ActionController::Base.new }
|
128
|
-
let(:objects) { [1, 2] }
|
129
|
-
|
130
|
-
it 'works fine with rabl' do
|
131
|
-
Gon.global.rabl :template => 'spec/test_data/sample.rabl', :controller => controller
|
132
|
-
expect(Gon.global.objects.length).to eq(2)
|
133
|
-
end
|
134
|
-
|
135
|
-
it 'works fine with jbuilder' do
|
136
|
-
Gon.global.jbuilder :template => 'spec/test_data/sample.json.jbuilder', :controller => controller
|
137
|
-
expect(Gon.global.objects.length).to eq(2)
|
138
|
-
end
|
139
|
-
|
140
|
-
it 'should throw exception, if use rabl or jbuilder without :template' do
|
141
|
-
expect { Gon.global.rabl }.to raise_error(RuntimeError)
|
142
|
-
expect { Gon.global.jbuilder }.to raise_error(RuntimeError)
|
143
|
-
end
|
144
|
-
|
145
|
-
end
|
146
|
-
end
|
data/spec/gon/jbuilder_spec.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
describe Gon do
|
2
|
-
|
3
|
-
describe '.jbuilder' do
|
4
|
-
context 'render jbuilder templates' do
|
5
|
-
|
6
|
-
before do
|
7
|
-
Gon.clear
|
8
|
-
controller.instance_variable_set('@objects', objects)
|
9
|
-
end
|
10
|
-
|
11
|
-
let(:controller) { ActionController::Base.new }
|
12
|
-
let(:objects) { [1, 2] }
|
13
|
-
|
14
|
-
it 'render json from jbuilder template' do
|
15
|
-
Gon.jbuilder :template => 'spec/test_data/sample.json.jbuilder', :controller => controller
|
16
|
-
expect(Gon.objects.length).to eq(2)
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'render json from jbuilder template with locals' do
|
20
|
-
Gon.jbuilder :template => 'spec/test_data/sample_with_locals.json.jbuilder',
|
21
|
-
:controller => controller,
|
22
|
-
:locals => { :some_local => 1234, :some_complex_local => OpenStruct.new(:id => 1234) }
|
23
|
-
expect(Gon.some_local).to eq(1234)
|
24
|
-
expect(Gon.some_complex_local_id).to eq(1234)
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'render json from jbuilder template with locals' do
|
28
|
-
Gon.jbuilder :template => 'spec/test_data/sample_with_helpers.json.jbuilder', :controller => controller
|
29
|
-
expect(Gon.date).to eq('about 6 hours')
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'render json from jbuilder template with controller methods' do
|
33
|
-
class << controller
|
34
|
-
def private_controller_method
|
35
|
-
'gon test helper works'
|
36
|
-
end
|
37
|
-
helper_method :private_controller_method
|
38
|
-
private :private_controller_method
|
39
|
-
end
|
40
|
-
|
41
|
-
Gon.jbuilder :template => 'spec/test_data/sample_with_controller_method.json.jbuilder', :controller => controller
|
42
|
-
expect(Gon.data_from_method).to eq('gon test helper works')
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'render json from jbuilder template with a partial' do
|
46
|
-
controller.view_paths << 'spec/test_data'
|
47
|
-
Gon.jbuilder :template => 'spec/test_data/sample_with_partial.json.jbuilder', :controller => controller
|
48
|
-
expect(Gon.objects.length).to eq(2)
|
49
|
-
end
|
50
|
-
|
51
|
-
context 'within Rails' do
|
52
|
-
before do
|
53
|
-
module ::Rails
|
54
|
-
end
|
55
|
-
|
56
|
-
allow(Rails).to receive_message_chain("application.routes.url_helpers.instance_methods") { [:user_path] }
|
57
|
-
controller.instance_variable_set('@user_id', 1)
|
58
|
-
end
|
59
|
-
|
60
|
-
after do
|
61
|
-
Object.send(:remove_const, :Rails)
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'includes url_helpers' do
|
65
|
-
expect(controller).to receive(:user_path) { |id| "/users/#{id}" }
|
66
|
-
Gon.jbuilder :template => 'spec/test_data/sample_url_helpers.json.jbuilder', :controller => controller
|
67
|
-
expect(Gon.url).to eq '/users/1'
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
data/spec/gon/rabl_spec.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
describe Gon do
|
2
|
-
describe '.rabl' do
|
3
|
-
|
4
|
-
before :each do
|
5
|
-
Gon.clear
|
6
|
-
controller.instance_variable_set('@objects', objects)
|
7
|
-
end
|
8
|
-
|
9
|
-
let(:controller) { ActionController::Base.new }
|
10
|
-
let(:objects) { [1, 2] }
|
11
|
-
|
12
|
-
context 'render template with deprecation' do
|
13
|
-
it 'still works' do
|
14
|
-
Gon.rabl 'spec/test_data/sample.rabl', :controller => controller
|
15
|
-
expect(Gon.objects.length).to eq(2)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'option locals' do
|
20
|
-
it 'works without locals object properly' do
|
21
|
-
Gon.rabl(
|
22
|
-
:template => 'spec/test_data/sample.rabl',
|
23
|
-
:controller => controller
|
24
|
-
)
|
25
|
-
expect(Gon.objects.map { |it| it['object']['inspect'] }).to eq(%w(1 2))
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'works with different locals object' do
|
29
|
-
Gon.rabl(
|
30
|
-
:template => 'spec/test_data/sample.rabl',
|
31
|
-
:controller => controller,
|
32
|
-
:locals => { :objects => [3, 4] }
|
33
|
-
)
|
34
|
-
expect(Gon.objects.map { |it| it['object']['inspect'] }).to eq(%w(3 4))
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'works if rabl is included' do
|
39
|
-
Gon.rabl :template => 'spec/test_data/sample.rabl', :controller => controller
|
40
|
-
expect(Gon.objects.length).to eq(2)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'works with ActionView::Helpers' do
|
44
|
-
Gon.rabl :template => 'spec/test_data/sample_with_helpers.rabl', :controller => controller
|
45
|
-
expect(Gon.objects.first['object']['time_ago']).to eq('about 6 hours')
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'raise exception if rabl is not included' do
|
49
|
-
Gon.send :remove_const, 'Rabl'
|
50
|
-
expect { Gon.rabl :template => 'spec/test_data/sample.rabl', :controller => controller }.to raise_error(NameError)
|
51
|
-
load 'rabl.rb'
|
52
|
-
load 'gon/rabl.rb'
|
53
|
-
end
|
54
|
-
|
55
|
-
context '.template_path' do
|
56
|
-
context 'template is specified' do
|
57
|
-
|
58
|
-
it 'add the extension if not included in the template name' do
|
59
|
-
expect(Gon::EnvFinder.send(:template_path, { :template => 'spec/test_data/sample' }, 'rabl')).to eql('spec/test_data/sample.rabl')
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'return the specified template' do
|
63
|
-
expect(Gon::EnvFinder.send(:template_path, { :template => 'spec/test_data/sample.rabl' }, 'rabl')).to eql('spec/test_data/sample.rabl')
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
context 'template is not specified' do
|
69
|
-
|
70
|
-
before do
|
71
|
-
Gon.clear
|
72
|
-
controller.instance_variable_set('@objects', objects)
|
73
|
-
controller.action_name = 'show'
|
74
|
-
end
|
75
|
-
|
76
|
-
let(:controller) { ActionController::Base.new }
|
77
|
-
let(:objects) { [1, 2] }
|
78
|
-
|
79
|
-
context 'the action doesn as a template at a different format' do
|
80
|
-
it 'return the same template as the action with rabl extension' do
|
81
|
-
expect(Gon::EnvFinder.send(:template_path, { :controller => controller }, 'rabl')).to eql('app/views/action_controller/base/show.json.rabl')
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
data/spec/gon/templates_spec.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
describe Gon do
|
2
|
-
|
3
|
-
describe '.template_path' do
|
4
|
-
context 'template is specified' do
|
5
|
-
|
6
|
-
it 'add the extension if not included in the template name' do
|
7
|
-
expect(Gon::EnvFinder.send(:template_path, { :template => 'spec/test_data/sample' }, 'jbuilder')).to eql('spec/test_data/sample.jbuilder')
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'return the specified template' do
|
11
|
-
expect(Gon::EnvFinder.send(:template_path, { :template => 'spec/test_data/sample.jbuilder' }, 'jbuilder')).to eql('spec/test_data/sample.jbuilder')
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
context 'template is not specified' do
|
17
|
-
|
18
|
-
before do
|
19
|
-
Gon.clear
|
20
|
-
controller.instance_variable_set('@objects', objects)
|
21
|
-
controller.action_name = 'show'
|
22
|
-
end
|
23
|
-
|
24
|
-
let(:controller) { ActionController::Base.new }
|
25
|
-
let(:objects) { [1, 2] }
|
26
|
-
|
27
|
-
context 'the action doesn as a template at a different format' do
|
28
|
-
it 'return the same template as the action with rabl extension' do
|
29
|
-
expect(Gon::EnvFinder.send(:template_path, { :controller => controller }, 'jbuilder')).to eql('app/views/action_controller/base/show.json.jbuilder')
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
data/spec/gon/thread_spec.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
class GonTestWorker
|
2
|
-
include Gon::ControllerHelpers
|
3
|
-
|
4
|
-
def request
|
5
|
-
@request ||= ActionDispatch::TestRequest.create
|
6
|
-
end
|
7
|
-
|
8
|
-
def env
|
9
|
-
request.env
|
10
|
-
end
|
11
|
-
|
12
|
-
def execute
|
13
|
-
gon.clear
|
14
|
-
gon.a ||= 1
|
15
|
-
gon.a += 1
|
16
|
-
end
|
17
|
-
|
18
|
-
def value
|
19
|
-
gon.a
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
describe 'threading behaviour' do
|
24
|
-
before do
|
25
|
-
allow(Gon).to receive(:current_gon).and_call_original
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'is threadsafe' do
|
29
|
-
threads = []
|
30
|
-
10.times do
|
31
|
-
threads << Thread.new do
|
32
|
-
gtw = GonTestWorker.new
|
33
|
-
gtw.execute
|
34
|
-
expect(gtw.value).to eq 2
|
35
|
-
end
|
36
|
-
end
|
37
|
-
threads.each(&:join)
|
38
|
-
end
|
39
|
-
end
|