js-routes 1.4.7 → 1.4.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/js_routes.rb +34 -29
- data/lib/js_routes/version.rb +1 -1
- data/lib/routes.js +2 -5
- data/lib/routes.js.coffee +3 -5
- data/spec/js_routes/options_spec.rb +28 -18
- data/spec/spec_helper.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fb822322b2dc899d084e7562fb8208f490cfa466acb560faa624391e5c08d78
|
4
|
+
data.tar.gz: 5b8ef3ebf451d7b6a3ea519c84d63c9360a63b80422756a60f4de76f68207cfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c62cf998ca3b96ce493a94bba20cbaf5b0980c29c2e3f4320f5633f4ea1336f0eaa4309658bb941be393184b332cb7f2eb2437a13b95351ff71b67c368d1a629
|
7
|
+
data.tar.gz: f008e5cc545deb0e8fb389de0111b796af4cc66f3fccaed002e1c2107b2ed61955ab2c12502b0c87d6f39f4b4106a958cdc742830b3df0e12181bfc3a5b0732e
|
data/CHANGELOG.md
CHANGED
data/lib/js_routes.rb
CHANGED
@@ -118,18 +118,18 @@ class JsRoutes
|
|
118
118
|
end
|
119
119
|
|
120
120
|
{
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
121
|
+
'GEM_VERSION' => JsRoutes::VERSION,
|
122
|
+
'ROUTES' => js_routes,
|
123
|
+
'NODE_TYPES' => json(NODE_TYPES),
|
124
|
+
'RAILS_VERSION' => ActionPack.version,
|
125
|
+
'DEPRECATED_GLOBBING_BEHAVIOR' => ActionPack::VERSION::MAJOR == 4 && ActionPack::VERSION::MINOR == 0,
|
126
|
+
|
127
|
+
'APP_CLASS' => application.class.to_s,
|
128
|
+
'NAMESPACE' => json(@configuration.namespace),
|
129
|
+
'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
|
130
|
+
'PREFIX' => json(@configuration.prefix),
|
131
|
+
'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),
|
132
|
+
'SERIALIZER' => @configuration.serializer || json(nil),
|
133
133
|
}.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)|
|
134
134
|
js.gsub!(key, value.to_s)
|
135
135
|
end
|
@@ -173,7 +173,8 @@ class JsRoutes
|
|
173
173
|
|
174
174
|
def mounted_app_routes(route)
|
175
175
|
rails_engine_app = get_app_from_route(route)
|
176
|
-
if rails_engine_app.respond_to?(:superclass) &&
|
176
|
+
if rails_engine_app.respond_to?(:superclass) &&
|
177
|
+
rails_engine_app.superclass == Rails::Engine && !route.path.anchored
|
177
178
|
rails_engine_app.routes.named_routes.map do |_, engine_route|
|
178
179
|
build_route_if_match(engine_route, route)
|
179
180
|
end
|
@@ -183,7 +184,8 @@ class JsRoutes
|
|
183
184
|
end
|
184
185
|
|
185
186
|
def get_app_from_route(route)
|
186
|
-
# rails engine in Rails 4.2 use additional
|
187
|
+
# rails engine in Rails 4.2 use additional
|
188
|
+
# ActionDispatch::Routing::Mapper::Constraints, which contain app
|
187
189
|
if route.app.respond_to?(:app) && route.app.respond_to?(:constraints)
|
188
190
|
route.app.app
|
189
191
|
else
|
@@ -191,8 +193,9 @@ class JsRoutes
|
|
191
193
|
end
|
192
194
|
end
|
193
195
|
|
194
|
-
def build_route_if_match(route, parent_route=nil)
|
195
|
-
if any_match?(route, parent_route, @configuration[:exclude]) ||
|
196
|
+
def build_route_if_match(route, parent_route = nil)
|
197
|
+
if any_match?(route, parent_route, @configuration[:exclude]) ||
|
198
|
+
!any_match?(route, parent_route, @configuration[:include])
|
196
199
|
nil
|
197
200
|
else
|
198
201
|
build_js(route, parent_route)
|
@@ -203,7 +206,7 @@ class JsRoutes
|
|
203
206
|
full_route = [parent_route.try(:name), route.name].compact.join('_')
|
204
207
|
|
205
208
|
matchers = Array(matchers)
|
206
|
-
matchers.any? {|regex| full_route =~ regex}
|
209
|
+
matchers.any? { |regex| full_route =~ regex }
|
207
210
|
end
|
208
211
|
|
209
212
|
def build_js(route, parent_route)
|
@@ -211,7 +214,7 @@ class JsRoutes
|
|
211
214
|
route_name = generate_route_name(name, (:path unless @configuration[:compact]))
|
212
215
|
parent_spec = parent_route.try(:path).try(:spec)
|
213
216
|
route_arguments = route_js_arguments(route, parent_spec)
|
214
|
-
url_link = generate_url_link(name,
|
217
|
+
url_link = generate_url_link(name, route_arguments)
|
215
218
|
_ = <<-JS.strip!
|
216
219
|
// #{name.join('.')} => #{parent_spec}#{route.path.spec}
|
217
220
|
// function(#{build_params(route.required_parts)})
|
@@ -225,7 +228,8 @@ class JsRoutes
|
|
225
228
|
hash[part] = required_parts.include?(part)
|
226
229
|
end
|
227
230
|
default_options = route.defaults.select do |part, _|
|
228
|
-
FILTERED_DEFAULT_PARTS.exclude?(part) &&
|
231
|
+
FILTERED_DEFAULT_PARTS.exclude?(part) &&
|
232
|
+
URL_OPTIONS.include?(part) || parts_table[part]
|
229
233
|
end
|
230
234
|
[
|
231
235
|
# JS objects don't preserve the order of properties which is crucial,
|
@@ -235,19 +239,19 @@ class JsRoutes
|
|
235
239
|
serialize(route.path.spec, parent_spec)
|
236
240
|
].map do |argument|
|
237
241
|
json(argument)
|
238
|
-
end.join(
|
242
|
+
end.join(', ')
|
239
243
|
end
|
240
244
|
|
241
|
-
def generate_url_link(name,
|
242
|
-
return
|
245
|
+
def generate_url_link(name, route_arguments)
|
246
|
+
return '' unless @configuration[:url_links]
|
247
|
+
|
243
248
|
<<-JS.strip!
|
244
249
|
#{generate_route_name(name, :url)}: Utils.route(#{route_arguments}, true)
|
245
250
|
JS
|
246
251
|
end
|
247
252
|
|
248
|
-
def generate_route_name(
|
249
|
-
route_name =
|
250
|
-
route_name << "_#{ suffix }" if suffix
|
253
|
+
def generate_route_name(*parts)
|
254
|
+
route_name = parts.compact.join('_')
|
251
255
|
@configuration[:camel_case] ? route_name.camelize(:lower) : route_name
|
252
256
|
end
|
253
257
|
|
@@ -257,7 +261,7 @@ class JsRoutes
|
|
257
261
|
|
258
262
|
def build_params(required_parts)
|
259
263
|
params = required_parts + [LAST_OPTIONS_KEY]
|
260
|
-
params.join(
|
264
|
+
params.join(', ')
|
261
265
|
end
|
262
266
|
|
263
267
|
# This function serializes Journey route into JSON structure
|
@@ -266,7 +270,9 @@ class JsRoutes
|
|
266
270
|
# Routes.js file will be smaller.
|
267
271
|
def serialize(spec, parent_spec=nil)
|
268
272
|
return nil unless spec
|
269
|
-
|
273
|
+
# Rails 4 globbing requires * removal
|
274
|
+
return spec.tr(':*', '') if spec.is_a?(String)
|
275
|
+
|
270
276
|
result = serialize_spec(spec, parent_spec)
|
271
277
|
if parent_spec && result[1].is_a?(String)
|
272
278
|
result = [
|
@@ -280,7 +286,7 @@ class JsRoutes
|
|
280
286
|
result
|
281
287
|
end
|
282
288
|
|
283
|
-
def serialize_spec(spec, parent_spec=nil)
|
289
|
+
def serialize_spec(spec, parent_spec = nil)
|
284
290
|
[
|
285
291
|
NODE_TYPES[spec.type],
|
286
292
|
serialize(spec.left, parent_spec),
|
@@ -288,4 +294,3 @@ class JsRoutes
|
|
288
294
|
]
|
289
295
|
end
|
290
296
|
end
|
291
|
-
|
data/lib/js_routes/version.rb
CHANGED
data/lib/routes.js
CHANGED
@@ -336,9 +336,6 @@ Based on Rails RAILS_VERSION routes of APP_CLASS
|
|
336
336
|
visit_globbing: function(route, parameters, optional) {
|
337
337
|
var left, right, type, value;
|
338
338
|
type = route[0], left = route[1], right = route[2];
|
339
|
-
if (left.replace(/^\*/i, "") !== left) {
|
340
|
-
route[1] = left = left.replace(/^\*/i, "");
|
341
|
-
}
|
342
339
|
value = parameters[left];
|
343
340
|
delete parameters[left];
|
344
341
|
if (value == null) {
|
@@ -472,7 +469,7 @@ Based on Rails RAILS_VERSION routes of APP_CLASS
|
|
472
469
|
},
|
473
470
|
namespace: function(root, namespace, routes) {
|
474
471
|
var index, j, len, part, parts;
|
475
|
-
parts = namespace.split(".");
|
472
|
+
parts = namespace ? namespace.split(".") : [];
|
476
473
|
if (parts.length === 0) {
|
477
474
|
return routes;
|
478
475
|
}
|
@@ -518,7 +515,7 @@ Based on Rails RAILS_VERSION routes of APP_CLASS
|
|
518
515
|
return Utils.make();
|
519
516
|
});
|
520
517
|
} else {
|
521
|
-
Utils.make();
|
518
|
+
return Utils.make();
|
522
519
|
}
|
523
520
|
|
524
521
|
}).call(this);
|
data/lib/routes.js.coffee
CHANGED
@@ -258,8 +258,6 @@ Utils =
|
|
258
258
|
#
|
259
259
|
visit_globbing: (route, parameters, optional) ->
|
260
260
|
[type, left, right] = route
|
261
|
-
# fix for rails 4 globbing
|
262
|
-
route[1] = left = left.replace(/^\*/i, "") if left.replace(/^\*/i, "") isnt left
|
263
261
|
value = parameters[left]
|
264
262
|
delete parameters[left]
|
265
263
|
return @visit(route, parameters, optional) unless value?
|
@@ -377,7 +375,7 @@ Utils =
|
|
377
375
|
result
|
378
376
|
|
379
377
|
namespace: (root, namespace, routes) ->
|
380
|
-
parts = namespace.split(".")
|
378
|
+
parts = if namespace then namespace.split(".") else []
|
381
379
|
return routes if parts.length == 0
|
382
380
|
for part, index in parts
|
383
381
|
if index < parts.length - 1
|
@@ -402,6 +400,7 @@ Utils =
|
|
402
400
|
|
403
401
|
routes.default_serializer = (object, prefix) ->
|
404
402
|
Utils.default_serializer(object, prefix)
|
403
|
+
# Browser globals
|
405
404
|
Utils.namespace(root, NAMESPACE, routes)
|
406
405
|
|
407
406
|
# Set up Routes appropriately for the environment.
|
@@ -409,5 +408,4 @@ if typeof define is "function" and define.amd
|
|
409
408
|
# AMD
|
410
409
|
define [], -> Utils.make()
|
411
410
|
else
|
412
|
-
|
413
|
-
Utils.make()
|
411
|
+
return Utils.make()
|
@@ -167,36 +167,46 @@ describe JsRoutes, "options" do
|
|
167
167
|
expect(evaljs("Routes.no_format_path({format: 'json'})")).to eq(test_routes.no_format_path(format: 'json'))
|
168
168
|
end
|
169
169
|
|
170
|
-
describe "
|
170
|
+
describe "namespace option" do
|
171
171
|
let(:_options) { {:namespace => "PHM"} }
|
172
172
|
it "should use this namespace for routing" do
|
173
173
|
expect(evaljs("window.Routes")).to be_nil
|
174
174
|
expect(evaljs("PHM.inbox_path")).not_to be_nil
|
175
175
|
end
|
176
|
-
end
|
177
176
|
|
178
|
-
|
179
|
-
|
180
|
-
let(:_presetup) { "window.PHM = {}" }
|
181
|
-
let(:_options) { {:namespace => "PHM.Routes"} }
|
177
|
+
context "is nil" do
|
178
|
+
let(:_options) { {:namespace => nil} }
|
182
179
|
it "should use this namespace for routing" do
|
183
|
-
|
180
|
+
evaljs("window.zz = #{JsRoutes.generate(namespace: nil)}")
|
181
|
+
expect(evaljs("window.Routes")).to be_nil
|
182
|
+
expect(evaljs("window.zz.inbox_path")).not_to be_nil
|
184
183
|
end
|
184
|
+
|
185
185
|
end
|
186
186
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
187
|
+
describe "is nested" do
|
188
|
+
context "and defined on client" do
|
189
|
+
let(:_presetup) { "window.PHM = {}" }
|
190
|
+
let(:_options) { {:namespace => "PHM.Routes"} }
|
191
|
+
it "should use this namespace for routing" do
|
192
|
+
expect(evaljs("PHM.Routes.inbox_path")).not_to be_nil
|
193
|
+
end
|
191
194
|
end
|
192
|
-
end
|
193
195
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
196
|
+
context "but undefined on client" do
|
197
|
+
let(:_options) { {:namespace => "PHM.Routes"} }
|
198
|
+
it "should initialize namespace" do
|
199
|
+
expect(evaljs("window.PHM.Routes.inbox_path")).not_to be_nil
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context "and some parts are defined" do
|
204
|
+
let(:_presetup) { "window.PHM = { Utils: {} };" }
|
205
|
+
let(:_options) { {:namespace => "PHM.Routes"} }
|
206
|
+
it "should not overwrite existing parts" do
|
207
|
+
expect(evaljs("window.PHM.Utils")).not_to be_nil
|
208
|
+
expect(evaljs("window.PHM.Routes.inbox_path")).not_to be_nil
|
209
|
+
end
|
200
210
|
end
|
201
211
|
end
|
202
212
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -95,11 +95,11 @@ RSpec.configure do |config|
|
|
95
95
|
|
96
96
|
if defined?(JRUBY_VERSION)
|
97
97
|
jscontext[:log] = lambda do |context, value|
|
98
|
-
puts value
|
98
|
+
puts value.inspect
|
99
99
|
end
|
100
100
|
else
|
101
101
|
jscontext.attach("log", proc do |value|
|
102
|
-
puts value
|
102
|
+
puts value.inspect
|
103
103
|
end)
|
104
104
|
end
|
105
105
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js-routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Gusiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|