js-routes 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +3 -1
- data/VERSION +1 -1
- data/js-routes.gemspec +2 -2
- data/lib/js_routes.rb +90 -67
- data/lib/routes.js +18 -4
- data/spec/js_routes_spec.rb +54 -6
- metadata +4 -4
data/Readme.md
CHANGED
@@ -30,8 +30,10 @@ Available options:
|
|
30
30
|
* Note that regexp applied to **named route** not to *URL*
|
31
31
|
* `:namespace` - global object used to access routes. Default: `Routes`
|
32
32
|
* Supports nested namespace like `MyProject.routes`
|
33
|
+
* `:prefix` - String representing a url path to prepend to all paths
|
34
|
+
* `Should be specified via :prefix => "/myprefix"`
|
33
35
|
|
34
|
-
|
36
|
+
This is how you can generate separated routes files for different parts of application:
|
35
37
|
|
36
38
|
``` ruby
|
37
39
|
JsRoutes.generate!(:file => "#{path}/app_routes.js", :namespace => "AppRoutes", :exclude => /^admin_/, :default_format => "json")
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.2
|
data/js-routes.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{js-routes}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.2"
|
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 = %q{2011-09-
|
12
|
+
s.date = %q{2011-09-21}
|
13
13
|
s.description = %q{Generates javascript file that defines all Rails named routes as javascript helpers}
|
14
14
|
s.email = %q{agresso@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/js_routes.rb
CHANGED
@@ -1,88 +1,111 @@
|
|
1
|
-
|
2
|
-
class << self
|
1
|
+
class JsRoutes
|
3
2
|
|
3
|
+
#
|
4
|
+
# API
|
5
|
+
#
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
def self.generate(options = {})
|
8
|
+
self.new(options).generate
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.generate!(options = {})
|
12
|
+
self.new(options).generate!
|
13
|
+
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
#
|
16
|
+
# Implementation
|
17
|
+
#
|
18
|
+
|
19
|
+
def initialize(options = {})
|
20
|
+
@options = default_options.merge(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def generate
|
24
|
+
js = File.read(File.dirname(__FILE__) + "/routes.js")
|
25
|
+
js.gsub!("NAMESPACE", @options[:namespace])
|
26
|
+
js.gsub!("DEFAULT_FORMAT", @options[:default_format].to_s)
|
27
|
+
js.gsub!("PREFIX", @options[:prefix])
|
28
|
+
js.gsub!("ROUTES", js_routes)
|
29
|
+
end
|
30
|
+
|
31
|
+
def generate!
|
32
|
+
# Some libraries like devise do yet load their routes so we will wait
|
33
|
+
# until initialization process finish
|
34
|
+
# https://github.com/railsware/js-routes/issues/7
|
35
|
+
Rails.configuration.after_initialize do
|
36
|
+
File.open(@options[:file], 'w') do |f|
|
37
|
+
f.write generate
|
17
38
|
end
|
18
39
|
end
|
40
|
+
end
|
19
41
|
|
20
|
-
|
21
|
-
# Implementation
|
22
|
-
#
|
23
|
-
|
24
|
-
protected
|
25
|
-
def js_routes(options = {})
|
26
|
-
|
27
|
-
options[:default_format] ||= ""
|
28
|
-
excludes = options[:exclude] || []
|
29
|
-
includes = options[:include] || //
|
30
|
-
|
31
|
-
Rails.application.reload_routes!
|
32
|
-
js_routes = Rails.application.routes.named_routes.routes.map do |_, route|
|
33
|
-
if any_match?(route, excludes) || !any_match?(route, includes)
|
34
|
-
nil
|
35
|
-
else
|
36
|
-
build_js(route, options)
|
37
|
-
end
|
38
|
-
end.compact
|
39
|
-
|
40
|
-
"{\n" + js_routes.join(",\n") + "}\n"
|
41
|
-
end
|
42
|
+
protected
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
def js_routes
|
45
|
+
Rails.application.reload_routes!
|
46
|
+
js_routes = Rails.application.routes.named_routes.routes.map do |_, route|
|
47
|
+
if any_match?(route, @options[:exclude]) || !any_match?(route, @options[:include])
|
48
|
+
nil
|
49
|
+
else
|
50
|
+
build_js(route)
|
51
|
+
end
|
52
|
+
end.compact
|
53
|
+
|
54
|
+
"{\n" + js_routes.join(",\n") + "}\n"
|
55
|
+
end
|
56
|
+
|
57
|
+
def any_match?(route, matchers)
|
58
|
+
matchers = Array(matchers)
|
59
|
+
matchers.any? {|regex| route.name =~ regex}
|
60
|
+
end
|
47
61
|
|
48
|
-
|
49
|
-
|
50
|
-
|
62
|
+
def build_js(route)
|
63
|
+
params = build_params route
|
64
|
+
_ = <<-JS.strip!
|
51
65
|
// #{route.name} => #{route.path}
|
52
66
|
#{route.name}_path: function(#{params.<<("options").join(", ")}) {
|
53
67
|
return Utils.build_path(#{params.size}, #{path_parts(route).inspect}, arguments)
|
54
68
|
}
|
55
|
-
JS
|
56
|
-
|
69
|
+
JS
|
70
|
+
end
|
57
71
|
|
58
72
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
73
|
+
def build_params route
|
74
|
+
route.conditions[:path_info].named_captures.to_a.sort do |cap1, cap2|
|
75
|
+
# Hash is not ordered in Ruby 1.8.7
|
76
|
+
cap1.last.first <=> cap2.last.first
|
77
|
+
end.map do |cap|
|
78
|
+
name = cap.first
|
79
|
+
if !(name.to_s == "format")
|
80
|
+
# prepending each parameter name with underscore
|
81
|
+
# to prevent conflict with JS reserved words
|
82
|
+
"_" + name.to_s.gsub(/^:/, '')
|
83
|
+
end
|
84
|
+
end.compact
|
85
|
+
end
|
72
86
|
|
73
87
|
|
74
|
-
|
75
|
-
|
76
|
-
|
88
|
+
def path_parts route
|
89
|
+
route.path.gsub(/\(\.:format\)$/, "").split(/:[a-z\-_]+/)
|
90
|
+
end
|
77
91
|
|
92
|
+
def default_options
|
93
|
+
{
|
94
|
+
:namespace => "Routes",
|
95
|
+
:default_format => "",
|
96
|
+
:exclude => [],
|
97
|
+
:include => //,
|
98
|
+
:file => default_file,
|
99
|
+
:prefix => ""
|
100
|
+
}
|
101
|
+
end
|
78
102
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
end
|
103
|
+
def default_file
|
104
|
+
if Rails.version >= "3.1"
|
105
|
+
"#{Rails.root}/app/assets/javascripts/routes.js"
|
106
|
+
else
|
107
|
+
"#{Rails.root}/public/javascripts/routes.js"
|
85
108
|
end
|
86
|
-
|
87
109
|
end
|
110
|
+
|
88
111
|
end
|
data/lib/routes.js
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
var Utils = {
|
4
4
|
|
5
|
-
default_format: 'DEFAULT_FORMAT',
|
6
|
-
|
7
5
|
serialize: function(obj){
|
8
6
|
if (obj === null) {return '';}
|
9
7
|
var s = [];
|
@@ -21,7 +19,7 @@
|
|
21
19
|
},
|
22
20
|
|
23
21
|
extract_format: function(options) {
|
24
|
-
var format = options.hasOwnProperty("format") ? options.format :
|
22
|
+
var format = options.hasOwnProperty("format") ? options.format : window.NAMESPACE.options.default_format;
|
25
23
|
delete options.format;
|
26
24
|
return format ? "." + format : "";
|
27
25
|
},
|
@@ -47,7 +45,7 @@
|
|
47
45
|
|
48
46
|
build_path: function(number_of_params, parts, args) {
|
49
47
|
args = Array.prototype.slice.call(args);
|
50
|
-
result =
|
48
|
+
result = Utils.get_prefix();
|
51
49
|
var opts = Utils.extract_options(number_of_params, args);
|
52
50
|
for (var i=0; i < parts.length; i++) {
|
53
51
|
value = args.shift();
|
@@ -71,10 +69,26 @@
|
|
71
69
|
|
72
70
|
optional_part: function(part) {
|
73
71
|
return part.match(/\(/);
|
72
|
+
},
|
73
|
+
|
74
|
+
get_prefix: function(){
|
75
|
+
var prefix = window.NAMESPACE.options.prefix;
|
76
|
+
|
77
|
+
if( prefix !== "" ){
|
78
|
+
prefix = prefix.match('\/$') ? prefix : ( prefix + '/');
|
79
|
+
}
|
80
|
+
|
81
|
+
return prefix;
|
74
82
|
}
|
75
83
|
|
76
84
|
};
|
77
85
|
|
78
86
|
window.NAMESPACE = ROUTES;
|
87
|
+
|
88
|
+
window.NAMESPACE.options = {
|
89
|
+
prefix: 'PREFIX',
|
90
|
+
default_format: 'DEFAULT_FORMAT',
|
91
|
+
};
|
92
|
+
|
79
93
|
|
80
94
|
})();
|
data/spec/js_routes_spec.rb
CHANGED
@@ -66,6 +66,36 @@ describe JsRoutes do
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
+
context "when prefix with trailing slash is specified" do
|
70
|
+
|
71
|
+
let(:_options) { {:prefix => "/myprefix/" } }
|
72
|
+
|
73
|
+
it "should render routing with prefix" do
|
74
|
+
evaljs("Routes.inbox_path(1)").should == "/myprefix/inboxes/1"
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should render routing with prefix set in JavaScript" do
|
78
|
+
evaljs("Routes.options.prefix = '/newprefix/'")
|
79
|
+
evaljs("Routes.inbox_path(1)").should == "/newprefix/inboxes/1"
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
context "when prefix without trailing slash is specified" do
|
85
|
+
|
86
|
+
let(:_options) { {:prefix => "/myprefix" } }
|
87
|
+
|
88
|
+
it "should render routing with prefix" do
|
89
|
+
evaljs("Routes.inbox_path(1)").should == "/myprefix/inboxes/1"
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should render routing with prefix set in JavaScript" do
|
93
|
+
evaljs("Routes.options.prefix = '/newprefix'")
|
94
|
+
evaljs("Routes.inbox_path(1)").should == "/newprefix/inboxes/1"
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
69
99
|
context "when default_format is specified" do
|
70
100
|
let(:_options) { {:default_format => "json"} }
|
71
101
|
|
@@ -73,7 +103,7 @@ describe JsRoutes do
|
|
73
103
|
evaljs("Routes.inbox_path(1)").should == "/inboxes/1.json"
|
74
104
|
end
|
75
105
|
|
76
|
-
it "should override default_format
|
106
|
+
it "should override default_format when spefified implicitly" do
|
77
107
|
evaljs("Routes.inbox_path(1, {format: 'xml'})").should == "/inboxes/1.xml"
|
78
108
|
end
|
79
109
|
|
@@ -138,23 +168,41 @@ describe JsRoutes do
|
|
138
168
|
describe "generated js" do
|
139
169
|
subject { JsRoutes.generate }
|
140
170
|
it "should have correct function without arguments signature" do
|
141
|
-
|
171
|
+
should include("inboxes_path: function(options)")
|
142
172
|
end
|
143
173
|
it "should have correct function with arguments signature" do
|
144
|
-
|
174
|
+
should include("inbox_message_path: function(_inbox_id, _id, options)")
|
145
175
|
end
|
146
176
|
it "should have correct function signature with Ruby 1.8.7 and unordered hash" do
|
147
|
-
|
177
|
+
should include("inbox_message_attachment_path: function(_inbox_id, _message_id, _id, options)")
|
148
178
|
end
|
149
179
|
end
|
150
180
|
|
151
181
|
describe ".generate!" do
|
182
|
+
|
152
183
|
let(:name) { "#{File.dirname(__FILE__)}/../routes.js" }
|
153
|
-
|
184
|
+
|
185
|
+
before(:each) do
|
186
|
+
# prevent warning
|
187
|
+
Rails.configuration.active_support.deprecation = :log
|
188
|
+
|
154
189
|
FileUtils.rm_f(name)
|
155
190
|
JsRoutes.generate!({:file => name})
|
156
|
-
File.exists?(name).should be_true
|
157
191
|
end
|
192
|
+
|
193
|
+
it "should not generate file at once" do
|
194
|
+
File.exists?(name).should be_false
|
195
|
+
end
|
196
|
+
|
197
|
+
context "after Rails initialization" do
|
198
|
+
before(:each) do
|
199
|
+
Rails.application.initialize!
|
200
|
+
end
|
201
|
+
it "should generate routes file only after rails initialization" do
|
202
|
+
File.exists?(name).should be_true
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
158
206
|
after(:all) do
|
159
207
|
FileUtils.rm_f(name)
|
160
208
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js-routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 2
|
10
|
+
version: 0.6.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Bogdan Gusiev
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-09-
|
18
|
+
date: 2011-09-21 00:00:00 +03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|