js-routes 0.9.9 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11383682f9433b305670ebe17feb5ac27ccb7626
4
- data.tar.gz: 422793010efafcc0e4a205c0e3d04f5bdcfdf533
3
+ metadata.gz: 82dc4e938c01969d6fa291afba76adbdc12106ce
4
+ data.tar.gz: ccab6107d6e2a86ab1529a2fa4ce06e4af50c820
5
5
  SHA512:
6
- metadata.gz: e75c78237118f839aedad8697e13c4f4364eec313c9ab6c84993f12bdfd98a08e8aae1914aa17f0318cd0130c9a763c3d145795a74b15df4ea55fd9fe6875c65
7
- data.tar.gz: badb9598727f1b815a5b3ca7b4e746b723c6321de85e0233ee647d13052d929cc3a7663615d89e77a2e75df22417545872ad1c30b68315170a901f219a7310e8
6
+ metadata.gz: 027362db5b8c3f3848cdea09fa9095364a3307e9f492ca5d871d9174e676e05492d5149818f76df989b53e15499465287c93644fa19b0b55117da8b1ed0db0b8
7
+ data.tar.gz: bbe7ac6a521322201322a01dcb847dcee803f8524dd1f190c17e5c99fe24fb5a6b57d27dd66644ff0b5be4c4fbc733dc69853d619cda8c975e7269f182daddcb
data/.travis.yml CHANGED
@@ -1,22 +1,33 @@
1
1
  language: ruby
2
+ cache: bundler
3
+
2
4
  rvm:
3
5
  - 1.9.3
4
- - 2.0.0
5
- - 2.1.2
6
+ - 2.0
7
+ - 2.1
8
+ - 2.2
6
9
  - jruby-19mode
7
10
  - ruby-head
8
11
  - jruby-head
12
+
9
13
  gemfile:
10
14
  - gemfiles/rails32.gemfile
11
15
  - gemfiles/rails40.gemfile
12
16
  - gemfiles/rails41.gemfile
17
+ - gemfiles/rails42.gemfile
18
+
19
+ sudo: false
20
+
13
21
  notifications:
14
22
  email:
15
23
  - agresso@gmail.com
24
+
16
25
  branches:
17
26
  only:
18
27
  - master
28
+
19
29
  matrix:
20
30
  allow_failures:
31
+ - rvm: jruby-19mode
21
32
  - rvm: ruby-head
22
33
  - rvm: jruby-head
data/Appraisals CHANGED
@@ -5,10 +5,12 @@ end
5
5
 
6
6
  appraise "rails40" do
7
7
  gem "railties", "~> 4.0.5"
8
- gem "minitest", "~> 4.0"
9
8
  end
10
9
 
11
10
  appraise "rails41" do
12
11
  gem "railties", "~> 4.1.1"
13
- gem "minitest", ">= 4.0"
12
+ end
13
+
14
+ appraise "rails42" do
15
+ gem "railties", "~> 4.2.0"
14
16
  end
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## master
2
2
 
3
+ * Add the compact mode [#125](https://github.com/railsware/js-routes/pull/125)
4
+ * Add support for host, protocol, and port configuration [#137](https://github.com/railsware/js-routes/pull/137)
5
+ * Routes path specs [#135](https://github.com/railsware/js-routes/pull/135)
6
+ * Support Rails 4.2 and Ruby 2.2 [#140](https://github.com/railsware/js-routes/pull/140)
7
+
8
+ ## v0.9.9
9
+
10
+ * Bugfix Rails Engine subapplication route generation when they are nested [#120](https://github.com/railsware/js-routes/pull/120)
11
+
3
12
  ## v0.9.8
4
13
 
5
14
  * Support AMD/Require.js [#111](https://github.com/railsware/js-routes/pull/111)
@@ -11,4 +20,4 @@
11
20
  * Fix typeof error for IE [#95](https://github.com/railsware/js-routes/pull/95)
12
21
  * Fix testing on ruby-head [#92](https://github.com/railsware/js-routes/pull/92)
13
22
  * Correct thread safety issue in js-routes generation [#90](https://github.com/railsware/js-routes/pull/90)
14
- * Use the `of` operator to detect for `to_param` and `id` in objects [#87](https://github.com/railsware/js-routes/pull/87)
23
+ * Use the `of` operator to detect for `to_param` and `id` in objects [#87](https://github.com/railsware/js-routes/pull/87)
data/Readme.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # JsRoutes
2
- [![Build Status](https://travis-ci.org/railsware/js-routes.png)](https://travis-ci.org/railsware/js-routes)
2
+ [![Build Status](https://travis-ci.org/railsware/js-routes.svg?branch=master)](https://travis-ci.org/railsware/js-routes)
3
3
 
4
4
  Generates javascript file that defines all Rails named routes as javascript helpers
5
5
 
@@ -43,9 +43,10 @@ end
43
43
 
44
44
  Available options:
45
45
 
46
- * `default_url_options` - default parameters to be used to generate url
47
- * Note that currently only optional parameters (like `:format` or `:trailing_slash`) can be defaulted.
48
- * Example: {:format => "json", :trailing_slash => true}
46
+ * `default_url_options` - default parameters used when generating URLs
47
+ * Note that only specific options are supported at this time.
48
+ * Supported options: `:format`, `:trailing_slash`, `:protocol`, `:host`, `:port`
49
+ * Example: {:format => "json", :trailing_slash => true, :protocol => "https", :host => "example.com", :port => 3000}
49
50
  * Default: {}
50
51
  * `exclude` - Array of regexps to exclude from js routes.
51
52
  * Default: []
@@ -61,9 +62,13 @@ Available options:
61
62
  * Default: blank
62
63
  * `camel_case` (version >= 0.8.8) - Generate camel case route names.
63
64
  * Default: false
64
- * `url_links` (version >= 0.8.9) - Generate `*_url` links (in addition to default `*_path`), where url_links value is beginning of url routes
65
- * Example: http[s]://example.com
65
+ * `url_links` (version >= 0.8.9) - Generate `*_url` helpers (in addition to the default `*_path` helpers).
66
+ * Example: true
66
67
  * Default: false
68
+ * Be sure to specify a default host in `default_url_options`. Routes which specify a specific host, protocol, or port will be used instead of their corresponding default.
69
+ * `compact` (version > 0.9.9) - Remove `_path` suffix in path routes(`*_url` routes stay untouched if they were enabled)
70
+ * Default: false
71
+ * Sample route call when option is set to true: Routes.users() => `/users`
67
72
 
68
73
  ### Very Advanced Setup
69
74
 
@@ -129,6 +134,29 @@ In order to make routes helpers available globally:
129
134
  jQuery.extend(window, Routes)
130
135
  ```
131
136
 
137
+ ## Get spec of routes and required params
138
+
139
+ Possible to get `spec` of route by function `toString`:
140
+
141
+ ```js
142
+ Routes.users_path.toString() // => "/users(.:format)"
143
+ Routes.user_path.toString() // => "/users/:id(.:format)"
144
+ ```
145
+
146
+ This function allow to get the same `spec` for route, if you will get string representation of the route function:
147
+
148
+ ```js
149
+ '' + Routes.users_path // => "/users(.:format)", a string representation of the object
150
+ '' + Routes.user_path // => "/users/:id(.:format)"
151
+ ```
152
+
153
+ Route function also contain inside attribute `required_params` required param names as array:
154
+
155
+ ```js
156
+ Routes.users_path.required_params // => []
157
+ Routes.user_path.required_params // => ['id']
158
+ ```
159
+
132
160
  ## What about security?
133
161
 
134
162
  js-routes itself do not have security holes. It makes URLs
@@ -145,7 +173,7 @@ Spork.trap_method(JsRoutes, :generate!)
145
173
 
146
174
  ## JS-Routes and heroku
147
175
 
148
- Heroku environment has a specific problems with setup. It is impossible to use asset pipeline in this environtment. You should use "Very Advanced Setup" schema in this case.
176
+ Heroku environment has a specific problems with setup. It is impossible to use asset pipeline in this environment. You should use "Very Advanced Setup" schema in this case.
149
177
 
150
178
  For example create routes.js.erb in assets folder with needed content:
151
179
 
@@ -5,4 +5,4 @@ source "http://rubygems.org"
5
5
  gem "railties", "~> 3.2.18"
6
6
  gem "tzinfo"
7
7
 
8
- gemspec :path=>"../"
8
+ gemspec :path => "../"
@@ -3,6 +3,5 @@
3
3
  source "http://rubygems.org"
4
4
 
5
5
  gem "railties", "~> 4.0.5"
6
- gem "minitest", "~> 4.0"
7
6
 
8
- gemspec :path=>"../"
7
+ gemspec :path => "../"
@@ -3,6 +3,5 @@
3
3
  source "http://rubygems.org"
4
4
 
5
5
  gem "railties", "~> 4.1.1"
6
- gem "minitest", ">= 4.0"
7
6
 
8
- gemspec :path=>"../"
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "railties", "~> 4.2.0"
6
+
7
+ gemspec :path => "../"
data/js-routes.gemspec CHANGED
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.homepage = %q{http://github.com/railsware/js-routes}
19
19
  s.licenses = ["MIT"]
20
20
  s.require_paths = ["lib"]
21
- s.rubygems_version = %q{1.6.0}
22
21
  s.summary = %q{Brings Rails named routes to javascript}
23
22
 
24
23
  s.add_runtime_dependency(%q<railties>, [">= 3.2"])
@@ -29,10 +28,9 @@ Gem::Specification.new do |s|
29
28
  s.add_development_dependency(%q<guard-coffeescript>, [">= 0"])
30
29
  s.add_development_dependency(%q<appraisal>, [">= 0.5.2"])
31
30
  if defined?(JRUBY_VERSION)
32
- s.add_development_dependency(%q<therubyrhino>, [">= 0"])
31
+ s.add_development_dependency(%q<therubyrhino>, [">= 2.0.4"])
33
32
  else
34
- s.add_development_dependency(%q<debugger>, [">= 0"]) if RUBY_VERSION =~ /\A2\.0/
35
- s.add_development_dependency(%q<therubyracer>, [">= 0"])
33
+ s.add_development_dependency(%q<therubyracer>, [">= 0.12.1"])
36
34
  end
37
35
  end
38
36
 
data/lib/js_routes.rb CHANGED
@@ -18,7 +18,8 @@ class JsRoutes
18
18
  prefix: nil,
19
19
  url_links: nil,
20
20
  camel_case: false,
21
- default_url_options: {}
21
+ default_url_options: {},
22
+ compact: false
22
23
  }
23
24
 
24
25
  NODE_TYPES = {
@@ -124,8 +125,9 @@ class JsRoutes
124
125
 
125
126
  def js_routes
126
127
  js_routes = Rails.application.routes.named_routes.routes.sort_by(&:to_s).map do |_, route|
127
- if route.app.respond_to?(:superclass) && route.app.superclass == Rails::Engine && !route.path.anchored
128
- route.app.routes.named_routes.map do |_, engine_route|
128
+ rails_engine_app = get_app_from_route(route)
129
+ if rails_engine_app.respond_to?(:superclass) && rails_engine_app.superclass == Rails::Engine && !route.path.anchored
130
+ rails_engine_app.routes.named_routes.map do |_, engine_route|
129
131
  build_route_if_match(engine_route, route)
130
132
  end
131
133
  else
@@ -136,6 +138,15 @@ class JsRoutes
136
138
  "{\n" + js_routes.join(",\n") + "}\n"
137
139
  end
138
140
 
141
+ def get_app_from_route(route)
142
+ # rails engine in Rails 4.2 use additional ActionDispatch::Routing::Mapper::Constraints, which contain app
143
+ if route.app.respond_to?(:app) && route.app.respond_to?(:constraints)
144
+ route.app.app
145
+ else
146
+ route.app
147
+ end
148
+ end
149
+
139
150
  def build_route_if_match(route, parent_route=nil)
140
151
  if any_match?(route, parent_route, @options[:exclude]) || !any_match?(route, parent_route, @options[:include])
141
152
  nil
@@ -154,28 +165,47 @@ class JsRoutes
154
165
  parent_spec = parent_route.try(:path).try(:spec)
155
166
  required_parts, optional_parts = route.required_parts.clone, route.optional_parts.clone
156
167
  optional_parts.push(required_parts.delete :format) if required_parts.include?(:format)
157
- route_name = generate_route_name(name)
158
- url_link = generate_url_link(name, route_name, required_parts)
168
+ route_name = generate_route_name(name, (:path unless @options[:compact]))
169
+ url_link = generate_url_link(name, route_name, required_parts, route)
159
170
  _ = <<-JS.strip!
160
171
  // #{name.join('.')} => #{parent_spec}#{route.path.spec}
161
- #{route_name}: function(#{build_params(required_parts)}) {
162
- return Utils.build_path(#{json(required_parts)}, #{json(optional_parts)}, #{json(serialize(route.path.spec, parent_spec))}, arguments);
163
- }#{",\n" + url_link if url_link.length > 0}
172
+ // function(#{[required_parts, LAST_OPTIONS_KEY].flatten.join(', ')})
173
+ #{route_name}: Utils.route(#{json(required_parts)}, #{json(optional_parts)}, #{json(serialize(route.path.spec, parent_spec))}, arguments)#{",\n" + url_link if url_link.length > 0}
164
174
  JS
165
175
  end
166
176
 
167
- def generate_url_link(name, route_name, required_parts)
177
+ def generate_url_link(name, route_name, required_parts, route)
168
178
  return "" unless @options[:url_links]
169
- raise "invalid URL format in url_links (ex: http[s]://example.com)" if @options[:url_links].match(URI::regexp(%w(http https))).nil?
170
179
  _ = <<-JS.strip!
171
- #{generate_route_name(name, true)}: function(#{build_params(required_parts)}) {
172
- return "" + #{@options[:url_links].inspect} + this.#{route_name}(#{build_params(required_parts)});
180
+ #{generate_route_name(name, :url)}: function(#{build_params(required_parts)}) {
181
+ return #{generate_base_url_js(route)} + this.#{route_name}(#{build_params(required_parts)});
173
182
  }
174
183
  JS
175
184
  end
176
185
 
177
- def generate_route_name(name, is_url = false)
178
- route_name = "#{name.join('_')}_#{is_url ? "url" : "path"}"
186
+ def generate_base_url_js(route)
187
+ # preserve and deprecate previous behavior
188
+ unless @options[:url_links] == true
189
+ ActiveSupport::Deprecation.warn('js-routes url_links config value must be a boolean. Use default_url_options for specifying a default host.')
190
+ 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?
191
+ return "#{@options[:url_links].inspect}"
192
+ else
193
+ protocol = route.defaults[:protocol] || @options[:default_url_options][:protocol] || 'http'
194
+ hostname = route.defaults[:host] || @options[:default_url_options][:host]
195
+ port = route.defaults[:port] || (@options[:default_url_options][:port] unless route.defaults[:host])
196
+ port = ":#{port}" if port
197
+
198
+ unless hostname
199
+ raise "A :default_url_options[:host] must be configured in order to generate *_url helpers"
200
+ end
201
+
202
+ return %Q|'#{protocol}://#{hostname}#{port}'|
203
+ end
204
+ end
205
+
206
+ def generate_route_name(name, suffix)
207
+ route_name = name.join('_')
208
+ route_name << "_#{ suffix }" if suffix
179
209
  @options[:camel_case] ? route_name.camelize(:lower) : route_name
180
210
  end
181
211
 
@@ -183,7 +213,7 @@ class JsRoutes
183
213
  self.class.json(string)
184
214
  end
185
215
 
186
- def build_params required_parts
216
+ def build_params(required_parts)
187
217
  params = required_parts.map do |name|
188
218
  # prepending each parameter name with underscore
189
219
  # to prevent conflict with JS reserved words
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "0.9.9"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/routes.js CHANGED
@@ -233,6 +233,35 @@
233
233
  throw new Error("Unknown Rails node type");
234
234
  }
235
235
  },
236
+ build_path_spec: function(route, wildcard) {
237
+ var left, right, type;
238
+
239
+ if (wildcard == null) {
240
+ wildcard = false;
241
+ }
242
+ type = route[0], left = route[1], right = route[2];
243
+ switch (type) {
244
+ case NodeTypes.GROUP:
245
+ return "(" + (this.build_path_spec(left)) + ")";
246
+ case NodeTypes.CAT:
247
+ return "" + (this.build_path_spec(left)) + (this.build_path_spec(right));
248
+ case NodeTypes.STAR:
249
+ return this.build_path_spec(left, true);
250
+ case NodeTypes.SYMBOL:
251
+ if (wildcard === true) {
252
+ return "" + (left[0] === '*' ? '' : '*') + left;
253
+ } else {
254
+ return ":" + left;
255
+ }
256
+ break;
257
+ case NodeTypes.SLASH:
258
+ case NodeTypes.DOT:
259
+ case NodeTypes.LITERAL:
260
+ return left;
261
+ default:
262
+ throw new Error("Unknown Rails node type");
263
+ }
264
+ },
236
265
  visit_globbing: function(route, parameters, optional) {
237
266
  var left, right, type, value;
238
267
 
@@ -263,6 +292,18 @@
263
292
  }
264
293
  return prefix;
265
294
  },
295
+ route: function(required_parts, optional_parts, route_spec) {
296
+ var path_fn;
297
+
298
+ path_fn = function() {
299
+ return Utils.build_path(required_parts, optional_parts, route_spec, arguments);
300
+ };
301
+ path_fn.required_params = required_parts;
302
+ path_fn.toString = function() {
303
+ return Utils.build_path_spec(route_spec);
304
+ };
305
+ return path_fn;
306
+ },
266
307
  _classToTypeCache: null,
267
308
  _classToType: function() {
268
309
  var name, _i, _len, _ref;
data/lib/routes.js.coffee CHANGED
@@ -167,6 +167,29 @@ Utils =
167
167
  else
168
168
  throw new Error("Unknown Rails node type")
169
169
 
170
+ #
171
+ # This method build spec for route
172
+ #
173
+ build_path_spec: (route, wildcard=false) ->
174
+ [type, left, right] = route
175
+ switch type
176
+ when NodeTypes.GROUP
177
+ "(#{@build_path_spec(left)})"
178
+ when NodeTypes.CAT
179
+ "#{@build_path_spec(left)}#{@build_path_spec(right)}"
180
+ when NodeTypes.STAR
181
+ @build_path_spec(left, true)
182
+ when NodeTypes.SYMBOL
183
+ if wildcard is true
184
+ "#{if left[0] is '*' then '' else '*'}#{left}"
185
+ else
186
+ ":#{left}"
187
+ when NodeTypes.SLASH, NodeTypes.DOT, NodeTypes.LITERAL
188
+ left
189
+ # Not sure about this one
190
+ # when NodeTypes.OR
191
+ else throw new Error("Unknown Rails node type")
192
+
170
193
  #
171
194
  # This method convert value for globbing in right value for rails route
172
195
  #
@@ -191,6 +214,15 @@ Utils =
191
214
  prefix = (if prefix.match("/$") then prefix else "#{prefix}/") if prefix isnt ""
192
215
  prefix
193
216
 
217
+ #
218
+ # route function: create route path function and add spec to it
219
+ #
220
+ route: (required_parts, optional_parts, route_spec) ->
221
+ path_fn = -> Utils.build_path(required_parts, optional_parts, route_spec, arguments)
222
+ path_fn.required_params = required_parts
223
+ path_fn.toString = -> Utils.build_path_spec(route_spec)
224
+ path_fn
225
+
194
226
  #
195
227
  # This is helper method to define object type.
196
228
  # The typeof operator is probably the biggest design flaw of JavaScript, simply because it's basically completely broken.
@@ -9,14 +9,24 @@ describe JsRoutes do
9
9
 
10
10
  describe "generated js" do
11
11
  subject { JsRoutes.generate }
12
+ it "should call route function for each route" do
13
+ is_expected.to include("inboxes_path: Utils.route(")
14
+ end
12
15
  it "should have correct function without arguments signature" do
13
- is_expected.to include("inboxes_path: function(options)")
16
+ is_expected.to include("inboxes_path: Utils.route([]")
14
17
  end
15
18
  it "should have correct function with arguments signature" do
16
- is_expected.to include("inbox_message_path: function(_inbox_id, _id, options)")
19
+ is_expected.to include("inbox_message_path: Utils.route([\"inbox_id\",\"id\"]")
17
20
  end
18
21
  it "should have correct function signature with unordered hash" do
19
- is_expected.to include("inbox_message_attachment_path: function(_inbox_id, _message_id, _id, options)")
22
+ is_expected.to include("inbox_message_attachment_path: Utils.route([\"inbox_id\",\"message_id\",\"id\"]")
23
+ end
24
+
25
+ it "should have correct function comment with options argument" do
26
+ is_expected.to include("// function(options)\n inboxes_path: Utils.route")
27
+ end
28
+ it "should have correct function comment with arguments" do
29
+ is_expected.to include("// function(inbox_id, message_id, options)\n new_inbox_message_attachment_path: Utils.route")
20
30
  end
21
31
 
22
32
  it "routes should be sorted in alphabetical order" do
@@ -51,7 +61,7 @@ describe JsRoutes do
51
61
  describe "compiled javascript asset" do
52
62
  subject { ERB.new(File.read("app/assets/javascripts/js-routes.js.erb")).result(binding) }
53
63
  it "should have js routes code" do
54
- is_expected.to include("inbox_message_path: function(_inbox_id, _id, options)")
64
+ is_expected.to include("inbox_message_path: Utils.route([\"inbox_id\",\"id\"]")
55
65
  end
56
66
  end
57
67
  end
@@ -238,40 +238,171 @@ describe JsRoutes, "options" do
238
238
  end
239
239
  end
240
240
 
241
- context "with host" do
242
- let(:_options) { { :url_links => "http://localhost" } }
243
- it "should generate path and url links" do
244
- expect(evaljs("Routes.inbox_path")).not_to be_nil
245
- expect(evaljs("Routes.inbox_url")).not_to be_nil
246
- expect(evaljs("Routes.inbox_path(1)")).to eq(routes.inbox_path(1))
247
- expect(evaljs("Routes.inbox_url(1)")).to eq("http://localhost#{routes.inbox_path(1)}")
248
- expect(evaljs("Routes.inbox_url(1, { test_key: \"test_val\" })")).to eq("http://localhost#{routes.inbox_path(1, :test_key => "test_val")}")
241
+ context 'with deprecated, non-boolean config value' do
242
+ around(:each) do |example|
243
+ ActiveSupport::Deprecation.silence do
244
+ example.run
245
+ end
246
+ end
247
+
248
+ context "with host" do
249
+ let(:_options) { { :url_links => "http://localhost" } }
250
+ it "should generate path and url links" do
251
+ expect(evaljs("Routes.inbox_path")).not_to be_nil
252
+ expect(evaljs("Routes.inbox_url")).not_to be_nil
253
+ expect(evaljs("Routes.inbox_path(1)")).to eq(routes.inbox_path(1))
254
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://localhost#{routes.inbox_path(1)}")
255
+ expect(evaljs("Routes.inbox_url(1, { test_key: \"test_val\" })")).to eq("http://localhost#{routes.inbox_path(1, :test_key => "test_val")}")
256
+ end
249
257
  end
250
- end
251
258
 
252
- context "with invalid host" do
253
- it "should raise error" do
254
- expect { JsRoutes.generate({ :url_links => "localhost" }) }.to raise_error RuntimeError
259
+ context "with invalid host" do
260
+ it "should raise error" do
261
+ expect { JsRoutes.generate({ :url_links => "localhost" }) }.to raise_error RuntimeError
262
+ end
263
+ end
264
+
265
+ context "with host and camel_case" do
266
+ let(:_options) { { :camel_case => true, :url_links => "http://localhost" } }
267
+ it "should generate path and url links" do
268
+ expect(evaljs("Routes.inboxPath")).not_to be_nil
269
+ expect(evaljs("Routes.inboxUrl")).not_to be_nil
270
+ expect(evaljs("Routes.inboxPath(1)")).to eq(routes.inbox_path(1))
271
+ expect(evaljs("Routes.inboxUrl(1)")).to eq("http://localhost#{routes.inbox_path(1)}")
272
+ end
273
+ end
274
+
275
+ context "with host and prefix" do
276
+ let(:_options) { { :prefix => "/api", :url_links => "https://example.com" } }
277
+ it "should generate path and url links" do
278
+ expect(evaljs("Routes.inbox_path")).not_to be_nil
279
+ expect(evaljs("Routes.inbox_url")).not_to be_nil
280
+ expect(evaljs("Routes.inbox_path(1)")).to eq("/api#{routes.inbox_path(1)}")
281
+ expect(evaljs("Routes.inbox_url(1)")).to eq("https://example.com/api#{routes.inbox_path(1)}")
282
+ end
255
283
  end
256
284
  end
257
285
 
258
- context "with host and camel_case" do
259
- let(:_options) { { :camel_case => true, :url_links => "http://localhost" } }
260
- it "should generate path and url links" do
261
- expect(evaljs("Routes.inboxPath")).not_to be_nil
262
- expect(evaljs("Routes.inboxUrl")).not_to be_nil
263
- expect(evaljs("Routes.inboxPath(1)")).to eq(routes.inbox_path(1))
264
- expect(evaljs("Routes.inboxUrl(1)")).to eq("http://localhost#{routes.inbox_path(1)}")
286
+ context "when configuring with default_url_options" do
287
+ context "when default host is not specified" do
288
+ it "raises an error" do
289
+ expect { JsRoutes.generate({ :url_links => true }) }.to raise_error RuntimeError
290
+ end
291
+ end
292
+
293
+ context "when only host option is specified" do
294
+ let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com"} } }
295
+
296
+ it "uses the specified host, defaults protocol to http, defaults port to 80 (leaving it blank)" do
297
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com#{routes.inbox_path(1)}")
298
+ end
299
+
300
+ it "does not override protocol when specified in route" do
301
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com#{routes.new_session_path}")
302
+ end
303
+
304
+ it "does not override host when specified in route" do
305
+ expect(evaljs("Routes.sso_url()")).to eq(routes.sso_url)
306
+ end
307
+
308
+ it "does not override port when specified in route" do
309
+ expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080#{routes.portals_path}")
310
+ end
311
+ end
312
+
313
+ context "when default host and protocol are specified" do
314
+ let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com", :protocol => "ftp"} } }
315
+
316
+ it "uses the specified protocol and host, defaults port to 80 (leaving it blank)" do
317
+ expect(evaljs("Routes.inbox_url(1)")).to eq("ftp://example.com#{routes.inbox_path(1)}")
318
+ end
319
+
320
+ it "does not override protocol when specified in route" do
321
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com#{routes.new_session_path}")
322
+ end
323
+
324
+ it "does not override host when host is specified in route" do
325
+ expect(evaljs("Routes.sso_url()")).to eq("ftp://sso.example.com#{routes.sso_path}")
326
+ end
327
+
328
+ it "does not override port when specified in route" do
329
+ expect(evaljs("Routes.portals_url()")).to eq("ftp://example.com:8080#{routes.portals_path}")
330
+ end
331
+ end
332
+
333
+ context "when default host and port are specified" do
334
+ let(:_options) { { :url_links => true, :default_url_options => {:host => "example.com", :port => 3000} } }
335
+
336
+ it "uses the specified host and port, defaults protocol to http" do
337
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com:3000#{routes.inbox_path(1)}")
338
+ end
339
+
340
+ it "does not override protocol when specified in route" do
341
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com:3000#{routes.new_session_path}")
342
+ end
343
+
344
+ it "does not override host, protocol, or port when host is specified in route" do
345
+ expect(evaljs("Routes.sso_url()")).to eq(routes.sso_url)
346
+ end
347
+
348
+ it "does not override port when specified in route" do
349
+ expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080#{routes.portals_path}")
350
+ end
351
+ end
352
+
353
+ context "with camel_case option" do
354
+ let(:_options) { { :camel_case => true, :url_links => true, :default_url_options => {:host => "example.com"} } }
355
+ it "should generate path and url links" do
356
+ expect(evaljs("Routes.inboxUrl(1)")).to eq("http://example.com#{routes.inbox_path(1)}")
357
+ expect(evaljs("Routes.newSessionUrl()")).to eq("https://example.com#{routes.new_session_path}")
358
+ expect(evaljs("Routes.ssoUrl()")).to eq(routes.sso_url)
359
+ expect(evaljs("Routes.portalsUrl()")).to eq("http://example.com:8080#{routes.portals_path}")
360
+ end
361
+ end
362
+
363
+ context "with prefix option" do
364
+ let(:_options) { { :prefix => "/api", :url_links => true, :default_url_options => {:host => 'example.com'} } }
365
+ it "should generate path and url links" do
366
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com/api#{routes.inbox_path(1)}")
367
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com/api#{routes.new_session_path}")
368
+ expect(evaljs("Routes.sso_url()")).to eq("http://sso.example.com/api#{routes.sso_path}")
369
+ expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080/api#{routes.portals_path}")
370
+ end
371
+ end
372
+
373
+ context "with compact option" do
374
+ let(:_options) { { :compact => true, :url_links => true, :default_url_options => {:host => 'example.com'} } }
375
+ it "does not affect url helpers" do
376
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://example.com#{routes.inbox_path(1)}")
377
+ expect(evaljs("Routes.new_session_url()")).to eq("https://example.com#{routes.new_session_path}")
378
+ expect(evaljs("Routes.sso_url()")).to eq(routes.sso_url)
379
+ expect(evaljs("Routes.portals_url()")).to eq("http://example.com:8080#{routes.portals_path}")
380
+ end
265
381
  end
266
382
  end
383
+ end
267
384
 
268
- context "with host and prefix" do
269
- let(:_options) { { :prefix => "/api", :url_links => "https://example.com" } }
270
- it "should generate path and url links" do
271
- expect(evaljs("Routes.inbox_path")).not_to be_nil
272
- expect(evaljs("Routes.inbox_url")).not_to be_nil
273
- expect(evaljs("Routes.inbox_path(1)")).to eq("/api#{routes.inbox_path(1)}")
274
- expect(evaljs("Routes.inbox_url(1)")).to eq("https://example.com/api#{routes.inbox_path(1)}")
385
+ describe "when the compact mode is enabled" do
386
+ let(:_options) { { :compact => true } }
387
+ it "removes _path suffix from path helpers" do
388
+ expect(evaljs("Routes.inbox_path")).to be_nil
389
+ expect(evaljs("Routes.inboxes()")).to eq(routes.inboxes_path())
390
+ expect(evaljs("Routes.inbox(2)")).to eq(routes.inbox_path(2))
391
+ end
392
+
393
+ context "with url_links option" do
394
+ context "with deprecated url_links config value" do
395
+ around(:each) do |example|
396
+ ActiveSupport::Deprecation.silence do
397
+ example.run
398
+ end
399
+ end
400
+
401
+ let(:_options) { { :compact => true, :url_links => "http://localhost" } }
402
+ it "should not strip urls" do
403
+ expect(evaljs("Routes.inbox(1)")).to eq(routes.inbox_path(1))
404
+ expect(evaljs("Routes.inbox_url(1)")).to eq("http://localhost#{routes.inbox_path(1)}")
405
+ end
275
406
  end
276
407
  end
277
408
  end
@@ -150,8 +150,8 @@ describe JsRoutes, "compatibility with Rails" do
150
150
  context "when jQuery is present" do
151
151
  before do
152
152
  evaljs("window.jQuery = {};")
153
- jscontext[:parameterize] = lambda {|object| _value.to_param}
154
- evaljs("window.jQuery.param = parameterize")
153
+ jscontext[:parameterizeFunc] = lambda {|object| _value.to_param}
154
+ evaljs("window.jQuery.param = parameterizeFunc")
155
155
  end
156
156
 
157
157
  shared_examples_for "serialization" do
@@ -177,6 +177,11 @@ describe JsRoutes, "compatibility with Rails" do
177
177
  let(:_value) do
178
178
  nil
179
179
  end
180
+
181
+ before do
182
+ pending("This test is invalid for nil/null and jruby #{JRUBY_VERSION}") if defined?(JRUBY_VERSION) && '1.7.13' == JRUBY_VERSION
183
+ end
184
+
180
185
  it_should_behave_like 'serialization'
181
186
  end
182
187
  end
@@ -199,11 +204,17 @@ describe JsRoutes, "compatibility with Rails" do
199
204
  expect(evaljs("Routes.thing_path(null, 5)")).to eq(routes.thing_path(nil, 5))
200
205
  end
201
206
 
207
+ # Fail for rails 4.2. Reason: https://github.com/rails/rails/issues/12178
202
208
  it "should treat undefined as non-given optional part" do
209
+ # TODO: remove me after release of rails 4.2.1 (change Appraisals to "~> 4.2.1")
210
+ pending("This test is invalid for rails 4.2. Reason: https://github.com/rails/rails/issues/12178") if [4, 2, 0] == [Rails::VERSION::MAJOR, Rails::VERSION::MINOR, Rails::VERSION::TINY]
203
211
  expect(evaljs("Routes.thing_path(5, {optional_id: undefined})")).to eq(routes.thing_path(5, :optional_id => nil))
204
212
  end
205
213
 
214
+ # Fail for rails 4.2. Reason: https://github.com/rails/rails/issues/12178
206
215
  it "should treat null as non-given optional part" do
216
+ # TODO: remove me after release of rails 4.2.1 (change Appraisals to "~> 4.2.1")
217
+ pending("This test is invalid for rails 4.2. Reason: https://github.com/rails/rails/issues/12178") if [4, 2, 0] == [Rails::VERSION::MAJOR, Rails::VERSION::MINOR, Rails::VERSION::TINY]
207
218
  expect(evaljs("Routes.thing_path(5, {optional_id: null})")).to eq(routes.thing_path(5, :optional_id => nil))
208
219
  end
209
220
  end
@@ -291,4 +302,32 @@ describe JsRoutes, "compatibility with Rails" do
291
302
  end
292
303
  end
293
304
  end
305
+
306
+ context "when specs" do
307
+ it "should show inbox spec" do
308
+ expect(evaljs("Routes.inbox_path.toString()")).to eq('/inboxes/:id(.:format)')
309
+ end
310
+
311
+ it "should show inbox spec convert to string" do
312
+ expect(evaljs("'' + Routes.inbox_path")).to eq('/inboxes/:id(.:format)')
313
+ end
314
+
315
+ it "should show inbox message spec" do
316
+ expect(evaljs("Routes.inbox_message_path.toString()")).to eq('/inboxes/:inbox_id/messages/:id(.:format)')
317
+ end
318
+
319
+ it "should show inbox message spec convert to string" do
320
+ expect(evaljs("'' + Routes.inbox_message_path")).to eq('/inboxes/:inbox_id/messages/:id(.:format)')
321
+ end
322
+ end
323
+
324
+ context "when params" do
325
+ it "should show inbox spec" do
326
+ expect(evaljs("Routes.inbox_path.required_params").to_a).to eq(["id"])
327
+ end
328
+
329
+ it "should show inbox message spec" do
330
+ expect(evaljs("Routes.inbox_message_path.required_params").to_a).to eq(["inbox_id", "id"])
331
+ end
332
+ end
294
333
  end
data/spec/spec_helper.rb CHANGED
@@ -108,6 +108,12 @@ def draw_routes
108
108
 
109
109
  get '/привет' => "foo#foo", :as => :hello
110
110
  get '(/o/:organization)/search/:q' => "foo#foo", as: :search
111
+
112
+ resources :sessions, :only => [:new, :create, :destroy], :protocol => 'https'
113
+
114
+ get '/' => 'sso#login', host: 'sso.example.com', as: :sso
115
+
116
+ resources :portals, :port => 8080
111
117
  end
112
118
 
113
119
  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: 0.9.9
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-15 00:00:00.000000000 Z
11
+ date: 2014-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: 0.12.1
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: '0'
124
+ version: 0.12.1
125
125
  description: Generates javascript file that defines all Rails named routes as javascript
126
126
  helpers
127
127
  email: agresso@gmail.com
@@ -145,6 +145,7 @@ files:
145
145
  - gemfiles/rails32.gemfile
146
146
  - gemfiles/rails40.gemfile
147
147
  - gemfiles/rails41.gemfile
148
+ - gemfiles/rails42.gemfile
148
149
  - js-routes.gemspec
149
150
  - lib/js-routes.rb
150
151
  - lib/js_routes.rb