js-routes 1.1.0 → 1.1.1
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/lib/js_routes/version.rb +1 -1
- data/lib/routes.js +25 -46
- data/lib/routes.js.coffee +5 -5
- data/spec/js_routes/default_serializer_spec.rb +2 -2
- data/spec/js_routes/generated_javascript_spec.rb +1 -1
- data/spec/js_routes/options_spec.rb +14 -5
- data/spec/js_routes/rails_routes_compatibility_spec.rb +4 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f0198d8ff6f861e2cee2326141e2bb1cbfd974c
|
4
|
+
data.tar.gz: 1052236ee191127a860aae5a71d22a1b0c7b8613
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6894424f650a910ba90b4a8ff1700bf564b320cfe7d124dfe3026a67c988d07ce06e745f8861de83e6c1add80a5bb71a91ecfa329e6523cfff13a344873253d2
|
7
|
+
data.tar.gz: af3bd894e3936bc2cf1621e0f4b19980887df8d8a3ee3dab99949290328d02ab70b845ea2e09021df951c299ce0d26110e9309fd7bb017497b8702b3574ab52c
|
data/lib/js_routes/version.rb
CHANGED
data/lib/routes.js
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
/*
|
2
2
|
File generated by js-routes GEM_VERSION
|
3
3
|
Based on Rails routes of APP_CLASS
|
4
|
-
*/
|
5
|
-
|
4
|
+
*/
|
6
5
|
|
7
6
|
(function() {
|
8
7
|
var NodeTypes, ParameterMissing, Utils, createGlobalJsRoutesObject, defaults, root,
|
9
|
-
|
8
|
+
hasProp = {}.hasOwnProperty;
|
10
9
|
|
11
10
|
root = typeof exports !== "undefined" && exports !== null ? exports : this;
|
12
11
|
|
@@ -25,12 +24,11 @@ Based on Rails routes of APP_CLASS
|
|
25
24
|
|
26
25
|
Utils = {
|
27
26
|
default_serializer: function(object, prefix) {
|
28
|
-
var element, i,
|
29
|
-
|
27
|
+
var element, i, j, key, len, prop, s;
|
30
28
|
if (prefix == null) {
|
31
29
|
prefix = null;
|
32
30
|
}
|
33
|
-
if (
|
31
|
+
if (object == null) {
|
34
32
|
return "";
|
35
33
|
}
|
36
34
|
if (!prefix && !(this.get_object_type(object) === "object")) {
|
@@ -39,27 +37,27 @@ Based on Rails routes of APP_CLASS
|
|
39
37
|
s = [];
|
40
38
|
switch (this.get_object_type(object)) {
|
41
39
|
case "array":
|
42
|
-
for (i =
|
40
|
+
for (i = j = 0, len = object.length; j < len; i = ++j) {
|
43
41
|
element = object[i];
|
44
42
|
s.push(this.default_serializer(element, prefix + "[]"));
|
45
43
|
}
|
46
44
|
break;
|
47
45
|
case "object":
|
48
46
|
for (key in object) {
|
49
|
-
if (!
|
47
|
+
if (!hasProp.call(object, key)) continue;
|
50
48
|
prop = object[key];
|
51
49
|
if (!(prop != null)) {
|
52
50
|
continue;
|
53
51
|
}
|
54
52
|
if (prefix != null) {
|
55
|
-
key =
|
53
|
+
key = prefix + "[" + key + "]";
|
56
54
|
}
|
57
55
|
s.push(this.default_serializer(prop, key));
|
58
56
|
}
|
59
57
|
break;
|
60
58
|
default:
|
61
|
-
if (object) {
|
62
|
-
s.push(
|
59
|
+
if (object != null) {
|
60
|
+
s.push((encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
|
63
61
|
}
|
64
62
|
}
|
65
63
|
if (!s.length) {
|
@@ -67,39 +65,34 @@ Based on Rails routes of APP_CLASS
|
|
67
65
|
}
|
68
66
|
return s.join("&");
|
69
67
|
},
|
68
|
+
custom_serializer: SERIALIZER,
|
70
69
|
serialize: function(object) {
|
71
|
-
|
72
|
-
|
73
|
-
custom_serializer = SERIALIZER;
|
74
|
-
if (custom_serializer) {
|
75
|
-
return custom_serializer(object);
|
70
|
+
if ((this.custom_serializer != null) && this.get_object_type(this.custom_serializer) === "function") {
|
71
|
+
return this.custom_serializer(object);
|
76
72
|
} else {
|
77
73
|
return this.default_serializer(object);
|
78
74
|
}
|
79
75
|
},
|
80
76
|
clean_path: function(path) {
|
81
77
|
var last_index;
|
82
|
-
|
83
78
|
path = path.split("://");
|
84
79
|
last_index = path.length - 1;
|
85
80
|
path[last_index] = path[last_index].replace(/\/+/g, "/");
|
86
81
|
return path.join("://");
|
87
82
|
},
|
88
83
|
set_default_url_options: function(optional_parts, options) {
|
89
|
-
var i,
|
90
|
-
|
91
|
-
|
92
|
-
for (i = _i = 0, _len = optional_parts.length; _i < _len; i = ++_i) {
|
84
|
+
var i, j, len, part, results;
|
85
|
+
results = [];
|
86
|
+
for (i = j = 0, len = optional_parts.length; j < len; i = ++j) {
|
93
87
|
part = optional_parts[i];
|
94
88
|
if (!options.hasOwnProperty(part) && defaults.default_url_options.hasOwnProperty(part)) {
|
95
|
-
|
89
|
+
results.push(options[part] = defaults.default_url_options[part]);
|
96
90
|
}
|
97
91
|
}
|
98
|
-
return
|
92
|
+
return results;
|
99
93
|
},
|
100
94
|
extract_anchor: function(options) {
|
101
95
|
var anchor;
|
102
|
-
|
103
96
|
anchor = "";
|
104
97
|
if (options.hasOwnProperty("anchor")) {
|
105
98
|
anchor = "#" + options.anchor;
|
@@ -109,7 +102,6 @@ Based on Rails routes of APP_CLASS
|
|
109
102
|
},
|
110
103
|
extract_trailing_slash: function(options) {
|
111
104
|
var trailing_slash;
|
112
|
-
|
113
105
|
trailing_slash = false;
|
114
106
|
if (defaults.default_url_options.hasOwnProperty("trailing_slash")) {
|
115
107
|
trailing_slash = defaults.default_url_options.trailing_slash;
|
@@ -122,7 +114,6 @@ Based on Rails routes of APP_CLASS
|
|
122
114
|
},
|
123
115
|
extract_options: function(number_of_params, args) {
|
124
116
|
var last_el;
|
125
|
-
|
126
117
|
last_el = args[args.length - 1];
|
127
118
|
if (args.length > number_of_params || ((last_el != null) && "object" === this.get_object_type(last_el) && !this.look_like_serialized_model(last_el))) {
|
128
119
|
return args.pop();
|
@@ -135,7 +126,6 @@ Based on Rails routes of APP_CLASS
|
|
135
126
|
},
|
136
127
|
path_identifier: function(object) {
|
137
128
|
var property;
|
138
|
-
|
139
129
|
if (object === 0) {
|
140
130
|
return "0";
|
141
131
|
}
|
@@ -159,23 +149,21 @@ Based on Rails routes of APP_CLASS
|
|
159
149
|
},
|
160
150
|
clone: function(obj) {
|
161
151
|
var attr, copy, key;
|
162
|
-
|
163
152
|
if ((obj == null) || "object" !== this.get_object_type(obj)) {
|
164
153
|
return obj;
|
165
154
|
}
|
166
155
|
copy = obj.constructor();
|
167
156
|
for (key in obj) {
|
168
|
-
if (!
|
157
|
+
if (!hasProp.call(obj, key)) continue;
|
169
158
|
attr = obj[key];
|
170
159
|
copy[key] = attr;
|
171
160
|
}
|
172
161
|
return copy;
|
173
162
|
},
|
174
163
|
prepare_parameters: function(required_parameters, actual_parameters, options) {
|
175
|
-
var i,
|
176
|
-
|
164
|
+
var i, j, len, result, val;
|
177
165
|
result = this.clone(options) || {};
|
178
|
-
for (i =
|
166
|
+
for (i = j = 0, len = required_parameters.length; j < len; i = ++j) {
|
179
167
|
val = required_parameters[i];
|
180
168
|
if (i < actual_parameters.length) {
|
181
169
|
result[val] = actual_parameters[i];
|
@@ -185,7 +173,6 @@ Based on Rails routes of APP_CLASS
|
|
185
173
|
},
|
186
174
|
build_path: function(required_parameters, optional_parts, route, args) {
|
187
175
|
var anchor, opts, parameters, result, trailing_slash, url, url_params;
|
188
|
-
|
189
176
|
args = Array.prototype.slice.call(args);
|
190
177
|
opts = this.extract_options(required_parameters.length, args);
|
191
178
|
if (args.length > required_parameters.length) {
|
@@ -208,7 +195,6 @@ Based on Rails routes of APP_CLASS
|
|
208
195
|
},
|
209
196
|
visit: function(route, parameters, optional) {
|
210
197
|
var left, left_part, right, right_part, type, value;
|
211
|
-
|
212
198
|
if (optional == null) {
|
213
199
|
optional = false;
|
214
200
|
}
|
@@ -247,7 +233,6 @@ Based on Rails routes of APP_CLASS
|
|
247
233
|
},
|
248
234
|
build_path_spec: function(route, wildcard) {
|
249
235
|
var left, right, type;
|
250
|
-
|
251
236
|
if (wildcard == null) {
|
252
237
|
wildcard = false;
|
253
238
|
}
|
@@ -276,7 +261,6 @@ Based on Rails routes of APP_CLASS
|
|
276
261
|
},
|
277
262
|
visit_globbing: function(route, parameters, optional) {
|
278
263
|
var left, right, type, value;
|
279
|
-
|
280
264
|
type = route[0], left = route[1], right = route[2];
|
281
265
|
if (left.replace(/^\*/i, "") !== left) {
|
282
266
|
route[1] = left = left.replace(/^\*/i, "");
|
@@ -297,16 +281,14 @@ Based on Rails routes of APP_CLASS
|
|
297
281
|
},
|
298
282
|
get_prefix: function() {
|
299
283
|
var prefix;
|
300
|
-
|
301
284
|
prefix = defaults.prefix;
|
302
285
|
if (prefix !== "") {
|
303
|
-
prefix = (prefix.match("/$") ? prefix :
|
286
|
+
prefix = (prefix.match("/$") ? prefix : prefix + "/");
|
304
287
|
}
|
305
288
|
return prefix;
|
306
289
|
},
|
307
290
|
route: function(required_parts, optional_parts, route_spec) {
|
308
291
|
var path_fn;
|
309
|
-
|
310
292
|
path_fn = function() {
|
311
293
|
return Utils.build_path(required_parts, optional_parts, route_spec, arguments);
|
312
294
|
};
|
@@ -318,15 +300,14 @@ Based on Rails routes of APP_CLASS
|
|
318
300
|
},
|
319
301
|
_classToTypeCache: null,
|
320
302
|
_classToType: function() {
|
321
|
-
var
|
322
|
-
|
303
|
+
var j, len, name, ref;
|
323
304
|
if (this._classToTypeCache != null) {
|
324
305
|
return this._classToTypeCache;
|
325
306
|
}
|
326
307
|
this._classToTypeCache = {};
|
327
|
-
|
328
|
-
for (
|
329
|
-
name =
|
308
|
+
ref = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
|
309
|
+
for (j = 0, len = ref.length; j < len; j++) {
|
310
|
+
name = ref[j];
|
330
311
|
this._classToTypeCache["[object " + name + "]"] = name.toLowerCase();
|
331
312
|
}
|
332
313
|
return this._classToTypeCache;
|
@@ -348,10 +329,8 @@ Based on Rails routes of APP_CLASS
|
|
348
329
|
|
349
330
|
createGlobalJsRoutesObject = function() {
|
350
331
|
var namespace;
|
351
|
-
|
352
332
|
namespace = function(mainRoot, namespaceString) {
|
353
333
|
var current, parts;
|
354
|
-
|
355
334
|
parts = (namespaceString ? namespaceString.split(".") : []);
|
356
335
|
if (!parts.length) {
|
357
336
|
return;
|
data/lib/routes.js.coffee
CHANGED
@@ -16,7 +16,7 @@ NodeTypes = NODE_TYPES
|
|
16
16
|
Utils =
|
17
17
|
|
18
18
|
default_serializer: (object, prefix = null) ->
|
19
|
-
return ""
|
19
|
+
return "" unless object?
|
20
20
|
if !prefix and !(@get_object_type(object) is "object")
|
21
21
|
throw new Error("Url parameters should be a javascript hash")
|
22
22
|
|
@@ -30,16 +30,16 @@ Utils =
|
|
30
30
|
key = "#{prefix}[#{key}]" if prefix?
|
31
31
|
s.push @default_serializer(prop, key)
|
32
32
|
else
|
33
|
-
if object
|
33
|
+
if object?
|
34
34
|
s.push "#{encodeURIComponent(prefix.toString())}=#{encodeURIComponent(object.toString())}"
|
35
35
|
|
36
36
|
return "" unless s.length
|
37
37
|
s.join("&")
|
38
38
|
|
39
|
+
custom_serializer: SERIALIZER
|
39
40
|
serialize: (object) ->
|
40
|
-
custom_serializer
|
41
|
-
|
42
|
-
custom_serializer(object)
|
41
|
+
if @custom_serializer? and @get_object_type(@custom_serializer) is "function"
|
42
|
+
@custom_serializer(object)
|
43
43
|
else
|
44
44
|
@default_serializer(object)
|
45
45
|
|
@@ -5,8 +5,8 @@ describe JsRoutes, "#default_serializer" do
|
|
5
5
|
end
|
6
6
|
|
7
7
|
it "should provide this method" do
|
8
|
-
expect(evaljs("Routes.default_serializer({a: 1, b: [2,3], c: {d: 4, e: 5}})")).to eq(
|
9
|
-
"a=1&b%5B%5D=2&b%5B%5D=3&c%5Bd%5D=4&c%5Be%5D=5"
|
8
|
+
expect(evaljs("Routes.default_serializer({a: 1, b: [2,3], c: {d: 4, e: 5}, f: ''})")).to eq(
|
9
|
+
"a=1&b%5B%5D=2&b%5B%5D=3&c%5Bd%5D=4&c%5Be%5D=5&f="
|
10
10
|
)
|
11
11
|
end
|
12
12
|
|
@@ -11,7 +11,7 @@ describe JsRoutes do
|
|
11
11
|
subject { JsRoutes.generate }
|
12
12
|
|
13
13
|
it "should set the default serializer when none is configured" do
|
14
|
-
is_expected.to match(%r(serialize: function\(object\) {\s+
|
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
15
|
end
|
16
16
|
|
17
17
|
it "should include a comment in the header" do
|
@@ -14,20 +14,29 @@ describe JsRoutes, "options" do
|
|
14
14
|
let(:_warnings) { true }
|
15
15
|
|
16
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"; }) }
|
17
21
|
let(:_options) { {:serializer => "myCustomSerializer"} }
|
18
22
|
|
19
23
|
it "should set configurable serializer" do
|
20
|
-
# define custom serializer
|
21
|
-
# this is a nonsense serializer, which always returns foo=bar
|
22
|
-
# for all inputs
|
23
|
-
evaljs(%q(function myCustomSerializer(object, prefix) { return "foo=bar"; }))
|
24
|
-
|
25
24
|
# expect the nonsense serializer above to have appened foo=bar
|
26
25
|
# to the end of the path
|
27
26
|
expect(evaljs(%q(Routes.inboxes_path()))).to eql("/inboxes?foo=bar")
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
30
|
+
context "when serializer is specified, but not function" do
|
31
|
+
let(:_presetup){ %q(var myCustomSerializer = 1) }
|
32
|
+
let(:_options) { {:serializer => "myCustomSerializer"} }
|
33
|
+
|
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")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
31
40
|
context "when exclude is specified" do
|
32
41
|
|
33
42
|
let(:_options) { {:exclude => /^admin_/} }
|
@@ -74,6 +74,10 @@ describe JsRoutes, "compatibility with Rails" do
|
|
74
74
|
expect(evaljs("Routes.json_only_path({format: 'json'})")).to eq(routes.json_only_path(:format => 'json'))
|
75
75
|
end
|
76
76
|
|
77
|
+
it "should serialize object with empty string value" do
|
78
|
+
expect(evaljs("Routes.inboxes_path({a: '', b: 1})")).to eq(routes.inboxes_path(:a => '', :b => 1))
|
79
|
+
end
|
80
|
+
|
77
81
|
it "should support utf-8 route" do
|
78
82
|
expect(evaljs("Routes.hello_path()")).to eq(routes.hello_path)
|
79
83
|
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.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Gusiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|