js-routes 1.3.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/routes.js CHANGED
@@ -4,7 +4,7 @@ Based on Rails routes of APP_CLASS
4
4
  */
5
5
 
6
6
  (function() {
7
- var DeprecatedBehavior, NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, Utils, createGlobalJsRoutesObject, defaults, root,
7
+ var DeprecatedBehavior, NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, Utils, root,
8
8
  hasProp = {}.hasOwnProperty,
9
9
  slice = [].slice;
10
10
 
@@ -16,20 +16,21 @@ Based on Rails routes of APP_CLASS
16
16
 
17
17
  ParameterMissing.prototype = new Error();
18
18
 
19
- defaults = {
20
- prefix: "PREFIX",
21
- default_url_options: DEFAULT_URL_OPTIONS
22
- };
23
-
24
19
  NodeTypes = NODE_TYPES;
25
20
 
26
- SpecialOptionsKey = "SPECIAL_OPTIONS_KEY";
21
+ SpecialOptionsKey = SPECIAL_OPTIONS_KEY;
27
22
 
28
23
  DeprecatedBehavior = DEPRECATED_BEHAVIOR;
29
24
 
30
25
  ReservedOptions = ['anchor', 'trailing_slash', 'host', 'port', 'protocol'];
31
26
 
32
27
  Utils = {
28
+ configuration: {
29
+ prefix: PREFIX,
30
+ default_url_options: DEFAULT_URL_OPTIONS,
31
+ special_options_key: SPECIAL_OPTIONS_KEY,
32
+ serializer: SERIALIZER
33
+ },
33
34
  default_serializer: function(object, prefix) {
34
35
  var element, i, j, key, len, prop, s;
35
36
  if (prefix == null) {
@@ -74,10 +75,11 @@ Based on Rails routes of APP_CLASS
74
75
  }
75
76
  return s.join("&");
76
77
  },
77
- custom_serializer: SERIALIZER,
78
78
  serialize: function(object) {
79
- if ((this.custom_serializer != null) && this.get_object_type(this.custom_serializer) === "function") {
80
- return this.custom_serializer(object);
79
+ var custom_serializer;
80
+ custom_serializer = this.configuration.serializer;
81
+ if ((custom_serializer != null) && this.get_object_type(custom_serializer) === "function") {
82
+ return custom_serializer(object);
81
83
  } else {
82
84
  return this.default_serializer(object);
83
85
  }
@@ -94,14 +96,14 @@ Based on Rails routes of APP_CLASS
94
96
  last_el = args[args.length - 1];
95
97
  if ((args.length > number_of_params && last_el === void 0) || ((last_el != null) && "object" === this.get_object_type(last_el) && !this.looks_like_serialized_model(last_el))) {
96
98
  options = args.pop() || {};
97
- delete options[SpecialOptionsKey];
99
+ delete options[this.configuration.special_options_key];
98
100
  return options;
99
101
  } else {
100
102
  return {};
101
103
  }
102
104
  },
103
105
  looks_like_serialized_model: function(object) {
104
- return !object[SpecialOptionsKey] && ("id" in object || "to_param" in object);
106
+ return !object[this.configuration.special_options_key] && ("id" in object || "to_param" in object);
105
107
  },
106
108
  path_identifier: function(object) {
107
109
  var property;
@@ -187,7 +189,7 @@ Based on Rails routes of APP_CLASS
187
189
  parts_options[key] = value;
188
190
  }
189
191
  }
190
- options = this.merge(defaults.default_url_options, default_options, options);
192
+ options = this.merge(this.configuration.default_url_options, default_options, options);
191
193
  result = {};
192
194
  url_parameters = {};
193
195
  result['url_parameters'] = url_parameters;
@@ -323,7 +325,7 @@ Based on Rails routes of APP_CLASS
323
325
  },
324
326
  get_prefix: function() {
325
327
  var prefix;
326
- prefix = defaults.prefix;
328
+ prefix = this.configuration.prefix;
327
329
  if (prefix !== "") {
328
330
  prefix = (prefix.match("/$") ? prefix : prefix + "/");
329
331
  }
@@ -428,36 +430,56 @@ Based on Rails routes of APP_CLASS
428
430
  }
429
431
  }
430
432
  return result;
433
+ },
434
+ namespace: function(root, namespace, routes) {
435
+ var index, j, len, part, parts;
436
+ parts = namespace.split(".");
437
+ if (parts.length === 0) {
438
+ return routes;
439
+ }
440
+ for (index = j = 0, len = parts.length; j < len; index = ++j) {
441
+ part = parts[index];
442
+ if (index < parts.length - 1) {
443
+ root = (root[part] || (root[part] = {}));
444
+ } else {
445
+ return root[part] = routes;
446
+ }
447
+ }
448
+ },
449
+ configure: function(new_config) {
450
+ return this.configuration = this.merge(this.configuration, new_config);
451
+ },
452
+ config: function() {
453
+ return this.clone(this.configuration);
454
+ },
455
+ make: function() {
456
+ var routes;
457
+ routes = ROUTES;
458
+ routes.configure = function(config) {
459
+ return Utils.configure(config);
460
+ };
461
+ routes.config = function() {
462
+ return Utils.config();
463
+ };
464
+ Object.defineProperty(routes, 'defaults', {
465
+ get: function() {
466
+ throw new Error(NAMESPACE + ".defaults is removed. Use " + NAMESPACE + ".configure() instead.");
467
+ },
468
+ set: function(value) {}
469
+ });
470
+ routes.default_serializer = function(object, prefix) {
471
+ return Utils.default_serializer(object, prefix);
472
+ };
473
+ return Utils.namespace(root, NAMESPACE, routes);
431
474
  }
432
475
  };
433
476
 
434
- createGlobalJsRoutesObject = function() {
435
- var namespace;
436
- namespace = function(mainRoot, namespaceString) {
437
- var current, parts;
438
- parts = (namespaceString ? namespaceString.split(".") : []);
439
- if (!parts.length) {
440
- return;
441
- }
442
- current = parts.shift();
443
- mainRoot[current] = mainRoot[current] || {};
444
- return namespace(mainRoot[current], parts.join("."));
445
- };
446
- namespace(root, "NAMESPACE");
447
- root.NAMESPACE = ROUTES;
448
- root.NAMESPACE.options = defaults;
449
- root.NAMESPACE.default_serializer = function(object, prefix) {
450
- return Utils.default_serializer(object, prefix);
451
- };
452
- return root.NAMESPACE;
453
- };
454
-
455
477
  if (typeof define === "function" && define.amd) {
456
478
  define([], function() {
457
- return createGlobalJsRoutesObject();
479
+ return Utils.make();
458
480
  });
459
481
  } else {
460
- createGlobalJsRoutesObject();
482
+ Utils.make();
461
483
  }
462
484
 
463
485
  }).call(this);
data/lib/routes.js.coffee CHANGED
@@ -7,12 +7,9 @@ root = (exports ? this)
7
7
 
8
8
  ParameterMissing = (@message) -> #
9
9
  ParameterMissing:: = new Error()
10
- defaults =
11
- prefix: "PREFIX"
12
- default_url_options: DEFAULT_URL_OPTIONS
13
10
 
14
11
  NodeTypes = NODE_TYPES
15
- SpecialOptionsKey = "SPECIAL_OPTIONS_KEY"
12
+ SpecialOptionsKey = SPECIAL_OPTIONS_KEY
16
13
  DeprecatedBehavior = DEPRECATED_BEHAVIOR
17
14
 
18
15
  ReservedOptions = [
@@ -25,6 +22,13 @@ ReservedOptions = [
25
22
 
26
23
  Utils =
27
24
 
25
+ configuration:
26
+ prefix: PREFIX
27
+ default_url_options: DEFAULT_URL_OPTIONS
28
+ special_options_key: SPECIAL_OPTIONS_KEY
29
+ serializer: SERIALIZER
30
+
31
+
28
32
  default_serializer: (object, prefix = null) ->
29
33
  return "" unless object?
30
34
  if !prefix and !(@get_object_type(object) is "object")
@@ -50,10 +54,10 @@ Utils =
50
54
  return "" unless s.length
51
55
  s.join("&")
52
56
 
53
- custom_serializer: SERIALIZER
54
57
  serialize: (object) ->
55
- if @custom_serializer? and @get_object_type(@custom_serializer) is "function"
56
- @custom_serializer(object)
58
+ custom_serializer = @configuration.serializer
59
+ if custom_serializer? and @get_object_type(custom_serializer) is "function"
60
+ custom_serializer(object)
57
61
  else
58
62
  @default_serializer(object)
59
63
 
@@ -67,13 +71,13 @@ Utils =
67
71
  last_el = args[args.length - 1]
68
72
  if (args.length > number_of_params and last_el == undefined) or (last_el? and "object" is @get_object_type(last_el) and !@looks_like_serialized_model(last_el))
69
73
  options = args.pop() || {}
70
- delete options[SpecialOptionsKey]
74
+ delete options[@configuration.special_options_key]
71
75
  options
72
76
  else
73
77
  {}
74
78
 
75
79
  looks_like_serialized_model: (object) ->
76
- !object[SpecialOptionsKey] and ("id" of object or "to_param" of object)
80
+ !object[@configuration.special_options_key] and ("id" of object or "to_param" of object)
77
81
 
78
82
 
79
83
  path_identifier: (object) ->
@@ -119,7 +123,7 @@ Utils =
119
123
  if @indexOf(parts, key) >= 0
120
124
  parts_options[key] = value
121
125
 
122
- options = @merge(defaults.default_url_options, default_options, options)
126
+ options = @merge(@configuration.default_url_options, default_options, options)
123
127
  result = {}
124
128
  url_parameters = {}
125
129
  result['url_parameters'] = url_parameters
@@ -248,7 +252,7 @@ Utils =
248
252
  # This method check and return prefix from options
249
253
  #
250
254
  get_prefix: ->
251
- prefix = defaults.prefix
255
+ prefix = @configuration.prefix
252
256
  prefix = (if prefix.match("/$") then prefix else "#{prefix}/") if prefix isnt ""
253
257
  prefix
254
258
 
@@ -343,26 +347,38 @@ Utils =
343
347
  (result = i for el, i in array when el is element)
344
348
  result
345
349
 
346
- # globalJsObject
347
- createGlobalJsRoutesObject = ->
348
- # namespace function, private
349
- namespace = (mainRoot, namespaceString) ->
350
- parts = (if namespaceString then namespaceString.split(".") else [])
351
- return unless parts.length
352
- current = parts.shift()
353
- mainRoot[current] = mainRoot[current] or {}
354
- namespace mainRoot[current], parts.join(".")
355
- # object
356
- namespace(root, "NAMESPACE")
357
- root.NAMESPACE = ROUTES
358
- root.NAMESPACE.options = defaults
359
- root.NAMESPACE.default_serializer = (object, prefix) ->
360
- Utils.default_serializer(object, prefix)
361
- root.NAMESPACE
350
+ namespace: (root, namespace, routes) ->
351
+ parts = namespace.split(".")
352
+ return routes if parts.length == 0
353
+ for part, index in parts
354
+ if index < parts.length - 1
355
+ root = (root[part] or= {})
356
+ else
357
+ return root[part] = routes
358
+
359
+ configure: (new_config) ->
360
+ @configuration = @merge(@configuration, new_config)
361
+
362
+ config: ->
363
+ @clone(@configuration)
364
+
365
+ make: ->
366
+ routes = ROUTES
367
+ routes.configure = (config) -> Utils.configure(config)
368
+ routes.config = -> Utils.config()
369
+ Object.defineProperty routes, 'defaults',
370
+ get: ->
371
+ throw new Error("#{NAMESPACE}.defaults is removed. Use #{NAMESPACE}.configure() instead.")
372
+ set: (value) ->
373
+
374
+ routes.default_serializer = (object, prefix) ->
375
+ Utils.default_serializer(object, prefix)
376
+ Utils.namespace(root, NAMESPACE, routes)
377
+
362
378
  # Set up Routes appropriately for the environment.
363
379
  if typeof define is "function" and define.amd
364
380
  # AMD
365
- define [], -> createGlobalJsRoutesObject()
381
+ define [], -> Utils.make()
366
382
  else
367
383
  # Browser globals
368
- createGlobalJsRoutesObject()
384
+ Utils.make()
@@ -28,7 +28,7 @@ EOF
28
28
  end
29
29
 
30
30
  it "should working from global scope" do
31
- expect(evaljs("Routes.inboxes_path()")).to eq(routes.inboxes_path())
31
+ expect(evaljs("Routes.inboxes_path()")).to eq(test_routes.inboxes_path())
32
32
  end
33
33
 
34
34
  it "should working from define function" do
@@ -36,7 +36,7 @@ EOF
36
36
  end
37
37
 
38
38
  it "should working from require" do
39
- expect(evaljs("require(['js-routes'], function(r){ return r.inboxes_path(); })")).to eq(routes.inboxes_path())
39
+ expect(evaljs("require(['js-routes'], function(r){ return r.inboxes_path(); })")).to eq(test_routes.inboxes_path())
40
40
  end
41
41
 
42
42
  end
@@ -1,5 +1,7 @@
1
+ require "spec_helper"
2
+
1
3
  describe JsRoutes, "#default_serializer" do
2
-
4
+
3
5
  before(:each) do
4
6
  evaljs(JsRoutes.generate({}))
5
7
  end
@@ -10,10 +10,6 @@ describe JsRoutes do
10
10
  describe "generated js" do
11
11
  subject { JsRoutes.generate }
12
12
 
13
- it "should set the default serializer when none is configured" do
14
- is_expected.to match(%r(custom_serializer: null,\s+serialize: function\(object\) {\s+if \(\(this\.custom_serializer != null\) && this.get_object_type\(this\.custom_serializer\) === \"function\"\) {\s+return this\.custom_serializer\(object\);\s+} else {\s+return this\.default_serializer\(object\);\s+}\s+},))
15
- end
16
-
17
13
  it "should include a comment in the header" do
18
14
  app_class = "App"
19
15
 
@@ -3,37 +3,48 @@ require 'spec_helper'
3
3
  describe JsRoutes, "options" do
4
4
 
5
5
  before(:each) do
6
- evaljs(_presetup)
6
+ evaljs(_presetup) if _presetup
7
7
  with_warnings(_warnings) do
8
8
  evaljs(JsRoutes.generate(_options))
9
9
  end
10
10
  end
11
11
 
12
- let(:_presetup) { "this;" }
12
+ let(:_presetup) { nil }
13
13
  let(:_options) { {} }
14
14
  let(:_warnings) { true }
15
15
 
16
- context "when serializer is specified" do
17
- # define custom serializer
18
- # this is a nonsense serializer, which always returns foo=bar
19
- # for all inputs
20
- let(:_presetup){ %q(function myCustomSerializer(object, prefix) { return "foo=bar"; }) }
21
- let(:_options) { {:serializer => "myCustomSerializer"} }
16
+ describe "serializer" do
17
+ context "when specified" do
18
+ # define custom serializer
19
+ # this is a nonsense serializer, which always returns foo=bar
20
+ # for all inputs
21
+ let(:_presetup){ %q(function myCustomSerializer(object, prefix) { return "foo=bar"; }) }
22
+ let(:_options) { {:serializer => "myCustomSerializer"} }
22
23
 
23
- it "should set configurable serializer" do
24
- # expect the nonsense serializer above to have appened foo=bar
25
- # to the end of the path
26
- expect(evaljs(%q(Routes.inboxes_path()))).to eql("/inboxes?foo=bar")
24
+ it "should set configurable serializer" do
25
+ # expect the nonsense serializer above to have appened foo=bar
26
+ # to the end of the path
27
+ expect(evaljs(%q(Routes.inboxes_path()))).to eql("/inboxes?foo=bar")
28
+ end
27
29
  end
28
- end
29
30
 
30
- context "when serializer is specified, but not function" do
31
- let(:_presetup){ %q(var myCustomSerializer = 1) }
32
- let(:_options) { {:serializer => "myCustomSerializer"} }
31
+ context "when specified, but not function" do
32
+ let(:_presetup){ %q(var myCustomSerializer = 1) }
33
+ let(:_options) { {:serializer => "myCustomSerializer"} }
33
34
 
34
- it "should set configurable serializer" do
35
- # expect to use default
36
- expect(evaljs(%q(Routes.inboxes_path({a: 1})))).to eql("/inboxes?a=1")
35
+ it "should set configurable serializer" do
36
+ # expect to use default
37
+ expect(evaljs(%q(Routes.inboxes_path({a: 1})))).to eql("/inboxes?a=1")
38
+ end
39
+ end
40
+
41
+ context "when configured in js" do
42
+ let(:_options) { {:serializer =>%q(function (object, prefix) { return "foo=bar"; })} }
43
+
44
+ it "uses JS serializer" do
45
+ evaljs("Routes.configure({serializer: function (object, prefix) { return 'bar=baz'; }})")
46
+ expect(evaljs(%q(Routes.inboxes_path({a: 1})))).to eql("/inboxes?bar=baz")
47
+ end
37
48
  end
38
49
  end
39
50
 
@@ -84,12 +95,13 @@ describe JsRoutes, "options" do
84
95
  let(:_options) { {:prefix => "/myprefix/" } }
85
96
 
86
97
  it "should render routing with prefix" do
87
- expect(evaljs("Routes.inbox_path(1)")).to eq("/myprefix#{routes.inbox_path(1)}")
98
+ expect(evaljs("Routes.inbox_path(1)")).to eq("/myprefix#{test_routes.inbox_path(1)}")
88
99
  end
89
100
 
90
101
  it "should render routing with prefix set in JavaScript" do
91
- evaljs("Routes.options.prefix = '/newprefix/'")
92
- expect(evaljs("Routes.inbox_path(1)")).to eq("/newprefix#{routes.inbox_path(1)}")
102
+ evaljs("Routes.configure({prefix: '/newprefix/'})")
103
+ expect(evaljs("Routes.config().prefix")).to eq("/newprefix/")
104
+ expect(evaljs("Routes.inbox_path(1)")).to eq("/newprefix#{test_routes.inbox_path(1)}")
93
105
  end
94
106
 
95
107
  end
@@ -99,7 +111,7 @@ describe JsRoutes, "options" do
99
111
  let(:_options) { {:prefix => "http://localhost:3000" } }
100
112
 
101
113
  it "should render routing with prefix" do
102
- expect(evaljs("Routes.inbox_path(1)")).to eq(_options[:prefix] + routes.inbox_path(1))
114
+ expect(evaljs("Routes.inbox_path(1)")).to eq(_options[:prefix] + test_routes.inbox_path(1))
103
115
  end
104
116
  end
105
117
 
@@ -108,46 +120,46 @@ describe JsRoutes, "options" do
108
120
  let(:_options) { {:prefix => "/myprefix" } }
109
121
 
110
122
  it "should render routing with prefix" do
111
- expect(evaljs("Routes.inbox_path(1)")).to eq("/myprefix#{routes.inbox_path(1)}")
123
+ expect(evaljs("Routes.inbox_path(1)")).to eq("/myprefix#{test_routes.inbox_path(1)}")
112
124
  end
113
125
 
114
126
  it "should render routing with prefix set in JavaScript" do
115
- evaljs("Routes.options.prefix = '/newprefix'")
116
- expect(evaljs("Routes.inbox_path(1)")).to eq("/newprefix#{routes.inbox_path(1)}")
127
+ evaljs("Routes.configure({prefix: '/newprefix/'})")
128
+ expect(evaljs("Routes.inbox_path(1)")).to eq("/newprefix#{test_routes.inbox_path(1)}")
117
129
  end
118
130
 
119
131
  end
120
132
 
121
- context "when default_format is specified" do
122
- let(:_options) { {:default_format => "json"} }
133
+ context "when default format is specified" do
134
+ let(:_options) { {:default_url_options => {format: "json"}} }
123
135
  let(:_warnings) { nil }
124
136
 
125
137
  it "should render routing with default_format" do
126
- expect(evaljs("Routes.inbox_path(1)")).to eq(routes.inbox_path(1, :format => "json"))
138
+ expect(evaljs("Routes.inbox_path(1)")).to eq(test_routes.inbox_path(1, :format => "json"))
127
139
  end
128
140
 
129
141
  it "should render routing with default_format and zero object" do
130
- expect(evaljs("Routes.inbox_path(0)")).to eq(routes.inbox_path(0, :format => "json"))
142
+ expect(evaljs("Routes.inbox_path(0)")).to eq(test_routes.inbox_path(0, :format => "json"))
131
143
  end
132
144
 
133
145
  it "should override default_format when spefified implicitly" do
134
- expect(evaljs("Routes.inbox_path(1, {format: 'xml'})")).to eq(routes.inbox_path(1, :format => "xml"))
146
+ expect(evaljs("Routes.inbox_path(1, {format: 'xml'})")).to eq(test_routes.inbox_path(1, :format => "xml"))
135
147
  end
136
148
 
137
149
  it "should override nullify implicitly when specified implicitly" do
138
- expect(evaljs("Routes.inbox_path(1, {format: null})")).to eq(routes.inbox_path(1))
150
+ expect(evaljs("Routes.inbox_path(1, {format: null})")).to eq(test_routes.inbox_path(1))
139
151
  end
140
152
 
141
153
 
142
154
  it "shouldn't require the format" do
143
155
  pending if Rails.version < "4.0"
144
- expect(evaljs("Routes.json_only_path()")).to eq(routes.json_only_path(:format => 'json'))
156
+ expect(evaljs("Routes.json_only_path()")).to eq(test_routes.json_only_path(:format => 'json'))
145
157
  end
146
158
  end
147
159
 
148
160
  it "shouldn't include the format when {:format => false} is specified" do
149
- expect(evaljs("Routes.no_format_path()")).to eq(routes.no_format_path())
150
- expect(evaljs("Routes.no_format_path({format: 'json'})")).to eq(routes.no_format_path(format: 'json'))
161
+ expect(evaljs("Routes.no_format_path()")).to eq(test_routes.no_format_path())
162
+ expect(evaljs("Routes.no_format_path({format: 'json'})")).to eq(test_routes.no_format_path(format: 'json'))
151
163
  end
152
164
 
153
165
  describe "when namespace option is specified" do
@@ -189,14 +201,14 @@ describe JsRoutes, "options" do
189
201
  context "provided" do
190
202
  let(:_options) { { :default_url_options => { :optional_id => "12", :format => "json" } } }
191
203
  it "should use this opions to fill optional parameters" do
192
- expect(evaljs("Routes.things_path()")).to eq(routes.things_path(:optional_id => 12, :format => "json"))
204
+ expect(evaljs("Routes.things_path()")).to eq(test_routes.things_path(:optional_id => 12, :format => "json"))
193
205
  end
194
206
  end
195
207
 
196
208
  context "not provided" do
197
209
  let(:_options) { { :default_url_options => { :format => "json" } } }
198
210
  it "breaks" do
199
- expect(evaljs("Routes.foo_all_path()")).to eq(routes.foo_all_path(:format => "json"))
211
+ expect(evaljs("Routes.foo_all_path()")).to eq(test_routes.foo_all_path(:format => "json"))
200
212
  end
201
213
  end
202
214
  end
@@ -204,7 +216,15 @@ describe JsRoutes, "options" do
204
216
  context "with required route parts" do
205
217
  let(:_options) { {:default_url_options => {:inbox_id => "12"}} }
206
218
  it "should use this opions to fill optional parameters" do
207
- expect(evaljs("Routes.inbox_messages_path()")).to eq(routes.inbox_messages_path(:inbox_id => 12))
219
+ expect(evaljs("Routes.inbox_messages_path()")).to eq(test_routes.inbox_messages_path(:inbox_id => 12))
220
+ end
221
+ end
222
+
223
+ context "when overwritten on JS level" do
224
+ let(:_options) { { :default_url_options => { :format => "json" } } }
225
+ it "uses JS defined value" do
226
+ evaljs("Routes.configure({default_url_options: {format: 'xml'}})")
227
+ expect(evaljs("Routes.inboxes_path()")).to eq(test_routes.inboxes_path(format: 'xml'))
208
228
  end
209
229
  end
210
230
  end
@@ -213,33 +233,33 @@ describe JsRoutes, "options" do
213
233
  context "with default option" do
214
234
  let(:_options) { Hash.new }
215
235
  it "should working in params" do
216
- expect(evaljs("Routes.inbox_path(1, {trailing_slash: true})")).to eq(routes.inbox_path(1, :trailing_slash => true))
236
+ expect(evaljs("Routes.inbox_path(1, {trailing_slash: true})")).to eq(test_routes.inbox_path(1, :trailing_slash => true))
217
237
  end
218
238
 
219
239
  it "should working with additional params" do
220
- expect(evaljs("Routes.inbox_path(1, {trailing_slash: true, test: 'params'})")).to eq(routes.inbox_path(1, :trailing_slash => true, :test => 'params'))
240
+ expect(evaljs("Routes.inbox_path(1, {trailing_slash: true, test: 'params'})")).to eq(test_routes.inbox_path(1, :trailing_slash => true, :test => 'params'))
221
241
  end
222
242
  end
223
243
 
224
244
  context "with default_url_options option" do
225
245
  let(:_options) { {:default_url_options => {:trailing_slash => true}} }
226
246
  it "should working" do
227
- expect(evaljs("Routes.inbox_path(1, {test: 'params'})")).to eq(routes.inbox_path(1, :trailing_slash => true, :test => 'params'))
247
+ expect(evaljs("Routes.inbox_path(1, {test: 'params'})")).to eq(test_routes.inbox_path(1, :trailing_slash => true, :test => 'params'))
228
248
  end
229
249
 
230
250
  it "should remove it by params" do
231
- expect(evaljs("Routes.inbox_path(1, {trailing_slash: false})")).to eq(routes.inbox_path(1))
251
+ expect(evaljs("Routes.inbox_path(1, {trailing_slash: false})")).to eq(test_routes.inbox_path(1))
232
252
  end
233
253
  end
234
254
 
235
255
  context "with disabled default_url_options option" do
236
256
  let(:_options) { {:default_url_options => {:trailing_slash => false}} }
237
257
  it "should not use trailing_slash" do
238
- expect(evaljs("Routes.inbox_path(1, {test: 'params'})")).to eq(routes.inbox_path(1, :test => 'params'))
258
+ expect(evaljs("Routes.inbox_path(1, {test: 'params'})")).to eq(test_routes.inbox_path(1, :test => 'params'))
239
259
  end
240
260
 
241
261
  it "should use it by params" do
242
- expect(evaljs("Routes.inbox_path(1, {trailing_slash: true})")).to eq(routes.inbox_path(1, :trailing_slash => true))
262
+ expect(evaljs("Routes.inbox_path(1, {trailing_slash: true})")).to eq(test_routes.inbox_path(1, :trailing_slash => true))
243
263
  end
244
264
  end
245
265
  end
@@ -248,7 +268,7 @@ describe JsRoutes, "options" do
248
268
  context "with default option" do
249
269
  let(:_options) { Hash.new }
250
270
  it "should use snake case routes" do
251
- expect(evaljs("Routes.inbox_path(1)")).to eq(routes.inbox_path(1))
271
+ expect(evaljs("Routes.inbox_path(1)")).to eq(test_routes.inbox_path(1))
252
272
  expect(evaljs("Routes.inboxPath")).to be_nil
253
273
  end
254
274
  end
@@ -258,8 +278,8 @@ describe JsRoutes, "options" do
258
278
  it "should generate camel case routes" do
259
279
  expect(evaljs("Routes.inbox_path")).to be_nil
260
280
  expect(evaljs("Routes.inboxPath")).not_to be_nil
261
- expect(evaljs("Routes.inboxPath(1)")).to eq(routes.inbox_path(1))
262
- expect(evaljs("Routes.inboxMessagesPath(10)")).to eq(routes.inbox_messages_path(:inbox_id => 10))
281
+ expect(evaljs("Routes.inboxPath(1)")).to eq(test_routes.inbox_path(1))
282
+ expect(evaljs("Routes.inboxMessagesPath(10)")).to eq(test_routes.inbox_messages_path(:inbox_id => 10))
263
283
  end
264
284
  end
265
285
  end
@@ -268,74 +288,29 @@ describe JsRoutes, "options" do
268
288
  context "with default option" do
269
289
  let(:_options) { Hash.new }
270
290
  it "should generate only path links" do
271
- expect(evaljs("Routes.inbox_path(1)")).to eq(routes.inbox_path(1))
291
+ expect(evaljs("Routes.inbox_path(1)")).to eq(test_routes.inbox_path(1))
272
292
  expect(evaljs("Routes.inbox_url")).to be_nil
273
293
  end
274
294
  end
275
295
 
276
- context 'with deprecated, non-boolean config value' do
277
- around(:each) do |example|
278
- ActiveSupport::Deprecation.silence do
279
- example.run
280
- end
281
- end
282
-
283
- context "with host" do
284
- let(:_options) { { :url_links => "http://localhost" } }
285
- it "should generate path and url links" do
286
- expect(evaljs("Routes.inbox_path")).not_to be_nil
287
- expect(evaljs("Routes.inbox_url")).not_to be_nil
288
- expect(evaljs("Routes.inbox_path(1)")).to eq(routes.inbox_path(1))
289
- expect(evaljs("Routes.inbox_url(1)")).to eq("http://localhost#{routes.inbox_path(1)}")
290
- expect(evaljs("Routes.inbox_url(1, { test_key: \"test_val\" })")).to eq("http://localhost#{routes.inbox_path(1, :test_key => "test_val")}")
291
- end
292
- end
293
-
294
- context "with invalid host" do
295
- it "should raise error" do
296
- expect { JsRoutes.generate({ :url_links => "localhost" }) }.to raise_error RuntimeError
297
- end
298
- end
299
-
300
- context "with host and camel_case" do
301
- let(:_options) { { :camel_case => true, :url_links => "http://localhost" } }
302
- it "should generate path and url links" do
303
- expect(evaljs("Routes.inboxPath")).not_to be_nil
304
- expect(evaljs("Routes.inboxUrl")).not_to be_nil
305
- expect(evaljs("Routes.inboxPath(1)")).to eq(routes.inbox_path(1))
306
- expect(evaljs("Routes.inboxUrl(1)")).to eq("http://localhost#{routes.inbox_path(1)}")
307
- end
308
- end
309
-
310
- context "with host and prefix" do
311
- let(:_options) { { :prefix => "/api", :url_links => "https://example.com" } }
312
- it "should generate path and url links" do
313
- expect(evaljs("Routes.inbox_path")).not_to be_nil
314
- expect(evaljs("Routes.inbox_url")).not_to be_nil
315
- expect(evaljs("Routes.inbox_path(1)")).to eq("/api#{routes.inbox_path(1)}")
316
- expect(evaljs("Routes.inbox_url(1)")).to eq("https://example.com/api#{routes.inbox_path(1)}")
317
- end
318
- end
319
- end
320
-
321
296
  context "when configuring with default_url_options" do
322
297
  context "when only host option is specified" do
323
298
  let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com"} } }
324
299
 
325
300
  it "uses the specified host, defaults protocol to http, defaults port to 80 (leaving it blank)" do
326
- expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com#{routes.inbox_path(1)}")
301
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com#{test_routes.inbox_path(1)}")
327
302
  end
328
303
 
329
304
  it "does not override protocol when specified in route" do
330
- expect(evaljs("Routes.new_session_url()")).to eq("https://example.com#{routes.new_session_path}")
305
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com#{test_routes.new_session_path}")
331
306
  end
332
307
 
333
308
  it "does not override host when specified in route" do
334
- expect(evaljs("Routes.sso_url()")).to eq(routes.sso_url)
309
+ expect(evaljs("Routes.sso_url()")).to eq(test_routes.sso_url)
335
310
  end
336
311
 
337
312
  it "does not override port when specified in route" do
338
- expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080#{routes.portals_path}")
313
+ expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080#{test_routes.portals_path}")
339
314
  end
340
315
  end
341
316
 
@@ -343,19 +318,19 @@ describe JsRoutes, "options" do
343
318
  let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com", :protocol => "ftp"} } }
344
319
 
345
320
  it "uses the specified protocol and host, defaults port to 80 (leaving it blank)" do
346
- expect(evaljs("Routes.inbox_url(1)")).to eq("ftp://example.com#{routes.inbox_path(1)}")
321
+ expect(evaljs("Routes.inbox_url(1)")).to eq("ftp://example.com#{test_routes.inbox_path(1)}")
347
322
  end
348
323
 
349
324
  it "does not override protocol when specified in route" do
350
- expect(evaljs("Routes.new_session_url()")).to eq("https://example.com#{routes.new_session_path}")
325
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com#{test_routes.new_session_path}")
351
326
  end
352
327
 
353
328
  it "does not override host when host is specified in route" do
354
- expect(evaljs("Routes.sso_url()")).to eq("ftp://sso.example.com#{routes.sso_path}")
329
+ expect(evaljs("Routes.sso_url()")).to eq("ftp://sso.example.com#{test_routes.sso_path}")
355
330
  end
356
331
 
357
332
  it "does not override port when specified in route" do
358
- expect(evaljs("Routes.portals_url()")).to eq("ftp://example.com:8080#{routes.portals_path}")
333
+ expect(evaljs("Routes.portals_url()")).to eq("ftp://example.com:8080#{test_routes.portals_path}")
359
334
  end
360
335
  end
361
336
 
@@ -363,49 +338,49 @@ describe JsRoutes, "options" do
363
338
  let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com", :port => 3000} } }
364
339
 
365
340
  it "uses the specified host and port, defaults protocol to http" do
366
- expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com:3000#{routes.inbox_path(1)}")
341
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com:3000#{test_routes.inbox_path(1)}")
367
342
  end
368
343
 
369
344
  it "does not override protocol when specified in route" do
370
- expect(evaljs("Routes.new_session_url()")).to eq("https://example.com:3000#{routes.new_session_path}")
345
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com:3000#{test_routes.new_session_path}")
371
346
  end
372
347
 
373
348
  it "does not override host, protocol, or port when host is specified in route" do
374
- expect(evaljs("Routes.sso_url()")).to eq("http://sso.example.com:3000" + routes.sso_path)
349
+ expect(evaljs("Routes.sso_url()")).to eq("http://sso.example.com:3000" + test_routes.sso_path)
375
350
  end
376
351
 
377
352
  it "does not override port when specified in route" do
378
- expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080#{routes.portals_path}")
353
+ expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080#{test_routes.portals_path}")
379
354
  end
380
355
  end
381
356
 
382
357
  context "with camel_case option" do
383
358
  let(:_options) { { :camel_case => true, :url_links => true, :default_url_options => {:host => "example.com"} } }
384
359
  it "should generate path and url links" do
385
- expect(evaljs("Routes.inboxUrl(1)")).to eq("http://example.com#{routes.inbox_path(1)}")
386
- expect(evaljs("Routes.newSessionUrl()")).to eq("https://example.com#{routes.new_session_path}")
387
- expect(evaljs("Routes.ssoUrl()")).to eq(routes.sso_url)
388
- expect(evaljs("Routes.portalsUrl()")).to eq("http://example.com:8080#{routes.portals_path}")
360
+ expect(evaljs("Routes.inboxUrl(1)")).to eq("http://example.com#{test_routes.inbox_path(1)}")
361
+ expect(evaljs("Routes.newSessionUrl()")).to eq("https://example.com#{test_routes.new_session_path}")
362
+ expect(evaljs("Routes.ssoUrl()")).to eq(test_routes.sso_url)
363
+ expect(evaljs("Routes.portalsUrl()")).to eq("http://example.com:8080#{test_routes.portals_path}")
389
364
  end
390
365
  end
391
366
 
392
367
  context "with prefix option" do
393
368
  let(:_options) { { :prefix => "/api", :url_links => true, :default_url_options => {:host => 'example.com'} } }
394
369
  it "should generate path and url links" do
395
- expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com/api#{routes.inbox_path(1)}")
396
- expect(evaljs("Routes.new_session_url()")).to eq("https://example.com/api#{routes.new_session_path}")
397
- expect(evaljs("Routes.sso_url()")).to eq("http://sso.example.com/api#{routes.sso_path}")
398
- expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080/api#{routes.portals_path}")
370
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com/api#{test_routes.inbox_path(1)}")
371
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com/api#{test_routes.new_session_path}")
372
+ expect(evaljs("Routes.sso_url()")).to eq("http://sso.example.com/api#{test_routes.sso_path}")
373
+ expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080/api#{test_routes.portals_path}")
399
374
  end
400
375
  end
401
376
 
402
377
  context "with compact option" do
403
378
  let(:_options) { { :compact => true, :url_links => true, :default_url_options => {:host => 'example.com'} } }
404
379
  it "does not affect url helpers" do
405
- expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com#{routes.inbox_path(1)}")
406
- expect(evaljs("Routes.new_session_url()")).to eq("https://example.com#{routes.new_session_path}")
407
- expect(evaljs("Routes.sso_url()")).to eq(routes.sso_url)
408
- expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080#{routes.portals_path}")
380
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com#{test_routes.inbox_path(1)}")
381
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com#{test_routes.new_session_path}")
382
+ expect(evaljs("Routes.sso_url()")).to eq(test_routes.sso_url)
383
+ expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080#{test_routes.portals_path}")
409
384
  end
410
385
  end
411
386
  end
@@ -430,23 +405,23 @@ describe JsRoutes, "options" do
430
405
  it "uses the current host" do
431
406
  expect(evaljs("Routes.inbox_path")).not_to be_nil
432
407
  expect(evaljs("Routes.inbox_url")).not_to be_nil
433
- expect(evaljs("Routes.inbox_url(1)")).to eq("http://current.example.com#{routes.inbox_path(1)}")
434
- expect(evaljs("Routes.inbox_url(1, { test_key: \"test_val\" })")).to eq("http://current.example.com#{routes.inbox_path(1, :test_key => "test_val")}")
435
- expect(evaljs("Routes.new_session_url()")).to eq("https://current.example.com#{routes.new_session_path}")
436
- expect(evaljs("Routes.sso_url()")).to eq("http://sso.example.com#{routes.sso_path}")
408
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://current.example.com#{test_routes.inbox_path(1)}")
409
+ expect(evaljs("Routes.inbox_url(1, { test_key: \"test_val\" })")).to eq("http://current.example.com#{test_routes.inbox_path(1, :test_key => "test_val")}")
410
+ expect(evaljs("Routes.new_session_url()")).to eq("https://current.example.com#{test_routes.new_session_path}")
411
+ expect(evaljs("Routes.sso_url()")).to eq("http://sso.example.com#{test_routes.sso_path}")
437
412
 
438
413
  end
439
414
 
440
415
  it "uses host option as an argument" do
441
- expect(evaljs("Routes.portals_url({host: 'another.com'})")).to eq(routes.portals_url(host: 'another.com'))
416
+ expect(evaljs("Routes.portals_url({host: 'another.com'})")).to eq(test_routes.portals_url(host: 'another.com'))
442
417
  end
443
418
 
444
419
  it "uses port option as an argument" do
445
- expect(evaljs("Routes.portals_url({host: 'localhost', port: 8080})")).to eq(routes.portals_url(host: 'localhost', port: 8080))
420
+ expect(evaljs("Routes.portals_url({host: 'localhost', port: 8080})")).to eq(test_routes.portals_url(host: 'localhost', port: 8080))
446
421
  end
447
422
 
448
423
  it "uses protocol option as an argument" do
449
- expect(evaljs("Routes.portals_url({host: 'localhost', protocol: 'https'})")).to eq(routes.portals_url(protocol: 'https', host: 'localhost'))
424
+ expect(evaljs("Routes.portals_url({host: 'localhost', protocol: 'https'})")).to eq(test_routes.portals_url(protocol: 'https', host: 'localhost'))
450
425
  end
451
426
  end
452
427
  end
@@ -456,23 +431,21 @@ describe JsRoutes, "options" do
456
431
  let(:_options) { { :compact => true } }
457
432
  it "removes _path suffix from path helpers" do
458
433
  expect(evaljs("Routes.inbox_path")).to be_nil
459
- expect(evaljs("Routes.inboxes()")).to eq(routes.inboxes_path())
460
- expect(evaljs("Routes.inbox(2)")).to eq(routes.inbox_path(2))
434
+ expect(evaljs("Routes.inboxes()")).to eq(test_routes.inboxes_path())
435
+ expect(evaljs("Routes.inbox(2)")).to eq(test_routes.inbox_path(2))
461
436
  end
462
437
 
463
438
  context "with url_links option" do
464
- context "with deprecated url_links config value" do
465
- around(:each) do |example|
466
- ActiveSupport::Deprecation.silence do
467
- example.run
468
- end
439
+ around(:each) do |example|
440
+ ActiveSupport::Deprecation.silence do
441
+ example.run
469
442
  end
443
+ end
470
444
 
471
- let(:_options) { { :compact => true, :url_links => "http://localhost" } }
472
- it "should not strip urls" do
473
- expect(evaljs("Routes.inbox(1)")).to eq(routes.inbox_path(1))
474
- expect(evaljs("Routes.inbox_url(1)")).to eq("http://localhost#{routes.inbox_path(1)}")
475
- end
445
+ let(:_options) { { :compact => true, :url_links => true, default_url_options: {host: 'localhost'} } }
446
+ it "should not strip urls" do
447
+ expect(evaljs("Routes.inbox(1)")).to eq(test_routes.inbox_path(1))
448
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://localhost#{test_routes.inbox_path(1)}")
476
449
  end
477
450
  end
478
451
  end
@@ -483,7 +456,7 @@ describe JsRoutes, "options" do
483
456
  expect {
484
457
  expect(evaljs("Routes.inbox_message_path({inbox_id: 1, id: 2, _options: true})")).to eq("")
485
458
  }.to raise_error(js_error_class)
486
- expect(evaljs("Routes.inbox_message_path({inbox_id: 1, id: 2, __options__: true})")).to eq(routes.inbox_message_path(inbox_id: 1, id: 2))
459
+ expect(evaljs("Routes.inbox_message_path({inbox_id: 1, id: 2, __options__: true})")).to eq(test_routes.inbox_message_path(inbox_id: 1, id: 2))
487
460
  end
488
461
  end
489
462