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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f1f7818deadc1efdc9737ec8a149e42c22ae25b32c441e2a72617cd62725611
4
- data.tar.gz: 2a9bb8fe0e342c6ff6601b0bd3addcdf909aa9221e0b0c5712ef7ce757f5fcd5
3
+ metadata.gz: 2fb822322b2dc899d084e7562fb8208f490cfa466acb560faa624391e5c08d78
4
+ data.tar.gz: 5b8ef3ebf451d7b6a3ea519c84d63c9360a63b80422756a60f4de76f68207cfe
5
5
  SHA512:
6
- metadata.gz: 141847fb1eb1fb8c70392a8ab54a094a5a3b2a3d0ec3dcfeb86e9bae416c4ac4311ab5395c3f295b0d527d7ecd23c35585354a62eda2befa5f12fa3a3e3514eb
7
- data.tar.gz: 57e2939986d82f88c4ed48be556c06e611cc8f6f0f03c232bc7ba2146d7ba3f417cb891b5c50cc97046eb02300233316ad00236c71712790fa5c377c6e9c6f3e
6
+ metadata.gz: c62cf998ca3b96ce493a94bba20cbaf5b0980c29c2e3f4320f5633f4ea1336f0eaa4309658bb941be393184b332cb7f2eb2437a13b95351ff71b67c368d1a629
7
+ data.tar.gz: f008e5cc545deb0e8fb389de0111b796af4cc66f3fccaed002e1c2107b2ed61955ab2c12502b0c87d6f39f4b4106a958cdc742830b3df0e12181bfc3a5b0732e
@@ -1,5 +1,9 @@
1
1
  ## master
2
2
 
3
+ ## v1.4.8
4
+
5
+ * Allow to specify null namespace and receive routes as an object without assigning it anywhere #247
6
+
3
7
  ## v1.4.7
4
8
 
5
9
  * Fix a LocalJumpError on secondary initialization of the app #248
@@ -118,18 +118,18 @@ class JsRoutes
118
118
  end
119
119
 
120
120
  {
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),
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) && rails_engine_app.superclass == Rails::Engine && !route.path.anchored
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 ActionDispatch::Routing::Mapper::Constraints, which contain app
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]) || !any_match?(route, parent_route, @configuration[:include])
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, route_name, route_arguments, route)
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) && URL_OPTIONS.include?(part) || parts_table[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, route_name, route_arguments, route)
242
- return "" unless @configuration[:url_links]
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(name, suffix)
249
- route_name = name.join('_')
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
- return spec.tr(':', '') if spec.is_a?(String)
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
-
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "1.4.7"
2
+ VERSION = "1.4.8"
3
3
  end
@@ -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);
@@ -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
- # Browser globals
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 "when namespace option is specified" do
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
- describe "when nested namespace option is specified" do
179
- context "and defined on client" do
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
- expect(evaljs("PHM.Routes.inbox_path")).not_to be_nil
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
- context "but undefined on client" do
188
- let(:_options) { {:namespace => "PHM.Routes"} }
189
- it "should initialize namespace" do
190
- expect(evaljs("window.PHM.Routes.inbox_path")).not_to be_nil
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
- context "and some parts are defined" do
195
- let(:_presetup) { "window.PHM = { Utils: {} };" }
196
- let(:_options) { {:namespace => "PHM.Routes"} }
197
- it "should not overwrite existing parts" do
198
- expect(evaljs("window.PHM.Utils")).not_to be_nil
199
- expect(evaljs("window.PHM.Routes.inbox_path")).not_to be_nil
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
@@ -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.7
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-06-24 00:00:00.000000000 Z
11
+ date: 2019-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties