js-routes 0.8.6 → 0.8.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +15 -7
- data/VERSION +1 -1
- data/js-routes.gemspec +8 -8
- data/lib/js_routes.rb +12 -3
- data/lib/routes.js +23 -25
- data/spec/js_routes/options_spec.rb +22 -1
- data/spec/spec_helper.rb +1 -0
- metadata +100 -88
data/Readme.md
CHANGED
@@ -42,22 +42,30 @@ end
|
|
42
42
|
|
43
43
|
Available options:
|
44
44
|
|
45
|
-
* `
|
46
|
-
*
|
45
|
+
* `default_url_options` - default parameters to be used to generate url
|
46
|
+
* Note that currently only optional parameters (like `:format`) can be defaulted.
|
47
|
+
* Example: {:format => "json"}
|
48
|
+
* Default: {}
|
49
|
+
* `exclude` - Array of regexps to exclude from js routes.
|
47
50
|
* Note that regexp applied to **named route** not to *URL*
|
48
|
-
*
|
51
|
+
* Default: []
|
52
|
+
* `include` - Array of regexps to include in js routes.
|
49
53
|
* Note that regexp applied to **named route** not to *URL*
|
50
|
-
*
|
54
|
+
* Default: []
|
55
|
+
* `namespace` - global object used to access routes.
|
51
56
|
* Supports nested namespace like `MyProject.routes`
|
52
|
-
*
|
57
|
+
* Default: `Routes`
|
58
|
+
* `prefix` - String representing a url path to prepend to all paths.
|
53
59
|
* Example: `http://yourdomain.com`. This will cause route helpers to generate full path only.
|
60
|
+
* Default: blank
|
54
61
|
|
55
62
|
|
56
63
|
You can generate routes files on the application side like this:
|
57
64
|
|
58
65
|
``` ruby
|
59
|
-
JsRoutes.generate!("#{path}/app_routes.js", :namespace => "AppRoutes", :exclude => /^admin_/,
|
60
|
-
JsRoutes.generate!("#{path}/adm_routes.js", :namespace => "AdmRoutes", :include => /^admin_
|
66
|
+
JsRoutes.generate!("#{path}/app_routes.js", :namespace => "AppRoutes", :exclude => [/^admin_/, /^api_/])
|
67
|
+
JsRoutes.generate!("#{path}/adm_routes.js", :namespace => "AdmRoutes", :include => /^admin_/)
|
68
|
+
JsRoutes.generate!("#{path}/api_routes.js", :namespace => "ApiRoutes", :include => /^api_/, :default_url_options => {:format => "json"})
|
61
69
|
```
|
62
70
|
|
63
71
|
In order to generate javascript to string and manipulate them yourself use:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.7
|
data/js-routes.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.8.
|
7
|
+
s.name = %q{js-routes}
|
8
|
+
s.version = "0.8.7"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Bogdan Gusiev"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = %q{2013-02-13}
|
13
|
+
s.description = %q{Generates javascript file that defines all Rails named routes as javascript helpers}
|
14
|
+
s.email = %q{agresso@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt"
|
17
17
|
]
|
@@ -38,11 +38,11 @@ Gem::Specification.new do |s|
|
|
38
38
|
"spec/js_routes/rails_routes_compatibility_spec.rb",
|
39
39
|
"spec/spec_helper.rb"
|
40
40
|
]
|
41
|
-
s.homepage =
|
41
|
+
s.homepage = %q{http://github.com/railsware/js-routes}
|
42
42
|
s.licenses = ["MIT"]
|
43
43
|
s.require_paths = ["lib"]
|
44
|
-
s.rubygems_version =
|
45
|
-
s.summary =
|
44
|
+
s.rubygems_version = %q{1.6.0}
|
45
|
+
s.summary = %q{Brings Rails named routes to javascript}
|
46
46
|
|
47
47
|
if s.respond_to? :specification_version then
|
48
48
|
s.specification_version = 3
|
data/lib/js_routes.rb
CHANGED
@@ -8,11 +8,11 @@ class JsRoutes
|
|
8
8
|
|
9
9
|
DEFAULTS = {
|
10
10
|
:namespace => "Routes",
|
11
|
-
:default_format => "",
|
12
11
|
:exclude => [],
|
13
12
|
:include => //,
|
14
13
|
:file => DEFAULT_PATH,
|
15
|
-
:prefix => ""
|
14
|
+
:prefix => "",
|
15
|
+
:default_url_options => {}
|
16
16
|
}
|
17
17
|
|
18
18
|
# We encode node symbols as integer to reduce the routes.js file size
|
@@ -86,12 +86,21 @@ class JsRoutes
|
|
86
86
|
def generate
|
87
87
|
js = File.read(File.dirname(__FILE__) + "/routes.js")
|
88
88
|
js.gsub!("NAMESPACE", @options[:namespace])
|
89
|
-
js.gsub!("
|
89
|
+
js.gsub!("DEFAULT_URL_OPTIONS", json(@options[:default_url_options].merge(deprecated_default_format)))
|
90
90
|
js.gsub!("PREFIX", @options[:prefix])
|
91
91
|
js.gsub!("NODE_TYPES", json(NODE_TYPES))
|
92
92
|
js.gsub!("ROUTES", js_routes)
|
93
93
|
end
|
94
94
|
|
95
|
+
def deprecated_default_format
|
96
|
+
if @options.key?(:default_format)
|
97
|
+
warn("default_format option is deprecated. Use default_url_options = {:format => <format>} instead")
|
98
|
+
{:format => @options[:default_format]}
|
99
|
+
else
|
100
|
+
{}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
95
104
|
def generate!(file_name = nil)
|
96
105
|
# Some libraries like Devise do not yet loaded their routes so we will wait
|
97
106
|
# until initialization process finish
|
data/lib/routes.js
CHANGED
@@ -7,10 +7,10 @@
|
|
7
7
|
|
8
8
|
var defaults = {
|
9
9
|
prefix: 'PREFIX',
|
10
|
-
|
10
|
+
default_url_options: DEFAULT_URL_OPTIONS
|
11
11
|
};
|
12
12
|
|
13
|
-
var NodeTypes = NODE_TYPES
|
13
|
+
var NodeTypes = NODE_TYPES;
|
14
14
|
|
15
15
|
var Utils = {
|
16
16
|
|
@@ -46,8 +46,13 @@
|
|
46
46
|
return path.join("://");
|
47
47
|
},
|
48
48
|
|
49
|
-
|
50
|
-
|
49
|
+
set_default_url_options: function(optional_parts, options) {
|
50
|
+
for (var i=0;i < optional_parts.length; i++) {
|
51
|
+
var part = optional_parts[i];
|
52
|
+
if (!options.hasOwnProperty(part) && defaults.default_url_options.hasOwnProperty(part)) {
|
53
|
+
options[part] = defaults.default_url_options[part];
|
54
|
+
}
|
55
|
+
}
|
51
56
|
},
|
52
57
|
|
53
58
|
extract_anchor: function(options) {
|
@@ -66,11 +71,11 @@
|
|
66
71
|
|
67
72
|
path_identifier: function(object) {
|
68
73
|
if (object === 0) {
|
69
|
-
|
74
|
+
return '0';
|
70
75
|
}
|
71
76
|
|
72
77
|
if (! object) { // null, undefined, false or ''
|
73
|
-
|
78
|
+
return '';
|
74
79
|
}
|
75
80
|
|
76
81
|
if (typeof(object) == "object") {
|
@@ -101,12 +106,6 @@
|
|
101
106
|
return result;
|
102
107
|
},
|
103
108
|
|
104
|
-
smartIndexOf: function(array, item){
|
105
|
-
if (Array.prototype.indexOf && array.indexOf === Array.prototype.indexOf) return array.indexOf(item);
|
106
|
-
for (var i = 0; i < array.length; i++) if (i in array && array[i] === item) return i;
|
107
|
-
return -1;
|
108
|
-
},
|
109
|
-
|
110
109
|
build_path: function(required_parameters, optional_parts, route, args) {
|
111
110
|
args = Array.prototype.slice.call(args);
|
112
111
|
var opts = this.extract_options(required_parameters.length, args);
|
@@ -115,10 +114,7 @@
|
|
115
114
|
}
|
116
115
|
|
117
116
|
var parameters = this.prepare_parameters(required_parameters, args, opts);
|
118
|
-
|
119
|
-
if (Utils.smartIndexOf(optional_parts, 'format') !== -1) {
|
120
|
-
this.set_default_format(parameters);
|
121
|
-
}
|
117
|
+
this.set_default_url_options(optional_parts, parameters);
|
122
118
|
var result = Utils.get_prefix();
|
123
119
|
var anchor = Utils.extract_anchor(parameters);
|
124
120
|
|
@@ -129,24 +125,26 @@
|
|
129
125
|
/*
|
130
126
|
* This function is JavaScript impelementation of the
|
131
127
|
* Journey::Visitors::Formatter that builds route by given parameters
|
132
|
-
*
|
128
|
+
* from route binary tree.
|
133
129
|
* Binary tree is serialized in the following way:
|
134
130
|
* [node type, left node, right node ]
|
135
131
|
*
|
136
|
-
|
132
|
+
* @param {Boolean} optional Marks the currently visited branch as optional.
|
133
|
+
* If set to `true`, this method will not throw when encountering
|
134
|
+
* a missing parameter (used in recursive calls).
|
137
135
|
*/
|
138
|
-
visit: function(route,
|
136
|
+
visit: function(route, parameters, optional) {
|
139
137
|
var type = route[0];
|
140
138
|
var left = route[1];
|
141
139
|
var right = route[2];
|
142
140
|
switch (type) {
|
143
141
|
case NodeTypes.GROUP:
|
144
|
-
return this.visit(left,
|
142
|
+
return this.visit(left, parameters, true)
|
145
143
|
case NodeTypes.STAR:
|
146
|
-
return this.visit(left,
|
144
|
+
return this.visit(left, parameters, true)
|
147
145
|
case NodeTypes.CAT:
|
148
|
-
var leftPart = this.visit(left,
|
149
|
-
rightPart = this.visit(right,
|
146
|
+
var leftPart = this.visit(left, parameters, optional),
|
147
|
+
rightPart = this.visit(right, parameters, optional);
|
150
148
|
|
151
149
|
if (optional && ! (leftPart && rightPart))
|
152
150
|
return '';
|
@@ -159,10 +157,10 @@
|
|
159
157
|
case NodeTypes.DOT:
|
160
158
|
return left;
|
161
159
|
case NodeTypes.SYMBOL:
|
162
|
-
var value =
|
160
|
+
var value = parameters[left];
|
163
161
|
|
164
162
|
if (value || value === 0) {
|
165
|
-
delete
|
163
|
+
delete parameters[left];
|
166
164
|
return this.path_identifier(value);
|
167
165
|
}
|
168
166
|
|
@@ -4,11 +4,14 @@ describe JsRoutes, "options" do
|
|
4
4
|
|
5
5
|
before(:each) do
|
6
6
|
evaljs(_presetup)
|
7
|
-
|
7
|
+
with_warnings(_warnings) do
|
8
|
+
evaljs(JsRoutes.generate(_options))
|
9
|
+
end
|
8
10
|
end
|
9
11
|
|
10
12
|
let(:_presetup) { "this;" }
|
11
13
|
let(:_options) { {} }
|
14
|
+
let(:_warnings) { true }
|
12
15
|
|
13
16
|
context "when exclude is specified" do
|
14
17
|
|
@@ -76,6 +79,7 @@ describe JsRoutes, "options" do
|
|
76
79
|
|
77
80
|
context "when default_format is specified" do
|
78
81
|
let(:_options) { {:default_format => "json"} }
|
82
|
+
let(:_warnings) { nil }
|
79
83
|
|
80
84
|
it "should render routing with default_format" do
|
81
85
|
evaljs("Routes.inbox_path(1)").should == routes.inbox_path(1, :format => "json")
|
@@ -131,4 +135,21 @@ describe JsRoutes, "options" do
|
|
131
135
|
end
|
132
136
|
end
|
133
137
|
end
|
138
|
+
|
139
|
+
describe "default_url_options" do
|
140
|
+
context "with optional route parts" do
|
141
|
+
let(:_options) { {:default_url_options => {:optional_id => "12", :format => "json"}}}
|
142
|
+
it "should use this opions to fill optional parameters" do
|
143
|
+
evaljs("Routes.things_path()").should == routes.things_path(:optional_id => 12, :format => "json")
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context "with required route parts" do
|
148
|
+
let(:_options) { {:default_url_options => {:inbox_id => "12"}} }
|
149
|
+
it "should use this opions to fill optional parameters" do
|
150
|
+
pending
|
151
|
+
evaljs("Routes.inbox_messages_path()").should == routes.inbox_messages_path(:inbox_id => 12)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
134
155
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -92,6 +92,7 @@ RSpec.configure do |config|
|
|
92
92
|
evaljs("var window = this;")
|
93
93
|
jscontext[:cgi] = CGI
|
94
94
|
evaljs("function encodeURIComponent(string) {return cgi.escape(string);}")
|
95
|
+
jscontext[:log] = lambda {|arg| puts arg.inspect}
|
95
96
|
end
|
96
97
|
config.before(:all) do
|
97
98
|
draw_routes
|
metadata
CHANGED
@@ -1,104 +1,109 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: js-routes
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 49
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 8
|
9
|
+
- 7
|
10
|
+
version: 0.8.7
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Bogdan Gusiev
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
|
18
|
+
date: 2013-02-13 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
23
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
22
|
-
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 2
|
31
|
+
version: "3.2"
|
32
|
+
version_requirements: *id001
|
23
33
|
prerelease: false
|
24
|
-
|
34
|
+
name: rails
|
35
|
+
type: :runtime
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
25
38
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
version_requirements: *id002
|
47
|
+
prerelease: false
|
31
48
|
name: therubyracer
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
38
49
|
type: :development
|
39
|
-
|
40
|
-
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
52
|
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: rspec
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
53
|
+
requirements:
|
51
54
|
- - ~>
|
52
|
-
- !ruby/object:Gem::Version
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 39
|
57
|
+
segments:
|
58
|
+
- 2
|
59
|
+
- 10
|
60
|
+
- 0
|
53
61
|
version: 2.10.0
|
54
|
-
|
62
|
+
version_requirements: *id003
|
55
63
|
prerelease: false
|
56
|
-
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 2.10.0
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: bundler
|
64
|
-
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
|
-
requirements:
|
67
|
-
- - ~>
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: 1.1.0
|
64
|
+
name: rspec
|
70
65
|
type: :development
|
71
|
-
|
72
|
-
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
68
|
none: false
|
74
|
-
requirements:
|
69
|
+
requirements:
|
75
70
|
- - ~>
|
76
|
-
- !ruby/object:Gem::Version
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 19
|
73
|
+
segments:
|
74
|
+
- 1
|
75
|
+
- 1
|
76
|
+
- 0
|
77
77
|
version: 1.1.0
|
78
|
-
|
79
|
-
name: jeweler
|
80
|
-
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
|
-
requirements:
|
83
|
-
- - ~>
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
version: 1.6.2
|
86
|
-
type: :development
|
78
|
+
version_requirements: *id004
|
87
79
|
prerelease: false
|
88
|
-
|
80
|
+
name: bundler
|
81
|
+
type: :development
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
89
84
|
none: false
|
90
|
-
requirements:
|
85
|
+
requirements:
|
91
86
|
- - ~>
|
92
|
-
- !ruby/object:Gem::Version
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 11
|
89
|
+
segments:
|
90
|
+
- 1
|
91
|
+
- 6
|
92
|
+
- 2
|
93
93
|
version: 1.6.2
|
94
|
-
|
95
|
-
|
94
|
+
version_requirements: *id005
|
95
|
+
prerelease: false
|
96
|
+
name: jeweler
|
97
|
+
type: :development
|
98
|
+
description: Generates javascript file that defines all Rails named routes as javascript helpers
|
96
99
|
email: agresso@gmail.com
|
97
100
|
executables: []
|
101
|
+
|
98
102
|
extensions: []
|
99
|
-
|
103
|
+
|
104
|
+
extra_rdoc_files:
|
100
105
|
- LICENSE.txt
|
101
|
-
files:
|
106
|
+
files:
|
102
107
|
- .document
|
103
108
|
- .rspec
|
104
109
|
- .travis.yml
|
@@ -120,32 +125,39 @@ files:
|
|
120
125
|
- spec/js_routes/post_rails_init_spec.rb
|
121
126
|
- spec/js_routes/rails_routes_compatibility_spec.rb
|
122
127
|
- spec/spec_helper.rb
|
128
|
+
has_rdoc: true
|
123
129
|
homepage: http://github.com/railsware/js-routes
|
124
|
-
licenses:
|
130
|
+
licenses:
|
125
131
|
- MIT
|
126
132
|
post_install_message:
|
127
133
|
rdoc_options: []
|
128
|
-
|
134
|
+
|
135
|
+
require_paths:
|
129
136
|
- lib
|
130
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
138
|
none: false
|
132
|
-
requirements:
|
133
|
-
- -
|
134
|
-
- !ruby/object:Gem::Version
|
135
|
-
|
136
|
-
segments:
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
hash: 3
|
143
|
+
segments:
|
137
144
|
- 0
|
138
|
-
|
139
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
version: "0"
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
147
|
none: false
|
141
|
-
requirements:
|
142
|
-
- -
|
143
|
-
- !ruby/object:Gem::Version
|
144
|
-
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
hash: 3
|
152
|
+
segments:
|
153
|
+
- 0
|
154
|
+
version: "0"
|
145
155
|
requirements: []
|
156
|
+
|
146
157
|
rubyforge_project:
|
147
|
-
rubygems_version: 1.
|
158
|
+
rubygems_version: 1.6.0
|
148
159
|
signing_key:
|
149
160
|
specification_version: 3
|
150
161
|
summary: Brings Rails named routes to javascript
|
151
162
|
test_files: []
|
163
|
+
|