js-routes 1.2.1 → 1.2.2
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/Readme.md +1 -1
- data/app/assets/javascripts/js-routes.js.erb +1 -0
- data/js-routes.gemspec +1 -1
- data/lib/js_routes/engine.rb +6 -9
- data/lib/js_routes/version.rb +1 -1
- data/lib/js_routes.rb +29 -30
- data/lib/routes.js +40 -56
- data/lib/routes.js.coffee +10 -9
- data/spec/js_routes/generated_javascript_spec.rb +4 -0
- data/spec/js_routes/options_spec.rb +1 -0
- data/spec/js_routes/rails_routes_compatibility_spec.rb +10 -0
- data/spec/js_routes/zzz_last_post_rails_init_spec.rb +4 -9
- data/spec/spec_helper.rb +5 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dabca1d4d4731f75724629d65370356f60a85ff2
|
4
|
+
data.tar.gz: c70302650e0525bfc7d26b59c15dd524afd11fdc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5106846103e15a73320b239924f492d02be47e5ff10d12d0de386829ae7ef22b3c6367c94d7fe1e1cdd7dcbff364e35f77c9d8b09f3cd69236b2f383f33301fd
|
7
|
+
data.tar.gz: 46533e90e0ec292657c2db70323b2643b681800942bd89e0735d880a7a99bb2fd4cfa9a0e72411b00ed84d885a9dc04abd795d2a03cc94666e16e99bc098d402
|
data/Readme.md
CHANGED
@@ -182,7 +182,7 @@ Heroku environment has a specific problems with setup. It is impossible to use a
|
|
182
182
|
For example create routes.js.erb in assets folder with needed content:
|
183
183
|
|
184
184
|
``` erb
|
185
|
-
<%= JsRoutes.generate(
|
185
|
+
<%= JsRoutes.generate(options) %>
|
186
186
|
```
|
187
187
|
|
188
188
|
This should just work.
|
data/js-routes.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.summary = %q{Brings Rails named routes to javascript}
|
22
22
|
|
23
23
|
s.add_runtime_dependency(%q<railties>, [">= 3.2"])
|
24
|
-
s.add_runtime_dependency(%q<sprockets-rails
|
24
|
+
s.add_runtime_dependency(%q<sprockets-rails>)
|
25
25
|
s.add_development_dependency(%q<rspec>, [">= 3.0.0"])
|
26
26
|
s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
|
27
27
|
s.add_development_dependency(%q<guard>, [">= 0"])
|
data/lib/js_routes/engine.rb
CHANGED
@@ -1,21 +1,18 @@
|
|
1
|
+
require 'sprockets/version'
|
2
|
+
|
1
3
|
class JsRoutes
|
4
|
+
SPROCKETS3 = Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('3.0.0')
|
2
5
|
class Engine < ::Rails::Engine
|
3
|
-
JS_ROUTES_ASSET = 'js-routes'
|
4
6
|
|
5
|
-
|
7
|
+
config.after_initialize do
|
6
8
|
routes = Rails.root.join('config', 'routes.rb').to_s
|
7
9
|
|
8
10
|
if Rails.application.assets.respond_to?(:register_preprocessor)
|
9
11
|
Rails.application.assets.register_preprocessor 'application/javascript', :'js-routes_dependent_on_routes' do |ctx,data|
|
10
|
-
ctx.depend_on(routes) if ctx.logical_path ==
|
12
|
+
ctx.depend_on(routes) if ctx.logical_path == 'js-routes'
|
11
13
|
data
|
12
14
|
end
|
13
15
|
end
|
14
|
-
|
15
|
-
# only sprockets >= 3.0
|
16
|
-
if Rails.application.assets.respond_to?(:depend_on)
|
17
|
-
Rails.application.assets.depend_on Rack::Utils.escape("file-digest://#{routes}")
|
18
|
-
end
|
19
|
-
end
|
16
|
+
end unless SPROCKETS3
|
20
17
|
end
|
21
18
|
end
|
data/lib/js_routes/version.rb
CHANGED
data/lib/js_routes.rb
CHANGED
@@ -35,6 +35,7 @@ class JsRoutes
|
|
35
35
|
}
|
36
36
|
|
37
37
|
LAST_OPTIONS_KEY = "options".freeze
|
38
|
+
FILTERED_DEFAULT_PARTS = [:controller, :action]
|
38
39
|
|
39
40
|
class Options < Struct.new(*DEFAULTS.keys)
|
40
41
|
def to_hash
|
@@ -102,7 +103,7 @@ class JsRoutes
|
|
102
103
|
"GEM_VERSION" => JsRoutes::VERSION,
|
103
104
|
"APP_CLASS" => Rails.application.class.to_s,
|
104
105
|
"NAMESPACE" => @options[:namespace],
|
105
|
-
"DEFAULT_URL_OPTIONS" => json(@options[:default_url_options].merge(
|
106
|
+
"DEFAULT_URL_OPTIONS" => json(@options[:default_url_options].merge(deprecate_url_options)),
|
106
107
|
"PREFIX" => @options[:prefix] || "",
|
107
108
|
"NODE_TYPES" => json(NODE_TYPES),
|
108
109
|
"SERIALIZER" => @options[:serializer] || "null",
|
@@ -112,13 +113,26 @@ class JsRoutes
|
|
112
113
|
end
|
113
114
|
end
|
114
115
|
|
115
|
-
def
|
116
|
+
def deprecate_url_options
|
117
|
+
result = {}
|
116
118
|
if @options.key?(:default_format)
|
117
119
|
warn("default_format option is deprecated. Use default_url_options = { format: <format> } instead")
|
118
|
-
|
119
|
-
|
120
|
-
|
120
|
+
result.merge!( format: @options[:default_format] )
|
121
|
+
end
|
122
|
+
if @options[:url_links].is_a?(String)
|
123
|
+
ActiveSupport::Deprecation.warn('js-routes url_links config value must be a boolean. Use default_url_options for specifying a default host.')
|
124
|
+
|
125
|
+
raise "invalid URL format in url_links (ex: http[s]://example.com)" if @options[:url_links].match(URI::Parser.new.make_regexp(%w(http https))).nil?
|
126
|
+
uri = URI.parse(@options[:url_links])
|
127
|
+
default_port = uri.scheme == "https" ? 443 : 80
|
128
|
+
port = uri.port == default_port ? nil : uri.port
|
129
|
+
result.merge!(
|
130
|
+
host: uri.host,
|
131
|
+
port: port,
|
132
|
+
protocol: uri.scheme,
|
133
|
+
)
|
121
134
|
end
|
135
|
+
result
|
122
136
|
end
|
123
137
|
|
124
138
|
def generate!(file_name = nil)
|
@@ -182,7 +196,7 @@ class JsRoutes
|
|
182
196
|
url_link = generate_url_link(name, route_name, route_arguments, route)
|
183
197
|
_ = <<-JS.strip!
|
184
198
|
// #{name.join('.')} => #{parent_spec}#{route.path.spec}
|
185
|
-
// function(#{build_params(route.required_parts
|
199
|
+
// function(#{build_params(route.required_parts)})
|
186
200
|
#{route_name}: Utils.route(#{route_arguments})#{",\n" + url_link if url_link.length > 0}
|
187
201
|
JS
|
188
202
|
end
|
@@ -190,34 +204,23 @@ class JsRoutes
|
|
190
204
|
def route_js_arguments(route, parent_spec)
|
191
205
|
required_parts = route.required_parts.clone
|
192
206
|
optional_parts = route.parts - required_parts
|
193
|
-
|
207
|
+
default_parts = route.defaults.reject do |part, _|
|
208
|
+
FILTERED_DEFAULT_PARTS.include?(part)
|
209
|
+
end
|
210
|
+
[
|
211
|
+
required_parts, optional_parts, serialize(route.path.spec, parent_spec), default_parts
|
212
|
+
].map do |argument|
|
194
213
|
json(argument)
|
195
214
|
end.join(", ")
|
196
215
|
end
|
197
216
|
|
198
217
|
def generate_url_link(name, route_name, route_arguments, route)
|
199
218
|
return "" unless @options[:url_links]
|
200
|
-
defaults = @options[:url_links] == true ? route.defaults.slice(:host, :port, :protocol) : deprecated_base_url
|
201
219
|
<<-JS.strip!
|
202
|
-
#{generate_route_name(name, :url)}: Utils.route(#{route_arguments},
|
220
|
+
#{generate_route_name(name, :url)}: Utils.route(#{route_arguments}, true)
|
203
221
|
JS
|
204
222
|
end
|
205
223
|
|
206
|
-
def deprecated_base_url
|
207
|
-
ActiveSupport::Deprecation.warn('js-routes url_links config value must be a boolean. Use default_url_options for specifying a default host.')
|
208
|
-
|
209
|
-
|
210
|
-
raise "invalid URL format in url_links (ex: http[s]://example.com)" if @options[:url_links].match(URI::Parser.new.make_regexp(%w(http https))).nil?
|
211
|
-
uri = URI.parse(@options[:url_links])
|
212
|
-
default_port = uri.scheme == "https" ? 443 : 80
|
213
|
-
port = uri.port == default_port ? nil : uri.port
|
214
|
-
{
|
215
|
-
host: uri.host,
|
216
|
-
port: port,
|
217
|
-
protocol: uri.scheme,
|
218
|
-
}
|
219
|
-
end
|
220
|
-
|
221
224
|
def generate_route_name(name, suffix)
|
222
225
|
route_name = name.join('_')
|
223
226
|
route_name << "_#{ suffix }" if suffix
|
@@ -228,12 +231,8 @@ class JsRoutes
|
|
228
231
|
self.class.json(string)
|
229
232
|
end
|
230
233
|
|
231
|
-
def build_params(required_parts
|
232
|
-
params = required_parts
|
233
|
-
# prepending each parameter name with underscore
|
234
|
-
# to prevent conflict with JS reserved words
|
235
|
-
unprotected ? name : "_#{name}"
|
236
|
-
end << LAST_OPTIONS_KEY
|
234
|
+
def build_params(required_parts)
|
235
|
+
params = required_parts + [LAST_OPTIONS_KEY]
|
237
236
|
params.join(", ")
|
238
237
|
end
|
239
238
|
|
data/lib/routes.js
CHANGED
@@ -1,13 +1,12 @@
|
|
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, ReservedOptions, Utils, createGlobalJsRoutesObject, defaults, root,
|
9
|
-
|
10
|
-
|
8
|
+
hasProp = {}.hasOwnProperty,
|
9
|
+
slice = [].slice;
|
11
10
|
|
12
11
|
root = typeof exports !== "undefined" && exports !== null ? exports : this;
|
13
12
|
|
@@ -28,8 +27,7 @@ Based on Rails routes of APP_CLASS
|
|
28
27
|
|
29
28
|
Utils = {
|
30
29
|
default_serializer: function(object, prefix) {
|
31
|
-
var element, i,
|
32
|
-
|
30
|
+
var element, i, j, key, len, prop, s;
|
33
31
|
if (prefix == null) {
|
34
32
|
prefix = null;
|
35
33
|
}
|
@@ -42,21 +40,21 @@ Based on Rails routes of APP_CLASS
|
|
42
40
|
s = [];
|
43
41
|
switch (this.get_object_type(object)) {
|
44
42
|
case "array":
|
45
|
-
for (i =
|
43
|
+
for (i = j = 0, len = object.length; j < len; i = ++j) {
|
46
44
|
element = object[i];
|
47
45
|
s.push(this.default_serializer(element, prefix + "[]"));
|
48
46
|
}
|
49
47
|
break;
|
50
48
|
case "object":
|
51
49
|
for (key in object) {
|
52
|
-
if (!
|
50
|
+
if (!hasProp.call(object, key)) continue;
|
53
51
|
prop = object[key];
|
54
52
|
if ((prop == null) && (prefix != null)) {
|
55
53
|
prop = "";
|
56
54
|
}
|
57
55
|
if (prop != null) {
|
58
56
|
if (prefix != null) {
|
59
|
-
key =
|
57
|
+
key = prefix + "[" + key + "]";
|
60
58
|
}
|
61
59
|
s.push(this.default_serializer(prop, key));
|
62
60
|
}
|
@@ -64,7 +62,7 @@ Based on Rails routes of APP_CLASS
|
|
64
62
|
break;
|
65
63
|
default:
|
66
64
|
if (object != null) {
|
67
|
-
s.push(
|
65
|
+
s.push((encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
|
68
66
|
}
|
69
67
|
}
|
70
68
|
if (!s.length) {
|
@@ -82,7 +80,6 @@ Based on Rails routes of APP_CLASS
|
|
82
80
|
},
|
83
81
|
clean_path: function(path) {
|
84
82
|
var last_index;
|
85
|
-
|
86
83
|
path = path.split("://");
|
87
84
|
last_index = path.length - 1;
|
88
85
|
path[last_index] = path[last_index].replace(/\/+/g, "/");
|
@@ -90,7 +87,6 @@ Based on Rails routes of APP_CLASS
|
|
90
87
|
},
|
91
88
|
extract_options: function(number_of_params, args) {
|
92
89
|
var last_el;
|
93
|
-
|
94
90
|
last_el = args[args.length - 1];
|
95
91
|
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
92
|
return args.pop() || {};
|
@@ -103,7 +99,6 @@ Based on Rails routes of APP_CLASS
|
|
103
99
|
},
|
104
100
|
path_identifier: function(object) {
|
105
101
|
var property;
|
106
|
-
|
107
102
|
if (object === 0) {
|
108
103
|
return "0";
|
109
104
|
}
|
@@ -127,13 +122,12 @@ Based on Rails routes of APP_CLASS
|
|
127
122
|
},
|
128
123
|
clone: function(obj) {
|
129
124
|
var attr, copy, key;
|
130
|
-
|
131
125
|
if ((obj == null) || "object" !== this.get_object_type(obj)) {
|
132
126
|
return obj;
|
133
127
|
}
|
134
128
|
copy = obj.constructor();
|
135
129
|
for (key in obj) {
|
136
|
-
if (!
|
130
|
+
if (!hasProp.call(obj, key)) continue;
|
137
131
|
attr = obj[key];
|
138
132
|
copy[key] = attr;
|
139
133
|
}
|
@@ -141,47 +135,43 @@ Based on Rails routes of APP_CLASS
|
|
141
135
|
},
|
142
136
|
merge: function() {
|
143
137
|
var tap, xs;
|
144
|
-
|
145
|
-
xs = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
138
|
+
xs = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
146
139
|
tap = function(o, fn) {
|
147
140
|
fn(o);
|
148
141
|
return o;
|
149
142
|
};
|
150
143
|
if ((xs != null ? xs.length : void 0) > 0) {
|
151
144
|
return tap({}, function(m) {
|
152
|
-
var
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
_results1 = [];
|
145
|
+
var j, k, len, results, v, x;
|
146
|
+
results = [];
|
147
|
+
for (j = 0, len = xs.length; j < len; j++) {
|
148
|
+
x = xs[j];
|
149
|
+
results.push((function() {
|
150
|
+
var results1;
|
151
|
+
results1 = [];
|
161
152
|
for (k in x) {
|
162
153
|
v = x[k];
|
163
|
-
|
154
|
+
results1.push(m[k] = v);
|
164
155
|
}
|
165
|
-
return
|
156
|
+
return results1;
|
166
157
|
})());
|
167
158
|
}
|
168
|
-
return
|
159
|
+
return results;
|
169
160
|
});
|
170
161
|
}
|
171
162
|
},
|
172
|
-
normalize_options: function(
|
173
|
-
var i, key, options, result, url_parameters, value
|
174
|
-
|
163
|
+
normalize_options: function(default_parts, required_parameters, optional_parts, actual_parameters) {
|
164
|
+
var i, j, key, len, options, result, url_parameters, value;
|
175
165
|
options = this.extract_options(required_parameters.length, actual_parameters);
|
176
166
|
if (actual_parameters.length > required_parameters.length) {
|
177
167
|
throw new Error("Too many parameters provided for path");
|
178
168
|
}
|
179
|
-
options = this.merge(defaults.default_url_options,
|
169
|
+
options = this.merge(defaults.default_url_options, default_parts, options);
|
180
170
|
result = {};
|
181
171
|
url_parameters = {};
|
182
172
|
result['url_parameters'] = url_parameters;
|
183
173
|
for (key in options) {
|
184
|
-
if (!
|
174
|
+
if (!hasProp.call(options, key)) continue;
|
185
175
|
value = options[key];
|
186
176
|
if (ReservedOptions.indexOf(key) >= 0) {
|
187
177
|
result[key] = value;
|
@@ -189,7 +179,7 @@ Based on Rails routes of APP_CLASS
|
|
189
179
|
url_parameters[key] = value;
|
190
180
|
}
|
191
181
|
}
|
192
|
-
for (i =
|
182
|
+
for (i = j = 0, len = required_parameters.length; j < len; i = ++j) {
|
193
183
|
value = required_parameters[i];
|
194
184
|
if (i < actual_parameters.length) {
|
195
185
|
url_parameters[value] = actual_parameters[i];
|
@@ -197,11 +187,10 @@ Based on Rails routes of APP_CLASS
|
|
197
187
|
}
|
198
188
|
return result;
|
199
189
|
},
|
200
|
-
build_route: function(required_parameters, optional_parts, route,
|
190
|
+
build_route: function(required_parameters, optional_parts, route, default_parts, full_url, args) {
|
201
191
|
var options, parameters, result, url, url_params;
|
202
|
-
|
203
192
|
args = Array.prototype.slice.call(args);
|
204
|
-
options = this.normalize_options(
|
193
|
+
options = this.normalize_options(default_parts, required_parameters, optional_parts, args);
|
205
194
|
parameters = options['url_parameters'];
|
206
195
|
result = "" + (this.get_prefix()) + (this.visit(route, parameters));
|
207
196
|
url = Utils.clean_path("" + result);
|
@@ -212,14 +201,13 @@ Based on Rails routes of APP_CLASS
|
|
212
201
|
url += "?" + url_params;
|
213
202
|
}
|
214
203
|
url += options.anchor ? "#" + options.anchor : "";
|
215
|
-
if (
|
204
|
+
if (full_url) {
|
216
205
|
url = this.route_url(options) + url;
|
217
206
|
}
|
218
207
|
return url;
|
219
208
|
},
|
220
209
|
visit: function(route, parameters, optional) {
|
221
210
|
var left, left_part, right, right_part, type, value;
|
222
|
-
|
223
211
|
if (optional == null) {
|
224
212
|
optional = false;
|
225
213
|
}
|
@@ -258,7 +246,6 @@ Based on Rails routes of APP_CLASS
|
|
258
246
|
},
|
259
247
|
build_path_spec: function(route, wildcard) {
|
260
248
|
var left, right, type;
|
261
|
-
|
262
249
|
if (wildcard == null) {
|
263
250
|
wildcard = false;
|
264
251
|
}
|
@@ -287,7 +274,6 @@ Based on Rails routes of APP_CLASS
|
|
287
274
|
},
|
288
275
|
visit_globbing: function(route, parameters, optional) {
|
289
276
|
var left, right, type, value;
|
290
|
-
|
291
277
|
type = route[0], left = route[1], right = route[2];
|
292
278
|
if (left.replace(/^\*/i, "") !== left) {
|
293
279
|
route[1] = left = left.replace(/^\*/i, "");
|
@@ -308,18 +294,16 @@ Based on Rails routes of APP_CLASS
|
|
308
294
|
},
|
309
295
|
get_prefix: function() {
|
310
296
|
var prefix;
|
311
|
-
|
312
297
|
prefix = defaults.prefix;
|
313
298
|
if (prefix !== "") {
|
314
|
-
prefix = (prefix.match("/$") ? prefix :
|
299
|
+
prefix = (prefix.match("/$") ? prefix : prefix + "/");
|
315
300
|
}
|
316
301
|
return prefix;
|
317
302
|
},
|
318
|
-
route: function(required_parts, optional_parts, route_spec,
|
303
|
+
route: function(required_parts, optional_parts, route_spec, default_parts, full_url) {
|
319
304
|
var path_fn;
|
320
|
-
|
321
305
|
path_fn = function() {
|
322
|
-
return Utils.build_route(required_parts, optional_parts, route_spec,
|
306
|
+
return Utils.build_route(required_parts, optional_parts, route_spec, default_parts, full_url, arguments);
|
323
307
|
};
|
324
308
|
path_fn.required_params = required_parts;
|
325
309
|
path_fn.toString = function() {
|
@@ -329,7 +313,6 @@ Based on Rails routes of APP_CLASS
|
|
329
313
|
},
|
330
314
|
route_url: function(route_defaults) {
|
331
315
|
var hostname, port, protocol;
|
332
|
-
|
333
316
|
if (typeof route_defaults === 'string') {
|
334
317
|
return route_defaults;
|
335
318
|
}
|
@@ -343,7 +326,11 @@ Based on Rails routes of APP_CLASS
|
|
343
326
|
return typeof window !== 'undefined' && typeof window.location !== 'undefined';
|
344
327
|
},
|
345
328
|
current_host: function() {
|
346
|
-
|
329
|
+
if (this.has_location()) {
|
330
|
+
return window.location.hostname;
|
331
|
+
} else {
|
332
|
+
return null;
|
333
|
+
}
|
347
334
|
},
|
348
335
|
current_protocol: function() {
|
349
336
|
if (this.has_location() && window.location.protocol !== '') {
|
@@ -361,15 +348,14 @@ Based on Rails routes of APP_CLASS
|
|
361
348
|
},
|
362
349
|
_classToTypeCache: null,
|
363
350
|
_classToType: function() {
|
364
|
-
var
|
365
|
-
|
351
|
+
var j, len, name, ref;
|
366
352
|
if (this._classToTypeCache != null) {
|
367
353
|
return this._classToTypeCache;
|
368
354
|
}
|
369
355
|
this._classToTypeCache = {};
|
370
|
-
|
371
|
-
for (
|
372
|
-
name =
|
356
|
+
ref = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
|
357
|
+
for (j = 0, len = ref.length; j < len; j++) {
|
358
|
+
name = ref[j];
|
373
359
|
this._classToTypeCache["[object " + name + "]"] = name.toLowerCase();
|
374
360
|
}
|
375
361
|
return this._classToTypeCache;
|
@@ -391,10 +377,8 @@ Based on Rails routes of APP_CLASS
|
|
391
377
|
|
392
378
|
createGlobalJsRoutesObject = function() {
|
393
379
|
var namespace;
|
394
|
-
|
395
380
|
namespace = function(mainRoot, namespaceString) {
|
396
381
|
var current, parts;
|
397
|
-
|
398
382
|
parts = (namespaceString ? namespaceString.split(".") : []);
|
399
383
|
if (!parts.length) {
|
400
384
|
return;
|
data/lib/routes.js.coffee
CHANGED
@@ -100,11 +100,11 @@ Utils =
|
|
100
100
|
if xs?.length > 0
|
101
101
|
tap {}, (m) -> m[k] = v for k, v of x for x in xs
|
102
102
|
|
103
|
-
normalize_options: (
|
103
|
+
normalize_options: (default_parts, required_parameters, optional_parts, actual_parameters) ->
|
104
104
|
options = @extract_options(required_parameters.length, actual_parameters)
|
105
105
|
if actual_parameters.length > required_parameters.length
|
106
106
|
throw new Error("Too many parameters provided for path")
|
107
|
-
options = @merge(defaults.default_url_options,
|
107
|
+
options = @merge(defaults.default_url_options, default_parts, options)
|
108
108
|
result = {}
|
109
109
|
url_parameters = {}
|
110
110
|
result['url_parameters'] = url_parameters
|
@@ -118,10 +118,10 @@ Utils =
|
|
118
118
|
url_parameters[value] = actual_parameters[i]
|
119
119
|
result
|
120
120
|
|
121
|
-
build_route: (required_parameters, optional_parts, route,
|
121
|
+
build_route: (required_parameters, optional_parts, route, default_parts, full_url, args) ->
|
122
122
|
args = Array::slice.call(args)
|
123
123
|
|
124
|
-
options = @normalize_options(
|
124
|
+
options = @normalize_options(default_parts, required_parameters, optional_parts, args)
|
125
125
|
parameters = options['url_parameters']
|
126
126
|
|
127
127
|
# path
|
@@ -134,7 +134,7 @@ Utils =
|
|
134
134
|
url += "?#{url_params}"
|
135
135
|
# set anchor
|
136
136
|
url += if options.anchor then "##{options.anchor}" else ""
|
137
|
-
if
|
137
|
+
if full_url
|
138
138
|
url = @route_url(options) + url
|
139
139
|
url
|
140
140
|
|
@@ -231,9 +231,10 @@ Utils =
|
|
231
231
|
#
|
232
232
|
# route function: create route path function and add spec to it
|
233
233
|
#
|
234
|
-
route: (required_parts, optional_parts, route_spec,
|
235
|
-
path_fn = ->
|
236
|
-
|
234
|
+
route: (required_parts, optional_parts, route_spec, default_parts, full_url) ->
|
235
|
+
path_fn = -> Utils.build_route(
|
236
|
+
required_parts, optional_parts, route_spec, default_parts, full_url, arguments
|
237
|
+
)
|
237
238
|
path_fn.required_params = required_parts
|
238
239
|
path_fn.toString = -> Utils.build_path_spec(route_spec)
|
239
240
|
path_fn
|
@@ -253,7 +254,7 @@ Utils =
|
|
253
254
|
typeof window != 'undefined' && typeof window.location != 'undefined'
|
254
255
|
|
255
256
|
current_host: ->
|
256
|
-
@has_location()
|
257
|
+
if @has_location() then window.location.hostname else null
|
257
258
|
|
258
259
|
current_protocol: () ->
|
259
260
|
if @has_location() && window.location.protocol != ''
|
@@ -75,6 +75,10 @@ describe JsRoutes do
|
|
75
75
|
end
|
76
76
|
|
77
77
|
describe "compiled javascript asset" do
|
78
|
+
if JsRoutes::SPROCKETS3
|
79
|
+
let(:routes_path){ "file://#{Rails.root.join 'config', 'routes.rb'}" }
|
80
|
+
before { expect(self).to receive(:depend_on).with routes_path }
|
81
|
+
end
|
78
82
|
subject { ERB.new(File.read("app/assets/javascripts/js-routes.js.erb")).result(binding) }
|
79
83
|
it "should have js routes code" do
|
80
84
|
is_expected.to include("inbox_message_path: Utils.route([\"inbox_id\",\"id\"]")
|
@@ -61,6 +61,16 @@ describe JsRoutes, "compatibility with Rails" do
|
|
61
61
|
expect(evaljs("Routes.budgie_descendents_path(1)")).to eq(routes.budgie_descendents_path(1))
|
62
62
|
end
|
63
63
|
|
64
|
+
describe "when route has defaults" do
|
65
|
+
it "should support route default format" do
|
66
|
+
expect(evaljs("Routes.api_purchases_path()")).to eq(routes.api_purchases_path)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should support default format override" do
|
70
|
+
expect(evaljs("Routes.api_purchases_path({format: 'xml'})")).to eq(routes.api_purchases_path(format: 'xml'))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
64
74
|
context "with rails engines" do
|
65
75
|
it "should support simple route" do
|
66
76
|
expect(evaljs("Routes.blog_app_posts_path()")).to eq(blog_routes.posts_path())
|
@@ -55,19 +55,14 @@ describe "after Rails initialization" do
|
|
55
55
|
FileUtils.rm_f(TEST_ASSET_PATH)
|
56
56
|
end
|
57
57
|
|
58
|
-
it "should have registered a preprocessor" do
|
59
|
-
pps = Rails.application.assets.preprocessors
|
60
|
-
js_pps = pps['application/javascript']
|
61
|
-
klass = sprockets_v3? ? 'LegacyProcProcessor' : 'Processor'
|
62
|
-
expect(js_pps.map(&:to_s)).to include("Sprockets::#{klass} (js-routes_dependent_on_routes)")
|
63
|
-
end
|
64
|
-
|
65
58
|
context "the preprocessor" do
|
66
59
|
before(:each) do
|
60
|
+
path = Rails.root.join('config','routes.rb').to_s
|
61
|
+
path = "file://#{path}" if JsRoutes::SPROCKETS3
|
67
62
|
if sprockets_v3?
|
68
|
-
expect_any_instance_of(Sprockets::Context).to receive(:depend_on).with(
|
63
|
+
expect_any_instance_of(Sprockets::Context).to receive(:depend_on).with(path)
|
69
64
|
else
|
70
|
-
expect(ctx).to receive(:depend_on).with(
|
65
|
+
expect(ctx).to receive(:depend_on).with(path)
|
71
66
|
end
|
72
67
|
end
|
73
68
|
let!(:ctx) do
|
data/spec/spec_helper.rb
CHANGED
@@ -116,13 +116,17 @@ def draw_routes
|
|
116
116
|
|
117
117
|
get '/привет' => "foo#foo", :as => :hello
|
118
118
|
get '(/o/:organization)/search/:q' => "foo#foo", as: :search
|
119
|
-
|
119
|
+
|
120
120
|
resources :sessions, :only => [:new, :create, :destroy], :protocol => 'https'
|
121
121
|
|
122
122
|
get '/' => 'sso#login', host: 'sso.example.com', as: :sso
|
123
123
|
|
124
124
|
resources :portals, :port => 8080
|
125
125
|
|
126
|
+
namespace :api, format: true, defaults: {format: 'json'} do
|
127
|
+
get "/purchases" => "purchases#index"
|
128
|
+
end
|
129
|
+
|
126
130
|
resources :budgies do
|
127
131
|
get "descendents"
|
128
132
|
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.2.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Gusiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: sprockets-rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|