js-routes 0.8.1 → 0.8.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.
- data/Rakefile +0 -9
- data/VERSION +1 -1
- data/js-routes.gemspec +2 -2
- data/lib/js_routes.rb +8 -5
- data/lib/js_routes/engine.rb +9 -11
- data/lib/routes.js +14 -5
- data/spec/js_routes_spec.rb +51 -0
- data/spec/spec_helper.rb +4 -0
- metadata +13 -13
data/Rakefile
CHANGED
@@ -38,12 +38,3 @@ end
|
|
38
38
|
|
39
39
|
task :default => :spec
|
40
40
|
|
41
|
-
require 'rake/rdoctask'
|
42
|
-
Rake::RDocTask.new do |rdoc|
|
43
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
-
|
45
|
-
rdoc.rdoc_dir = 'rdoc'
|
46
|
-
rdoc.title = "js-routes #{version}"
|
47
|
-
rdoc.rdoc_files.include('README*')
|
48
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
-
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.2
|
data/js-routes.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "js-routes"
|
8
|
-
s.version = "0.8.
|
8
|
+
s.version = "0.8.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 = "2012-
|
12
|
+
s.date = "2012-06-23"
|
13
13
|
s.description = "Generates javascript file that defines all Rails named routes as javascript helpers"
|
14
14
|
s.email = "agresso@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/js_routes.rb
CHANGED
@@ -108,7 +108,7 @@ class JsRoutes
|
|
108
108
|
|
109
109
|
def js_routes
|
110
110
|
Rails.application.reload_routes!
|
111
|
-
js_routes = Rails.application.routes.named_routes.routes.map do |_, route|
|
111
|
+
js_routes = Rails.application.routes.named_routes.routes.sort_by(&:to_s).map do |_, route|
|
112
112
|
if route.app.respond_to?(:superclass) && route.app.superclass == Rails::Engine
|
113
113
|
route.app.routes.named_routes.map do |_, engine_route|
|
114
114
|
build_route_if_match(engine_route, route)
|
@@ -137,10 +137,13 @@ class JsRoutes
|
|
137
137
|
def build_js(route, parent_route)
|
138
138
|
name = [parent_route.try(:name), route.name].compact
|
139
139
|
parent_spec = parent_route.try(:path).try(:spec)
|
140
|
+
required_parts = route.required_parts.clone
|
141
|
+
optional_parts = route.optional_parts.clone
|
142
|
+
optional_parts.push(required_parts.delete :format) if required_parts.include?(:format)
|
140
143
|
_ = <<-JS.strip!
|
141
144
|
// #{name.join('.')} => #{parent_spec}#{route.path.spec}
|
142
|
-
#{name.join('_')}_path: function(#{build_params(
|
143
|
-
return Utils.build_path(#{json(
|
145
|
+
#{name.join('_')}_path: function(#{build_params(required_parts)}) {
|
146
|
+
return Utils.build_path(#{json(required_parts)}, #{json(optional_parts)}, #{json(serialize(route.path.spec, parent_spec))}, arguments);
|
144
147
|
}
|
145
148
|
JS
|
146
149
|
end
|
@@ -149,8 +152,8 @@ class JsRoutes
|
|
149
152
|
self.class.json(string)
|
150
153
|
end
|
151
154
|
|
152
|
-
def build_params
|
153
|
-
params =
|
155
|
+
def build_params required_parts
|
156
|
+
params = required_parts.map do |name|
|
154
157
|
# prepending each parameter name with underscore
|
155
158
|
# to prevent conflict with JS reserved words
|
156
159
|
"_" + name.to_s
|
data/lib/js_routes/engine.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
|
-
|
2
|
-
class
|
3
|
-
|
4
|
-
JS_ROUTES_ASSET = 'js-routes'
|
1
|
+
class JsRoutes
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
JS_ROUTES_ASSET = 'js-routes'
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
5
|
+
initializer 'js-routes.dependent_on_routes', :after => "sprockets.environment" do
|
6
|
+
if Rails.application.assets.respond_to?(:register_preprocessor)
|
7
|
+
routes = Rails.root.join('config','routes.rb')
|
8
|
+
Rails.application.assets.register_preprocessor 'application/javascript', :'js-routes_dependent_on_routes' do |ctx,data|
|
9
|
+
ctx.depend_on(routes) if ctx.logical_path == JS_ROUTES_ASSET
|
10
|
+
data
|
13
11
|
end
|
14
12
|
end
|
15
13
|
end
|
data/lib/routes.js
CHANGED
@@ -15,7 +15,11 @@
|
|
15
15
|
var Utils = {
|
16
16
|
|
17
17
|
serialize: function(obj){
|
18
|
-
if (obj
|
18
|
+
if (!obj) {return '';}
|
19
|
+
if (window.jQuery) {
|
20
|
+
var result = window.jQuery.param(obj);
|
21
|
+
return !result ? "" : "?" + result
|
22
|
+
}
|
19
23
|
var s = [];
|
20
24
|
for (prop in obj){
|
21
25
|
if (obj[prop]) {
|
@@ -36,7 +40,10 @@
|
|
36
40
|
},
|
37
41
|
|
38
42
|
clean_path: function(path) {
|
39
|
-
|
43
|
+
path = path.split("://");
|
44
|
+
last_index = path.length - 1;
|
45
|
+
path[last_index] = path[last_index].replace(/\/+/g, "/").replace(/\/$/m, '');
|
46
|
+
return path.join("://");
|
40
47
|
},
|
41
48
|
|
42
49
|
set_default_format: function(options) {
|
@@ -82,14 +89,14 @@
|
|
82
89
|
},
|
83
90
|
|
84
91
|
prepare_parameters: function(required_parameters, actual_parameters, options) {
|
85
|
-
var result = this.clone(options);
|
92
|
+
var result = this.clone(options) || {};
|
86
93
|
for (var i=0; i < required_parameters.length; i++) {
|
87
94
|
result[required_parameters[i]] = actual_parameters[i];
|
88
95
|
}
|
89
96
|
return result;
|
90
97
|
},
|
91
98
|
|
92
|
-
build_path: function(required_parameters, route, args) {
|
99
|
+
build_path: function(required_parameters, optional_parts, route, args) {
|
93
100
|
args = Array.prototype.slice.call(args);
|
94
101
|
var opts = this.extract_options(required_parameters.length, args);
|
95
102
|
if (args.length > required_parameters.length) {
|
@@ -97,7 +104,9 @@
|
|
97
104
|
}
|
98
105
|
|
99
106
|
parameters = this.prepare_parameters(required_parameters, args, opts);
|
100
|
-
|
107
|
+
if (optional_parts.indexOf('format') != -1) {
|
108
|
+
this.set_default_format(parameters);
|
109
|
+
}
|
101
110
|
var result = Utils.get_prefix();
|
102
111
|
var anchor = Utils.extract_anchor(parameters);
|
103
112
|
|
data/spec/js_routes_spec.rb
CHANGED
@@ -69,6 +69,10 @@ describe JsRoutes do
|
|
69
69
|
evaljs("Routes.blog_app_post_path(1)").should == blog_routes.post_path(1)
|
70
70
|
end
|
71
71
|
|
72
|
+
it "shouldn't require the format" do
|
73
|
+
evaljs("Routes.json_only_path({format: 'json'})").should == routes.json_only_path(:format => 'json')
|
74
|
+
end
|
75
|
+
|
72
76
|
context "routes globbing" do
|
73
77
|
it "should be supported as parameters" do
|
74
78
|
evaljs("Routes.book_path('thrillers', 1)").should == routes.book_path('thrillers', 1)
|
@@ -83,6 +87,33 @@ describe JsRoutes do
|
|
83
87
|
end
|
84
88
|
end
|
85
89
|
|
90
|
+
context "when jQuery is present" do
|
91
|
+
before do
|
92
|
+
evaljs("window.jQuery = {};")
|
93
|
+
jscontext[:parameterize] = lambda {|object| _value.to_param}
|
94
|
+
evaljs("window.jQuery.param = parameterize")
|
95
|
+
end
|
96
|
+
|
97
|
+
shared_examples_for "serialization" do
|
98
|
+
it "should support serialization of objects" do
|
99
|
+
evaljs("window.jQuery.param(#{_value.to_json})").should == _value.to_param
|
100
|
+
evaljs("Routes.inboxes_path(#{_value.to_json})").should == routes.inboxes_path(_value)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
context "when parameters is a hash" do
|
104
|
+
let(:_value) do
|
105
|
+
{a: {b: 'c'}, q: [1,2]}
|
106
|
+
end
|
107
|
+
it_should_behave_like 'serialization'
|
108
|
+
end
|
109
|
+
context "when parameters is null" do
|
110
|
+
let(:_value) do
|
111
|
+
nil
|
112
|
+
end
|
113
|
+
it_should_behave_like 'serialization'
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
86
117
|
context "using optional path fragments" do
|
87
118
|
context "including not optional parts" do
|
88
119
|
it "should include everything that is not optional" do
|
@@ -176,6 +207,15 @@ describe JsRoutes do
|
|
176
207
|
end
|
177
208
|
|
178
209
|
end
|
210
|
+
|
211
|
+
context "when prefix with http:// is specified" do
|
212
|
+
|
213
|
+
let(:_options) { {:prefix => "http://localhost:3000" } }
|
214
|
+
|
215
|
+
it "should render routing with prefix" do
|
216
|
+
evaljs("Routes.inbox_path(1)").should == _options[:prefix] + routes.inbox_path(1)
|
217
|
+
end
|
218
|
+
end
|
179
219
|
|
180
220
|
context "when prefix without trailing slash is specified" do
|
181
221
|
|
@@ -207,6 +247,13 @@ describe JsRoutes do
|
|
207
247
|
evaljs("Routes.inbox_path(1, {format: null})").should == routes.inbox_path(1)
|
208
248
|
end
|
209
249
|
|
250
|
+
it "shouldn't include the format when {:format => false} is specified" do
|
251
|
+
evaljs("Routes.no_format_path()").should == routes.no_format_path
|
252
|
+
end
|
253
|
+
|
254
|
+
it "shouldn't require the format" do
|
255
|
+
evaljs("Routes.json_only_path()").should == routes.json_only_path(:format => 'json')
|
256
|
+
end
|
210
257
|
end
|
211
258
|
|
212
259
|
describe "when namespace option is specified" do
|
@@ -282,6 +329,10 @@ describe JsRoutes do
|
|
282
329
|
it "should have correct function signature with Ruby 1.8.7 and unordered hash" do
|
283
330
|
should include("inbox_message_attachment_path: function(_inbox_id, _message_id, _id, options)")
|
284
331
|
end
|
332
|
+
|
333
|
+
it "routes should be sorted in alphabetical order" do
|
334
|
+
subject.index("book_path").should <= subject.index("inboxes_path")
|
335
|
+
end
|
285
336
|
end
|
286
337
|
|
287
338
|
describe ".generate!" do
|
data/spec/spec_helper.rb
CHANGED
@@ -56,6 +56,10 @@ class App < Rails::Application
|
|
56
56
|
match 'books/*section/:title' => 'books#show', :as => :book
|
57
57
|
|
58
58
|
mount BlogEngine::Engine => "/blog", :as => :blog_app
|
59
|
+
|
60
|
+
get '/no_format' => "foo#foo", :format => false, :as => :no_format
|
61
|
+
|
62
|
+
get '/json_only' => "foo#foo", :format => true, :constraints => {:format => /json/}, :as => :json_only
|
59
63
|
end
|
60
64
|
|
61
65
|
end
|
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.8.
|
4
|
+
version: 0.8.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &12169560 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.2'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *12169560
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: therubyracer
|
27
|
-
requirement: &
|
27
|
+
requirement: &12167600 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *12167600
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &12164260 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 2.7.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *12164260
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
requirement: &
|
49
|
+
requirement: &12177960 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.1.0
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *12177960
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: jeweler
|
60
|
-
requirement: &
|
60
|
+
requirement: &12176940 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: 1.6.2
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *12176940
|
69
69
|
description: Generates javascript file that defines all Rails named routes as javascript
|
70
70
|
helpers
|
71
71
|
email: agresso@gmail.com
|
@@ -108,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
segments:
|
110
110
|
- 0
|
111
|
-
hash:
|
111
|
+
hash: 379600740285990591
|
112
112
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
113
|
none: false
|
114
114
|
requirements:
|