js-routes 1.2.6 → 1.2.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 547d8dc7d5bb050616b71bb10d414aeddb9fcb6a
4
- data.tar.gz: 35c567ffb95027338b01162db20daa8e312adc32
3
+ metadata.gz: 673824b92c317dcd831dba31a576e63c057569fc
4
+ data.tar.gz: 3bde0b614cb515193e7b2166adeb6be99e8d0bf8
5
5
  SHA512:
6
- metadata.gz: bece2fc2356916ecf50103bdbd4fcff15d2dbaa024d7bfdc24e4f521f6ca513eb47a1c4938370dd04bdcbe4563b80fe0b3ff5506f2b7507ce6a83688557ad03c
7
- data.tar.gz: a89e298a614595691c1a5a0f9e91fed8ee6a934645a6e7a26f6785ba970499dd1de5be8a140a4b90b1dcf3dc3e231614379da4e7e4b5483e0f76a713c0c7ec52
6
+ metadata.gz: 999966b93d59c45104dfd2a24d9ebd7d891df7924430cb7fb2d4a1438e84e229a901043cf4b27ab1da279f74ae33a2c09b1b6c68005a72d33be76ac8430ea9b0
7
+ data.tar.gz: a2ece0a5892d4762c26a5a74f130806c90d82a1c04ec2f528b42f2e3f15172033fa1073b99852bb9b897e21af4d3964e76133128448e23f84372121b6b5801d2
data/.travis.yml CHANGED
@@ -1,11 +1,13 @@
1
1
  language: ruby
2
2
  cache: bundler
3
3
 
4
+ before_install:
5
+ - gem install bundler # need for jruby and ruby-head
6
+
4
7
  rvm:
5
- - 1.9.3
6
8
  - 2.0
7
9
  - 2.1
8
- - 2.2
10
+ - 2.2.5
9
11
  - 2.3.1
10
12
  - jruby-19mode
11
13
  - ruby-head
@@ -19,6 +21,8 @@ gemfile:
19
21
  - gemfiles/rails41_sprockets3.gemfile
20
22
  - gemfiles/rails42.gemfile
21
23
  - gemfiles/rails42_sprockets3.gemfile
24
+ - gemfiles/rails50.gemfile
25
+ - gemfiles/rails50_sprockets3.gemfile
22
26
 
23
27
  sudo: false
24
28
 
@@ -35,3 +39,13 @@ matrix:
35
39
  - rvm: jruby-19mode
36
40
  - rvm: ruby-head
37
41
  - rvm: jruby-head
42
+ - gemfile: gemfiles/rails50.gemfile
43
+ exclude:
44
+ - rvm: 2.0
45
+ gemfile: gemfiles/rails50.gemfile
46
+ - rvm: 2.0
47
+ gemfile: gemfiles/rails50_sprockets3.gemfile
48
+ - rvm: 2.1
49
+ gemfile: gemfiles/rails50.gemfile
50
+ - rvm: 2.1
51
+ gemfile: gemfiles/rails50_sprockets3.gemfile
data/Appraisals CHANGED
@@ -3,7 +3,7 @@ appraise "rails32" do
3
3
  gem 'tzinfo'
4
4
  end
5
5
 
6
- {rails40: '4.0.5', rails41: '4.1.1', rails42: '4.2.1'}.each do |rails, version|
6
+ {rails40: '4.0.5', rails41: '4.1.1', rails42: '4.2.1', rails50: '5.0.0'}.each do |rails, version|
7
7
  appraise "#{rails}" do
8
8
  gem "railties", "~> #{version}"
9
9
  gem "sprockets", "< 3"
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## master
2
2
 
3
+ ## v1.2.7
4
+
5
+ * Drop support 1.9.3
6
+ * Add helper for indexOf, if no native implementation in JS engine
7
+ * Add sprockets3 compatibility
8
+ * Bugfix domain defaults to path #197
9
+
3
10
  ## v1.2.6
4
11
 
5
12
  * Use default prefix from `Rails.application.config.relative_url_root` #186
@@ -1,3 +1,2 @@
1
1
  <%# encoding: UTF-8 %>
2
- <% depend_on "#{Rails.root.join 'config', 'routes.rb'}" if JsRoutes::SPROCKETS3 %>
3
2
  <%= JsRoutes.assert_usable_configuration! && JsRoutes.generate %>
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "railties", "~> 5.0.0"
6
+ gem "sprockets", "< 3"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "railties", "~> 5.0.0"
6
+ gem "sprockets", "~> 3.0"
7
+
8
+ gemspec :path => "../"
data/js-routes.gemspec CHANGED
@@ -29,9 +29,7 @@ Gem::Specification.new do |s|
29
29
  if defined?(JRUBY_VERSION)
30
30
  s.add_development_dependency(%q<therubyrhino>, [">= 2.0.4"])
31
31
  else
32
- if RUBY_VERSION >= "2.0.0"
33
- s.add_development_dependency(%q<byebug>)
34
- end
32
+ s.add_development_dependency(%q<byebug>)
35
33
  s.add_development_dependency(%q<therubyracer>, [">= 0.12.1"])
36
34
  end
37
35
  end
data/lib/js_routes.rb CHANGED
@@ -36,6 +36,7 @@ class JsRoutes
36
36
 
37
37
  LAST_OPTIONS_KEY = "options".freeze
38
38
  FILTERED_DEFAULT_PARTS = [:controller, :action, :subdomain]
39
+ URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain]
39
40
 
40
41
  class Options < Struct.new(*DEFAULTS.keys)
41
42
  def to_hash
@@ -151,19 +152,22 @@ class JsRoutes
151
152
 
152
153
  def js_routes
153
154
  js_routes = Rails.application.routes.named_routes.to_a.sort_by(&:first).flat_map do |_, route|
154
- rails_engine_app = get_app_from_route(route)
155
- if rails_engine_app.respond_to?(:superclass) && rails_engine_app.superclass == Rails::Engine && !route.path.anchored
156
- rails_engine_app.routes.named_routes.map do |_, engine_route|
157
- build_route_if_match(engine_route, route)
158
- end
159
- else
160
- build_route_if_match(route)
161
- end
155
+ [build_route_if_match(route)] + mounted_app_routes(route)
162
156
  end.compact
163
-
164
157
  "{\n" + js_routes.join(",\n") + "}\n"
165
158
  end
166
159
 
160
+ def mounted_app_routes(route)
161
+ rails_engine_app = get_app_from_route(route)
162
+ if rails_engine_app.respond_to?(:superclass) && rails_engine_app.superclass == Rails::Engine && !route.path.anchored
163
+ rails_engine_app.routes.named_routes.map do |_, engine_route|
164
+ build_route_if_match(engine_route, route)
165
+ end
166
+ else
167
+ []
168
+ end
169
+ end
170
+
167
171
  def get_app_from_route(route)
168
172
  # rails engine in Rails 4.2 use additional ActionDispatch::Routing::Mapper::Constraints, which contain app
169
173
  if route.app.respond_to?(:app) && route.app.respond_to?(:constraints)
@@ -204,8 +208,8 @@ class JsRoutes
204
208
  def route_js_arguments(route, parent_spec)
205
209
  required_parts = route.required_parts.clone
206
210
  optional_parts = route.parts - required_parts
207
- default_parts = route.defaults.reject do |part, _|
208
- FILTERED_DEFAULT_PARTS.include?(part)
211
+ default_parts = route.defaults.select do |part, _|
212
+ FILTERED_DEFAULT_PARTS.exclude?(part) && URL_OPTIONS.include?(part) || required_parts.include?(part)
209
213
  end
210
214
  [
211
215
  required_parts, optional_parts, serialize(route.path.spec, parent_spec), default_parts
@@ -4,15 +4,27 @@ class JsRoutes
4
4
  SPROCKETS3 = Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('3.0.0')
5
5
  class Engine < ::Rails::Engine
6
6
 
7
- initializer 'js-routes.dependent_on_routes', after: "sprockets.environment" do
7
+ if SPROCKETS3
8
+ initializer 'js-routes.dependent_on_routes', after: :engines_blank_point, before: :finisher_hook do
9
+ Rails.application.config.assets.configure do |config|
10
+ routes = Rails.root.join('config', 'routes.rb').to_s
11
+ config.register_preprocessor 'application/javascript', :'js-routes_dependent_on_routes' do |ctx,data|
12
+ ctx.depend_on(routes) if ctx.logical_path == 'js-routes'
13
+ data
14
+ end
15
+ end
16
+ end
17
+ else
18
+ initializer 'js-routes.dependent_on_routes', after: "sprockets.environment" do
8
19
 
9
- if Rails.application.assets.respond_to?(:register_preprocessor)
10
- routes = Rails.root.join('config', 'routes.rb').to_s
11
- Rails.application.assets.register_preprocessor 'application/javascript', :'js-routes_dependent_on_routes' do |ctx,data|
12
- ctx.depend_on(routes) if ctx.logical_path == 'js-routes'
13
- data
20
+ if Rails.application.assets.respond_to?(:register_preprocessor)
21
+ routes = Rails.root.join('config', 'routes.rb').to_s
22
+ Rails.application.assets.register_preprocessor 'application/javascript', :'js-routes_dependent_on_routes' do |ctx,data|
23
+ ctx.depend_on(routes) if ctx.logical_path == 'js-routes'
24
+ data
25
+ end
14
26
  end
15
27
  end
16
- end unless SPROCKETS3
28
+ end
17
29
  end
18
30
  end
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "1.2.6"
2
+ VERSION = "1.2.7"
3
3
  end
data/lib/routes.js CHANGED
@@ -173,7 +173,7 @@ Based on Rails routes of APP_CLASS
173
173
  for (key in options) {
174
174
  if (!hasProp.call(options, key)) continue;
175
175
  value = options[key];
176
- if (ReservedOptions.indexOf(key) >= 0) {
176
+ if (this.indexOf(ReservedOptions, key) >= 0) {
177
177
  result[key] = value;
178
178
  } else {
179
179
  url_parameters[key] = value;
@@ -245,7 +245,7 @@ Based on Rails routes of APP_CLASS
245
245
  }
246
246
  },
247
247
  is_optional_node: function(node) {
248
- return node === NodeTypes.STAR || node === NodeTypes.SYMBOL || node === NodeTypes.CAT;
248
+ return this.indexOf([NodeTypes.STAR, NodeTypes.SYMBOL, NodeTypes.CAT], node) >= 0;
249
249
  },
250
250
  build_path_spec: function(route, wildcard) {
251
251
  var left, right, type;
@@ -375,6 +375,24 @@ Based on Rails routes of APP_CLASS
375
375
  } else {
376
376
  return typeof obj;
377
377
  }
378
+ },
379
+ indexOf: function(array, element) {
380
+ if (Array.prototype.indexOf) {
381
+ return array.indexOf(element);
382
+ } else {
383
+ return this.indexOfImplementation(array, element);
384
+ }
385
+ },
386
+ indexOfImplementation: function(array, element) {
387
+ var el, i, j, len, result;
388
+ result = -1;
389
+ for (i = j = 0, len = array.length; j < len; i = ++j) {
390
+ el = array[i];
391
+ if (el === element) {
392
+ result = i;
393
+ }
394
+ }
395
+ return result;
378
396
  }
379
397
  };
380
398
 
data/lib/routes.js.coffee CHANGED
@@ -109,7 +109,7 @@ Utils =
109
109
  url_parameters = {}
110
110
  result['url_parameters'] = url_parameters
111
111
  for own key, value of options
112
- if ReservedOptions.indexOf(key) >= 0
112
+ if @indexOf(ReservedOptions, key) >= 0
113
113
  result[key] = value
114
114
  else
115
115
  url_parameters[key] = value
@@ -183,8 +183,7 @@ Utils =
183
183
  throw new Error("Unknown Rails node type")
184
184
 
185
185
 
186
- is_optional_node: (node) ->
187
- node == NodeTypes.STAR or node == NodeTypes.SYMBOL or node == NodeTypes.CAT
186
+ is_optional_node: (node) -> @indexOf([NodeTypes.STAR, NodeTypes.SYMBOL, NodeTypes.CAT], node) >= 0
188
187
 
189
188
  #
190
189
  # This method build spec for route
@@ -311,6 +310,13 @@ Utils =
311
310
  return "#{obj}" unless obj?
312
311
  (if typeof obj is "object" or typeof obj is "function" then @_classToType()[Object::toString.call(obj)] or "object" else typeof obj)
313
312
 
313
+ # indexOf helper
314
+ indexOf: (array, element) -> if Array::indexOf then array.indexOf(element) else @indexOfImplementation(array, element)
315
+ indexOfImplementation: (array, element) ->
316
+ result = -1
317
+ (result = i for el, i in array when el is element)
318
+ result
319
+
314
320
  # globalJsObject
315
321
  createGlobalJsRoutesObject = ->
316
322
  # namespace function, private
@@ -75,10 +75,6 @@ describe JsRoutes do
75
75
  end
76
76
 
77
77
  describe "compiled javascript asset" do
78
- if JsRoutes::SPROCKETS3
79
- let(:routes_path){ "#{Rails.root.join 'config', 'routes.rb'}" }
80
- before { expect(self).to receive(:depend_on).with routes_path }
81
- end
82
78
  subject { ERB.new(File.read("app/assets/javascripts/js-routes.js.erb")).result(binding) }
83
79
  it "should have js routes code" do
84
80
  is_expected.to include("inbox_message_path: Utils.route([\"inbox_id\",\"id\"]")
@@ -421,14 +421,7 @@ describe JsRoutes, "options" do
421
421
  end
422
422
 
423
423
  before do
424
- jscontext['window'] = {
425
- :location => {
426
- :protocol => current_protocol,
427
- :hostname => current_hostname,
428
- :port => current_port,
429
- :host => current_host
430
- }
431
- }
424
+ jscontext.eval("window = {'location': {'protocol': '#{current_protocol}', 'hostname': '#{current_hostname}', 'port': '#{current_port}', 'host': '#{current_host}'}}")
432
425
  end
433
426
 
434
427
  context "without specifying a default host" do
@@ -67,12 +67,19 @@ describe JsRoutes, "compatibility with Rails" do
67
67
  end
68
68
 
69
69
  it 'should support route default subdomain' do
70
+ # root inside namespace is broken
71
+ # https://github.com/rails/rails/pull/23235
72
+ pending if Rails.version == '5.0.0'
70
73
  expect(evaljs("Routes.backend_root_path()")).to eq(routes.backend_root_path)
71
74
  end
72
75
 
73
76
  it "should support default format override" do
74
77
  expect(evaljs("Routes.api_purchases_path({format: 'xml'})")).to eq(routes.api_purchases_path(format: 'xml'))
75
78
  end
79
+
80
+ it "doesn't apply defaults to path" do
81
+ expect(evaljs("Routes.with_defaults_path()")).to eq(routes.with_defaults_path)
82
+ end
76
83
  end
77
84
 
78
85
  context "with rails engines" do
@@ -80,6 +87,10 @@ describe JsRoutes, "compatibility with Rails" do
80
87
  expect(evaljs("Routes.blog_app_posts_path()")).to eq(blog_routes.posts_path())
81
88
  end
82
89
 
90
+ it "should support root route" do
91
+ expect(evaljs("Routes.blog_app_path()")).to eq(routes.blog_app_path())
92
+ end
93
+
83
94
  it "should support route with parameters" do
84
95
  expect(evaljs("Routes.blog_app_post_path(1)")).to eq(blog_routes.post_path(1))
85
96
  end
data/spec/spec_helper.rb CHANGED
@@ -14,7 +14,7 @@ if defined?(JRUBY_VERSION)
14
14
  require 'rhino'
15
15
  JS_LIB_CLASS = Rhino
16
16
  else
17
- require "v8"
17
+ require 'v8'
18
18
  JS_LIB_CLASS = V8
19
19
  end
20
20
 
@@ -124,6 +124,8 @@ def draw_routes
124
124
 
125
125
  resources :portals, :port => 8080
126
126
 
127
+ get '/with_defaults' => 'foo#foo', defaults: { bar: 'tested', format: :json }, format: :true
128
+
127
129
  namespace :api, format: true, defaults: {format: 'json'} do
128
130
  get "/purchases" => "purchases#index"
129
131
  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: 1.2.6
4
+ version: 1.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-02 00:00:00.000000000 Z
11
+ date: 2016-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -148,6 +148,8 @@ files:
148
148
  - gemfiles/rails41_sprockets3.gemfile
149
149
  - gemfiles/rails42.gemfile
150
150
  - gemfiles/rails42_sprockets3.gemfile
151
+ - gemfiles/rails50.gemfile
152
+ - gemfiles/rails50_sprockets3.gemfile
151
153
  - js-routes.gemspec
152
154
  - lib/js-routes.rb
153
155
  - lib/js_routes.rb