js-routes 0.9.0 → 0.9.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.
- data/Readme.md +2 -2
- data/js-routes.gemspec +3 -0
- data/lib/js_routes/version.rb +1 -1
- data/lib/js_routes.rb +0 -1
- data/lib/routes.js +39 -22
- data/lib/routes.js.coffee +29 -16
- data/spec/js_routes/rails_routes_compatibility_spec.rb +9 -0
- data/spec/spec_helper.rb +3 -1
- metadata +19 -3
data/Readme.md
CHANGED
@@ -48,11 +48,11 @@ Available options:
|
|
48
48
|
* Example: {:format => "json"}
|
49
49
|
* Default: {}
|
50
50
|
* `exclude` - Array of regexps to exclude from js routes.
|
51
|
-
* Note that regexp applied to **named route** not to *URL*
|
52
51
|
* Default: []
|
52
|
+
* The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
|
53
53
|
* `include` - Array of regexps to include in js routes.
|
54
|
-
* Note that regexp applied to **named route** not to *URL*
|
55
54
|
* Default: []
|
55
|
+
* The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
|
56
56
|
* `namespace` - global object used to access routes.
|
57
57
|
* Supports nested namespace like `MyProject.routes`
|
58
58
|
* Default: `Routes`
|
data/js-routes.gemspec
CHANGED
@@ -29,5 +29,8 @@ Gem::Specification.new do |s|
|
|
29
29
|
s.add_development_dependency(%q<guard>, [">= 0"])
|
30
30
|
s.add_development_dependency(%q<rb-fsevent>, [">= 0"])
|
31
31
|
s.add_development_dependency(%q<guard-coffeescript>, [">= 0"])
|
32
|
+
if RUBY_VERSION >= "1.9"
|
33
|
+
s.add_development_dependency(%q<debugger>, [">= 0"])
|
34
|
+
end
|
32
35
|
end
|
33
36
|
|
data/lib/js_routes/version.rb
CHANGED
data/lib/js_routes.rb
CHANGED
@@ -160,7 +160,6 @@ class JsRoutes
|
|
160
160
|
_ = <<-JS.strip!
|
161
161
|
// #{name.join('.')} => #{parent_spec}#{route.path.spec}
|
162
162
|
#{route_name}: function(#{build_params(required_parts)}) {
|
163
|
-
if (!#{LAST_OPTIONS_KEY}){ #{LAST_OPTIONS_KEY} = {}; }
|
164
163
|
return Utils.build_path(#{json(required_parts)}, #{json(optional_parts)}, #{json(serialize(route.path.spec, parent_spec))}, arguments);
|
165
164
|
}#{",\n" + url_link if url_link.length > 0}
|
166
165
|
JS
|
data/lib/routes.js
CHANGED
@@ -16,30 +16,47 @@
|
|
16
16
|
NodeTypes = NODE_TYPES;
|
17
17
|
|
18
18
|
Utils = {
|
19
|
-
serialize: function(
|
20
|
-
var i, key, prop, result, s,
|
19
|
+
serialize: function(object, prefix) {
|
20
|
+
var element, i, key, prop, result, s, _i, _len;
|
21
21
|
|
22
|
-
if (
|
22
|
+
if (prefix == null) {
|
23
|
+
prefix = null;
|
24
|
+
}
|
25
|
+
if (!object) {
|
23
26
|
return "";
|
24
27
|
}
|
28
|
+
if (!prefix && !(this.get_object_type(object) === "object")) {
|
29
|
+
throw new Error("Url parameters should be a javascript hash");
|
30
|
+
}
|
25
31
|
if (window.jQuery) {
|
26
|
-
result = window.jQuery.param(
|
32
|
+
result = window.jQuery.param(object);
|
27
33
|
return (!result ? "" : result);
|
28
34
|
}
|
29
35
|
s = [];
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
36
|
+
switch (this.get_object_type(object)) {
|
37
|
+
case "array":
|
38
|
+
for (i = _i = 0, _len = object.length; _i < _len; i = ++_i) {
|
39
|
+
element = object[i];
|
40
|
+
s.push(this.serialize(element, prefix + "[]"));
|
41
|
+
}
|
42
|
+
break;
|
43
|
+
case "object":
|
44
|
+
for (key in object) {
|
45
|
+
if (!__hasProp.call(object, key)) continue;
|
46
|
+
prop = object[key];
|
47
|
+
if (!(prop != null)) {
|
48
|
+
continue;
|
38
49
|
}
|
39
|
-
|
40
|
-
|
50
|
+
if (prefix != null) {
|
51
|
+
key = "" + prefix + "[" + key + "]";
|
52
|
+
}
|
53
|
+
s.push(this.serialize(prop, key));
|
54
|
+
}
|
55
|
+
break;
|
56
|
+
default:
|
57
|
+
if (object) {
|
58
|
+
s.push("" + (encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
|
41
59
|
}
|
42
|
-
}
|
43
60
|
}
|
44
61
|
if (!s.length) {
|
45
62
|
return "";
|
@@ -51,7 +68,7 @@
|
|
51
68
|
|
52
69
|
path = path.split("://");
|
53
70
|
last_index = path.length - 1;
|
54
|
-
path[last_index] = path[last_index].replace(/\/+/g, "/").replace(
|
71
|
+
path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/.\/$/m, "");
|
55
72
|
return path.join("://");
|
56
73
|
},
|
57
74
|
set_default_url_options: function(optional_parts, options) {
|
@@ -82,7 +99,7 @@
|
|
82
99
|
var ret_value;
|
83
100
|
|
84
101
|
ret_value = {};
|
85
|
-
if (args.length > number_of_params
|
102
|
+
if (args.length > number_of_params) {
|
86
103
|
ret_value = args.pop();
|
87
104
|
}
|
88
105
|
return ret_value;
|
@@ -97,9 +114,9 @@
|
|
97
114
|
return "";
|
98
115
|
}
|
99
116
|
property = object;
|
100
|
-
if (this.
|
117
|
+
if (this.get_object_type(object) === "object") {
|
101
118
|
property = object.to_param || object.id || object;
|
102
|
-
if (this.
|
119
|
+
if (this.get_object_type(property) === "function") {
|
103
120
|
property = property.call(object);
|
104
121
|
}
|
105
122
|
}
|
@@ -108,7 +125,7 @@
|
|
108
125
|
clone: function(obj) {
|
109
126
|
var attr, copy, key;
|
110
127
|
|
111
|
-
if ((obj == null) || "object" !== this.
|
128
|
+
if ((obj == null) || "object" !== this.get_object_type(obj)) {
|
112
129
|
return obj;
|
113
130
|
}
|
114
131
|
copy = obj.constructor();
|
@@ -194,7 +211,7 @@
|
|
194
211
|
return this.visit(route, parameters, optional);
|
195
212
|
}
|
196
213
|
parameters[left] = (function() {
|
197
|
-
switch (this.
|
214
|
+
switch (this.get_object_type(value)) {
|
198
215
|
case "array":
|
199
216
|
return value.join("/");
|
200
217
|
default:
|
@@ -227,7 +244,7 @@
|
|
227
244
|
}
|
228
245
|
return this._classToTypeCache;
|
229
246
|
},
|
230
|
-
|
247
|
+
get_object_type: function(obj) {
|
231
248
|
var strType;
|
232
249
|
|
233
250
|
if (window.jQuery && (window.jQuery.type != null)) {
|
data/lib/routes.js.coffee
CHANGED
@@ -5,27 +5,38 @@ defaults =
|
|
5
5
|
default_url_options: DEFAULT_URL_OPTIONS
|
6
6
|
|
7
7
|
NodeTypes = NODE_TYPES
|
8
|
+
|
8
9
|
Utils =
|
9
10
|
|
10
|
-
serialize: (
|
11
|
-
return "" unless
|
11
|
+
serialize: (object, prefix = null) ->
|
12
|
+
return "" unless object
|
13
|
+
if !prefix and !(@get_object_type(object) is "object")
|
14
|
+
throw new Error("Url parameters should be a javascript hash")
|
15
|
+
|
12
16
|
if window.jQuery
|
13
|
-
result = window.jQuery.param(
|
17
|
+
result = window.jQuery.param(object)
|
14
18
|
return (if not result then "" else result)
|
19
|
+
|
15
20
|
s = []
|
16
|
-
|
17
|
-
|
18
|
-
for
|
19
|
-
s.push
|
21
|
+
switch @get_object_type(object)
|
22
|
+
when "array"
|
23
|
+
for element, i in object
|
24
|
+
s.push @serialize(element, prefix + "[]")
|
25
|
+
when "object"
|
26
|
+
for own key, prop of object when prop?
|
27
|
+
key = "#{prefix}[#{key}]" if prefix?
|
28
|
+
s.push @serialize(prop, key)
|
20
29
|
else
|
21
|
-
|
30
|
+
if object
|
31
|
+
s.push "#{encodeURIComponent(prefix.toString())}=#{encodeURIComponent(object.toString())}"
|
32
|
+
|
22
33
|
return "" unless s.length
|
23
34
|
s.join("&")
|
24
35
|
|
25
36
|
clean_path: (path) ->
|
26
37
|
path = path.split("://")
|
27
38
|
last_index = path.length - 1
|
28
|
-
path[last_index] = path[last_index].replace(/\/+/g, "/").replace(
|
39
|
+
path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/.\/$/m, "")
|
29
40
|
path.join "://"
|
30
41
|
|
31
42
|
set_default_url_options: (optional_parts, options) ->
|
@@ -42,7 +53,7 @@ Utils =
|
|
42
53
|
|
43
54
|
extract_options: (number_of_params, args) ->
|
44
55
|
ret_value = {}
|
45
|
-
if args.length > number_of_params
|
56
|
+
if args.length > number_of_params
|
46
57
|
ret_value = args.pop()
|
47
58
|
ret_value
|
48
59
|
|
@@ -51,13 +62,13 @@ Utils =
|
|
51
62
|
# null, undefined, false or ''
|
52
63
|
return "" unless object
|
53
64
|
property = object
|
54
|
-
if @
|
65
|
+
if @get_object_type(object) is "object"
|
55
66
|
property = object.to_param or object.id or object
|
56
|
-
property = property.call(object) if @
|
67
|
+
property = property.call(object) if @get_object_type(property) is "function"
|
57
68
|
property.toString()
|
58
69
|
|
59
70
|
clone: (obj) ->
|
60
|
-
return obj if !obj? or "object" isnt @
|
71
|
+
return obj if !obj? or "object" isnt @get_object_type(obj)
|
61
72
|
copy = obj.constructor()
|
62
73
|
copy[key] = attr for own key, attr of obj
|
63
74
|
copy
|
@@ -70,7 +81,9 @@ Utils =
|
|
70
81
|
build_path: (required_parameters, optional_parts, route, args) ->
|
71
82
|
args = Array::slice.call(args)
|
72
83
|
opts = @extract_options(required_parameters.length, args)
|
73
|
-
|
84
|
+
|
85
|
+
if args.length > required_parameters.length
|
86
|
+
throw new Error("Too many parameters provided for path")
|
74
87
|
parameters = @prepare_parameters(required_parameters, args, opts)
|
75
88
|
@set_default_url_options optional_parts, parameters
|
76
89
|
result = "#{@get_prefix()}#{@visit(route, parameters)}"
|
@@ -126,7 +139,7 @@ Utils =
|
|
126
139
|
[type, left, right] = route
|
127
140
|
value = parameters[left]
|
128
141
|
return @visit(route, parameters, optional) unless value?
|
129
|
-
parameters[left] = switch @
|
142
|
+
parameters[left] = switch @get_object_type(value)
|
130
143
|
when "array"
|
131
144
|
value.join("/")
|
132
145
|
else
|
@@ -173,7 +186,7 @@ Utils =
|
|
173
186
|
for name in "Boolean Number String Function Array Date RegExp Undefined Null".split(" ")
|
174
187
|
@_classToTypeCache["[object " + name + "]"] = name.toLowerCase()
|
175
188
|
@_classToTypeCache
|
176
|
-
|
189
|
+
get_object_type: (obj) ->
|
177
190
|
return window.jQuery.type(obj) if window.jQuery and window.jQuery.type?
|
178
191
|
strType = Object::toString.call(obj)
|
179
192
|
@_classToType()[strType] or "object"
|
@@ -39,6 +39,11 @@ describe JsRoutes, "compatibility with Rails" do
|
|
39
39
|
evaljs("Routes.inbox_path(1, {hello: ['world', 'mars']})").should == routes.inbox_path(1, :hello => [:world, :mars])
|
40
40
|
end
|
41
41
|
|
42
|
+
it "should support nested get parameters" do
|
43
|
+
evaljs("Routes.inbox_path(1, {format: 'json', env: 'test', search: { category_ids: [2,5], q: 'hello'}})").should ==
|
44
|
+
routes.inbox_path(1, :env => 'test', :search => {:category_ids => [2,5], :q => "hello"}, :format => "json")
|
45
|
+
end
|
46
|
+
|
42
47
|
it "should support null and undefined parameters" do
|
43
48
|
evaljs("Routes.inboxes_path({uri: null, key: undefined})").should == routes.inboxes_path(:uri => nil, :key => nil)
|
44
49
|
end
|
@@ -71,6 +76,10 @@ describe JsRoutes, "compatibility with Rails" do
|
|
71
76
|
evaljs("Routes.hello_path()").should == routes.hello_path
|
72
77
|
end
|
73
78
|
|
79
|
+
it "should support root_path" do
|
80
|
+
evaljs("Routes.root_path()").should == routes.root_path
|
81
|
+
end
|
82
|
+
|
74
83
|
context "routes globbing" do
|
75
84
|
it "should be supported as parameters" do
|
76
85
|
evaljs("Routes.book_path('thrillers', 1)").should == routes.book_path('thrillers', 1)
|
data/spec/spec_helper.rb
CHANGED
@@ -52,6 +52,8 @@ def draw_routes
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
root :to => "inboxes#index"
|
56
|
+
|
55
57
|
namespace :admin do
|
56
58
|
resources :users
|
57
59
|
end
|
@@ -95,7 +97,7 @@ RSpec.configure do |config|
|
|
95
97
|
# No need to replace native V8 functions for now
|
96
98
|
#jscontext[:cgi] = CGI
|
97
99
|
#evaljs("function encodeURIComponent(string) {return cgi.escape(string);}")
|
98
|
-
jscontext[:log] = lambda {|
|
100
|
+
jscontext[:log] = lambda {|context, value| puts value.inspect}
|
99
101
|
end
|
100
102
|
config.before(:all) do
|
101
103
|
# compile all js files begin
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js-routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -123,6 +123,22 @@ dependencies:
|
|
123
123
|
- - ! '>='
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: debugger
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
126
142
|
description: Generates javascript file that defines all Rails named routes as javascript
|
127
143
|
helpers
|
128
144
|
email: agresso@gmail.com
|
@@ -169,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
169
185
|
version: '0'
|
170
186
|
segments:
|
171
187
|
- 0
|
172
|
-
hash:
|
188
|
+
hash: 3670636352927995574
|
173
189
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
190
|
none: false
|
175
191
|
requirements:
|
@@ -178,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
178
194
|
version: '0'
|
179
195
|
requirements: []
|
180
196
|
rubyforge_project:
|
181
|
-
rubygems_version: 1.8.
|
197
|
+
rubygems_version: 1.8.25
|
182
198
|
signing_key:
|
183
199
|
specification_version: 3
|
184
200
|
summary: Brings Rails named routes to javascript
|