js-routes 1.0.1 → 1.2.9
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 +4 -4
- data/.travis.yml +19 -4
- data/Appraisals +2 -2
- data/CHANGELOG.md +70 -0
- data/Rakefile +14 -1
- data/Readme.md +23 -11
- data/gemfiles/rails42.gemfile +1 -1
- data/gemfiles/rails42_sprockets3.gemfile +1 -1
- data/gemfiles/rails50.gemfile +8 -0
- data/gemfiles/rails50_sprockets3.gemfile +8 -0
- data/js-routes.gemspec +2 -2
- data/lib/js_routes/engine.rb +62 -14
- data/lib/js_routes/version.rb +1 -1
- data/lib/js_routes.rb +76 -60
- data/lib/routes.js +163 -100
- data/lib/routes.js.coffee +113 -54
- data/spec/js_routes/default_serializer_spec.rb +13 -0
- data/spec/js_routes/generated_javascript_spec.rb +12 -0
- data/spec/js_routes/options_spec.rb +90 -20
- data/spec/js_routes/rails_routes_compatibility_spec.rb +46 -49
- data/spec/js_routes/zzz_last_post_rails_init_spec.rb +3 -9
- data/spec/spec_helper.rb +31 -57
- data/spec/support/routes.rb +71 -0
- metadata +14 -12
- data/Guardfile +0 -3
- data/temp +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33fffcaa3c5d7614a35673b8a53ea0f06030dbed
|
4
|
+
data.tar.gz: b8c5ffa5eb34114667b2d8e3f53dec2809816f2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 989c63812f7821d21fc94b0bd42f43e87be6a933a2f934733a8f30f3b575f566cb43619a3ca64b49e39ec1dc91ea633ab5337f5be133804aa995756e252cc27f
|
7
|
+
data.tar.gz: 880139c994a2f813b4e0c65d16f281626252462010a08f68e4ef234f3743cab1ae141041fb3cb60f4bdc04fe5d2803dbb22694cc08cf1604e5da692bc3f6e6ef
|
data/.travis.yml
CHANGED
@@ -1,11 +1,14 @@
|
|
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
|
11
|
+
- 2.3.1
|
9
12
|
- jruby-19mode
|
10
13
|
- ruby-head
|
11
14
|
- jruby-head
|
@@ -13,11 +16,13 @@ rvm:
|
|
13
16
|
gemfile:
|
14
17
|
- gemfiles/rails32.gemfile
|
15
18
|
- gemfiles/rails40.gemfile
|
16
|
-
- gemfiles/rails41.gemfile
|
17
|
-
- gemfiles/rails42.gemfile
|
18
19
|
- gemfiles/rails40_sprockets3.gemfile
|
20
|
+
- gemfiles/rails41.gemfile
|
19
21
|
- gemfiles/rails41_sprockets3.gemfile
|
22
|
+
- gemfiles/rails42.gemfile
|
20
23
|
- gemfiles/rails42_sprockets3.gemfile
|
24
|
+
- gemfiles/rails50.gemfile
|
25
|
+
- gemfiles/rails50_sprockets3.gemfile
|
21
26
|
|
22
27
|
sudo: false
|
23
28
|
|
@@ -34,3 +39,13 @@ matrix:
|
|
34
39
|
- rvm: jruby-19mode
|
35
40
|
- rvm: ruby-head
|
36
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.0'}.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"
|
@@ -13,4 +13,4 @@ end
|
|
13
13
|
gem "railties", "~> #{version}"
|
14
14
|
gem "sprockets", "~> 3.0"
|
15
15
|
end
|
16
|
-
end
|
16
|
+
end
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,75 @@
|
|
1
1
|
## master
|
2
2
|
|
3
|
+
## v1.2.9
|
4
|
+
|
5
|
+
* Fixed deprecation varning on Sprockets 3.7
|
6
|
+
|
7
|
+
## v1.2.8
|
8
|
+
|
9
|
+
* Bugfix warning on Sprockets 4.0 #202
|
10
|
+
|
11
|
+
## v1.2.7
|
12
|
+
|
13
|
+
* Drop support 1.9.3
|
14
|
+
* Add helper for indexOf, if no native implementation in JS engine
|
15
|
+
* Add sprockets3 compatibility
|
16
|
+
* Bugfix domain defaults to path #197
|
17
|
+
|
18
|
+
## v1.2.6
|
19
|
+
|
20
|
+
* Use default prefix from `Rails.application.config.relative_url_root` #186
|
21
|
+
* Bugfix route globbing with optional fragments bug #191
|
22
|
+
|
23
|
+
## v1.2.5
|
24
|
+
|
25
|
+
* Bugfix subdomain default parameter in routes #184
|
26
|
+
* Bugfix infinite recursion in some specific route sets #183
|
27
|
+
|
28
|
+
## v1.2.4
|
29
|
+
|
30
|
+
* Additional bugfixes to support all versions of Sprockets: 2.x and 3.x
|
31
|
+
|
32
|
+
## v1.2.3
|
33
|
+
|
34
|
+
* Sprockets ~= 3.0 support
|
35
|
+
|
36
|
+
## v1.2.2
|
37
|
+
|
38
|
+
* Sprockets ~= 3.0 support
|
39
|
+
* Support default parameters specified in route.rb file
|
40
|
+
|
41
|
+
## v1.2.1
|
42
|
+
|
43
|
+
* Fixes for Rails 5
|
44
|
+
|
45
|
+
## v1.2.0
|
46
|
+
|
47
|
+
* Support host, port and protocol inline parameters
|
48
|
+
* Support host, port and protocol parameters given to a route explicitly
|
49
|
+
* Remove all incompatibilities between actiondispatch and js-routes in handling route URLs
|
50
|
+
|
51
|
+
## v1.1.2
|
52
|
+
|
53
|
+
* Bugfix support nested object null parameters #164
|
54
|
+
* Bugfix support for nested optional parameters #162 #163
|
55
|
+
|
56
|
+
## v1.1.1
|
57
|
+
|
58
|
+
* Bugfix regression in serialisation on blank strings caused by [#155](https://github.com/railsware/js-routes/pull/155/files)
|
59
|
+
|
60
|
+
## v1.1.0
|
61
|
+
|
62
|
+
* Ensure routes are loaded, prior to generating them [#148](https://github.com/railsware/js-routes/pull/148)
|
63
|
+
* Use `flat_map` rather than `map{...}.flatten` [#149](https://github.com/railsware/js-routes/pull/149)
|
64
|
+
* URL escape routes.rb url to fix bad URI(is not URI?) error [#150](https://github.com/railsware/js-routes/pull/150)
|
65
|
+
* Fix for rails 5 - test rails-edge on travis allowing failure [#151](https://github.com/railsware/js-routes/pull/151)
|
66
|
+
* Adds `serializer` option [#155](https://github.com/railsware/js-routes/pull/155/files)
|
67
|
+
|
68
|
+
## v1.0.1
|
69
|
+
|
70
|
+
* Support sprockets-3
|
71
|
+
* Performance optimization of include/exclude options
|
72
|
+
|
3
73
|
## v1.0.0
|
4
74
|
|
5
75
|
* Add the compact mode [#125](https://github.com/railsware/js-routes/pull/125)
|
data/Rakefile
CHANGED
@@ -12,9 +12,22 @@ require 'bundler/gem_tasks'
|
|
12
12
|
require 'rspec/core'
|
13
13
|
require 'rspec/core/rake_task'
|
14
14
|
require 'appraisal'
|
15
|
+
load "rails/tasks/routes.rake"
|
15
16
|
|
16
17
|
RSpec::Core::RakeTask.new(:spec)
|
17
18
|
|
18
19
|
task :test_all => :appraisal # test all rails
|
19
20
|
|
20
|
-
task :default => :spec
|
21
|
+
task :default => :spec
|
22
|
+
|
23
|
+
|
24
|
+
namespace :spec do
|
25
|
+
task :routes do
|
26
|
+
require './spec/spec_helper'
|
27
|
+
require 'action_dispatch/routing/inspector'
|
28
|
+
draw_routes
|
29
|
+
all_routes = Rails.application.routes.routes
|
30
|
+
inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
|
31
|
+
puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
|
32
|
+
end
|
33
|
+
end
|
data/Readme.md
CHANGED
@@ -11,14 +11,12 @@ Your Rails Gemfile:
|
|
11
11
|
gem "js-routes"
|
12
12
|
```
|
13
13
|
|
14
|
-
### Basic Setup
|
14
|
+
### Basic Setup
|
15
15
|
|
16
16
|
Require js routes file in `application.js` or other bundle
|
17
17
|
|
18
18
|
``` js
|
19
|
-
|
20
|
-
= require js-routes
|
21
|
-
*/
|
19
|
+
//= require js-routes
|
22
20
|
```
|
23
21
|
|
24
22
|
Also in order to flush asset pipeline cache sometimes you might need to run:
|
@@ -45,8 +43,7 @@ Available options:
|
|
45
43
|
|
46
44
|
* `default_url_options` - default parameters used when generating URLs
|
47
45
|
* Note that only specific options are supported at this time.
|
48
|
-
*
|
49
|
-
* Example: {:format => "json", :trailing_slash => true, :protocol => "https", :host => "example.com", :port => 3000}
|
46
|
+
* Example: {:format => "json", :trailing\_slash => true, :protocol => "https", :host => "example.com", :port => 3000}
|
50
47
|
* Default: {}
|
51
48
|
* `exclude` - Array of regexps to exclude from js routes.
|
52
49
|
* Default: []
|
@@ -59,16 +56,20 @@ Available options:
|
|
59
56
|
* Default: `Routes`
|
60
57
|
* `prefix` - String representing a url path to prepend to all paths.
|
61
58
|
* Example: `http://yourdomain.com`. This will cause route helpers to generate full path only.
|
62
|
-
* Default:
|
59
|
+
* Default: Rails.application.config.relative_url_root
|
63
60
|
* `camel_case` (version >= 0.8.8) - Generate camel case route names.
|
64
61
|
* Default: false
|
65
62
|
* `url_links` (version >= 0.8.9) - Generate `*_url` helpers (in addition to the default `*_path` helpers).
|
66
63
|
* Example: true
|
67
64
|
* Default: false
|
68
|
-
*
|
65
|
+
* Note: generated URLs will first use the protocol, host, and port options specified in the route definition. Otherwise, the URL will be based on the option specified in the `default_url_options` config. If no default option has been set, then the URL will fallback to the current URL based on `window.location`.
|
69
66
|
* `compact` (version > 0.9.9) - Remove `_path` suffix in path routes(`*_url` routes stay untouched if they were enabled)
|
70
67
|
* Default: false
|
71
68
|
* Sample route call when option is set to true: Routes.users() => `/users`
|
69
|
+
* `serializer` (version >= 1.1.0) - Puts a JS function here that serializes a Javascript Hash object into URL paramters: `{a: 1, b: 2} => "a=1&b=2"`.
|
70
|
+
* Default: `nil`. Uses built-in serializer
|
71
|
+
* Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
|
72
|
+
* Example: `MyApp.custom_serialize` - use completely custom serializer of your application.
|
72
73
|
|
73
74
|
### Very Advanced Setup
|
74
75
|
|
@@ -91,7 +92,7 @@ If your application has an `admin` and an `application` namespace for example:
|
|
91
92
|
#= require application/routes
|
92
93
|
```
|
93
94
|
|
94
|
-
In order to generate the routes to a string:
|
95
|
+
In order to generate the routes JS code to a string:
|
95
96
|
|
96
97
|
```ruby
|
97
98
|
routes_js = JsRoutes.generate(options)
|
@@ -106,6 +107,15 @@ JsRoutes.generate!("#{path}/adm_routes.js", :namespace => "AdmRoutes", :include
|
|
106
107
|
JsRoutes.generate!("#{path}/api_routes.js", :namespace => "ApiRoutes", :include => /^api_/, :default_url_options => {:format => "json"})
|
107
108
|
```
|
108
109
|
|
110
|
+
### Rails relative URL root
|
111
|
+
|
112
|
+
If you've installed your application in a sub-path or sub-URI of your server instead of at the root, you need to set the `RAILS_RELATIVE_URL_ROOT` environment variable to the correct path prefix for your application when you precompile assets. Eg., if your application's base URL is "https://appl.example.com/Application1", the command to precompile assets would be:
|
113
|
+
```
|
114
|
+
RAILS_RELATIVE_URL_ROOT=/Application1 RAILS_ENV=production bundle exec rake assets:precompile
|
115
|
+
```
|
116
|
+
The environment variable is only needed for precompilation of assets, at any other time (eg. when assets are compiled on-the-fly as in the development environment) Rails will set the relative URL root correctly on it's own.
|
117
|
+
|
118
|
+
|
109
119
|
## Usage
|
110
120
|
|
111
121
|
Configuration above will create a nice javascript file with `Routes` object that has all the rails routes available:
|
@@ -114,6 +124,7 @@ Configuration above will create a nice javascript file with `Routes` object that
|
|
114
124
|
Routes.users_path() // => "/users"
|
115
125
|
Routes.user_path(1) // => "/users/1"
|
116
126
|
Routes.user_path(1, {format: 'json'}) // => "/users/1.json"
|
127
|
+
Routes.user_path(1, {anchor: 'profile'}) // => "/users/1#profile"
|
117
128
|
Routes.new_user_project_path(1, {format: 'json'}) // => "/users/1/projects/new.json"
|
118
129
|
Routes.user_project_path(1,2, {q: 'hello', custom: true}) // => "/users/1/projects/2?q=hello&custom=true"
|
119
130
|
Routes.user_project_path(1,2, {hello: ['world', 'mars']}) // => "/users/1/projects/2?hello%5B%5D=world&hello%5B%5D=mars"
|
@@ -178,7 +189,7 @@ Heroku environment has a specific problems with setup. It is impossible to use a
|
|
178
189
|
For example create routes.js.erb in assets folder with needed content:
|
179
190
|
|
180
191
|
``` erb
|
181
|
-
<%= JsRoutes.generate(
|
192
|
+
<%= JsRoutes.generate(options) %>
|
182
193
|
```
|
183
194
|
|
184
195
|
This should just work.
|
@@ -188,8 +199,9 @@ This should just work.
|
|
188
199
|
There are some alternatives available. Most of them has only basic feature and don't reach the level of quality I accept.
|
189
200
|
Advantages of this one are:
|
190
201
|
|
191
|
-
*
|
202
|
+
* Rails 3-5 support
|
192
203
|
* Rich options set
|
204
|
+
* Full rails compatibility
|
193
205
|
* Support Rails `#to_param` convention for seo optimized paths
|
194
206
|
* Well tested
|
195
207
|
|
data/gemfiles/rails42.gemfile
CHANGED
data/js-routes.gemspec
CHANGED
@@ -24,12 +24,12 @@ Gem::Specification.new do |s|
|
|
24
24
|
s.add_runtime_dependency(%q<sprockets-rails>)
|
25
25
|
s.add_development_dependency(%q<rspec>, [">= 3.0.0"])
|
26
26
|
s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
|
27
|
-
s.add_development_dependency(%q<
|
28
|
-
s.add_development_dependency(%q<guard-coffeescript>, [">= 0"])
|
27
|
+
s.add_development_dependency(%q<coffee-script>, [">= 0"])
|
29
28
|
s.add_development_dependency(%q<appraisal>, [">= 0.5.2"])
|
30
29
|
if defined?(JRUBY_VERSION)
|
31
30
|
s.add_development_dependency(%q<therubyrhino>, [">= 2.0.4"])
|
32
31
|
else
|
32
|
+
s.add_development_dependency(%q<byebug>)
|
33
33
|
s.add_development_dependency(%q<therubyracer>, [">= 0.12.1"])
|
34
34
|
end
|
35
35
|
end
|
data/lib/js_routes/engine.rb
CHANGED
@@ -1,21 +1,69 @@
|
|
1
|
-
class
|
2
|
-
|
3
|
-
|
1
|
+
class JsRoutesSprocketsExtension
|
2
|
+
def initialize(filename, &block)
|
3
|
+
@filename = filename
|
4
|
+
@source = block.call
|
5
|
+
end
|
6
|
+
|
7
|
+
def render(context, empty_hash_wtf)
|
8
|
+
self.class.run(@filename, @source, context)
|
9
|
+
end
|
4
10
|
|
5
|
-
|
11
|
+
def self.run(filename, source, context)
|
12
|
+
if context.logical_path == 'js-routes'
|
6
13
|
routes = Rails.root.join('config', 'routes.rb').to_s
|
14
|
+
context.depend_on(routes)
|
15
|
+
end
|
16
|
+
source
|
17
|
+
end
|
7
18
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
end
|
19
|
+
def self.call(input)
|
20
|
+
filename = input[:filename]
|
21
|
+
source = input[:data]
|
22
|
+
context = input[:environment].context_class.new(input)
|
14
23
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
24
|
+
result = run(filename, source, context)
|
25
|
+
context.metadata.merge(data: result)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
class Engine < ::Rails::Engine
|
31
|
+
require 'sprockets/version'
|
32
|
+
v2 = Gem::Dependency.new('', ' ~> 2')
|
33
|
+
v3 = Gem::Dependency.new('', ' >= 3' ,' < 3.7')
|
34
|
+
v37 = Gem::Dependency.new('', ' >= 3.7')
|
35
|
+
sprockets_version = Gem::Version.new(::Sprockets::VERSION).release
|
36
|
+
initializer_args = case sprockets_version
|
37
|
+
when -> (v) { v2.match?('', v) }
|
38
|
+
{ after: "sprockets.environment" }
|
39
|
+
when -> (v) { v3.match?('', v) || v37.match?('', v) }
|
40
|
+
{ after: :engines_blank_point, before: :finisher_hook }
|
41
|
+
else
|
42
|
+
raise StandardError, "Sprockets version #{sprockets_version} is not supported"
|
43
|
+
end
|
44
|
+
|
45
|
+
initializer 'js-routes.dependent_on_routes', initializer_args do
|
46
|
+
case sprockets_version
|
47
|
+
when -> (v) { v2.match?('', v) }
|
48
|
+
if Rails.application.assets.respond_to?(:register_preprocessor)
|
49
|
+
routes = Rails.root.join('config', 'routes.rb').to_s
|
50
|
+
Rails.application.assets.register_preprocessor 'application/javascript', :'js-routes_dependent_on_routes' do |ctx, data|
|
51
|
+
ctx.depend_on(routes) if ctx.logical_path == 'js-routes'
|
52
|
+
data
|
53
|
+
end
|
54
|
+
end
|
55
|
+
when -> (v) { v3.match?('', v) }
|
56
|
+
Rails.application.config.assets.configure do |config|
|
57
|
+
routes = Rails.root.join('config', 'routes.rb').to_s
|
58
|
+
config.register_preprocessor 'application/javascript', :'js-routes_dependent_on_routes' do |ctx, data|
|
59
|
+
ctx.depend_on(routes) if ctx.logical_path == 'js-routes'
|
60
|
+
data
|
61
|
+
end
|
62
|
+
end
|
63
|
+
when -> (v) { v37.match?('', v) }
|
64
|
+
Sprockets.register_preprocessor 'application/javascript', JsRoutesSprocketsExtension
|
65
|
+
else
|
66
|
+
raise StandardError, "Sprockets version #{sprockets_version} is not supported"
|
19
67
|
end
|
20
68
|
end
|
21
69
|
end
|
data/lib/js_routes/version.rb
CHANGED
data/lib/js_routes.rb
CHANGED
@@ -16,10 +16,11 @@ class JsRoutes
|
|
16
16
|
include: //,
|
17
17
|
file: DEFAULT_PATH,
|
18
18
|
prefix: nil,
|
19
|
-
url_links:
|
19
|
+
url_links: false,
|
20
20
|
camel_case: false,
|
21
21
|
default_url_options: {},
|
22
|
-
compact: false
|
22
|
+
compact: false,
|
23
|
+
serializer: nil
|
23
24
|
}
|
24
25
|
|
25
26
|
NODE_TYPES = {
|
@@ -34,6 +35,8 @@ class JsRoutes
|
|
34
35
|
}
|
35
36
|
|
36
37
|
LAST_OPTIONS_KEY = "options".freeze
|
38
|
+
FILTERED_DEFAULT_PARTS = [:controller, :action, :subdomain]
|
39
|
+
URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain]
|
37
40
|
|
38
41
|
class Options < Struct.new(*DEFAULTS.keys)
|
39
42
|
def to_hash
|
@@ -57,6 +60,11 @@ class JsRoutes
|
|
57
60
|
end
|
58
61
|
|
59
62
|
def generate(opts = {})
|
63
|
+
# Ensure routes are loaded. If they're not, load them.
|
64
|
+
if Rails.application.routes.named_routes.to_a.empty?
|
65
|
+
Rails.application.reload_routes!
|
66
|
+
end
|
67
|
+
|
60
68
|
new(opts).generate
|
61
69
|
end
|
62
70
|
|
@@ -92,21 +100,40 @@ class JsRoutes
|
|
92
100
|
end
|
93
101
|
|
94
102
|
def generate
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
103
|
+
{
|
104
|
+
"GEM_VERSION" => JsRoutes::VERSION,
|
105
|
+
"APP_CLASS" => Rails.application.class.to_s,
|
106
|
+
"NAMESPACE" => @options[:namespace],
|
107
|
+
"DEFAULT_URL_OPTIONS" => json(@options[:default_url_options].merge(deprecate_url_options)),
|
108
|
+
"PREFIX" => @options[:prefix] || Rails.application.config.relative_url_root || "",
|
109
|
+
"NODE_TYPES" => json(NODE_TYPES),
|
110
|
+
"SERIALIZER" => @options[:serializer] || "null",
|
111
|
+
"ROUTES" => js_routes,
|
112
|
+
}.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)|
|
113
|
+
js.gsub!(key, value)
|
114
|
+
end
|
101
115
|
end
|
102
116
|
|
103
|
-
def
|
117
|
+
def deprecate_url_options
|
118
|
+
result = {}
|
104
119
|
if @options.key?(:default_format)
|
105
120
|
warn("default_format option is deprecated. Use default_url_options = { format: <format> } instead")
|
106
|
-
|
107
|
-
else
|
108
|
-
{}
|
121
|
+
result.merge!( format: @options[:default_format] )
|
109
122
|
end
|
123
|
+
if @options[:url_links].is_a?(String)
|
124
|
+
ActiveSupport::Deprecation.warn('js-routes url_links config value must be a boolean. Use default_url_options for specifying a default host.')
|
125
|
+
|
126
|
+
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?
|
127
|
+
uri = URI.parse(@options[:url_links])
|
128
|
+
default_port = uri.scheme == "https" ? 443 : 80
|
129
|
+
port = uri.port == default_port ? nil : uri.port
|
130
|
+
result.merge!(
|
131
|
+
host: uri.host,
|
132
|
+
port: port,
|
133
|
+
protocol: uri.scheme,
|
134
|
+
)
|
135
|
+
end
|
136
|
+
result
|
110
137
|
end
|
111
138
|
|
112
139
|
def generate!(file_name = nil)
|
@@ -124,20 +151,23 @@ class JsRoutes
|
|
124
151
|
protected
|
125
152
|
|
126
153
|
def js_routes
|
127
|
-
js_routes = Rails.application.routes.named_routes.
|
128
|
-
|
129
|
-
|
130
|
-
rails_engine_app.routes.named_routes.map do |_, engine_route|
|
131
|
-
build_route_if_match(engine_route, route)
|
132
|
-
end
|
133
|
-
else
|
134
|
-
build_route_if_match(route)
|
135
|
-
end
|
136
|
-
end.flatten.compact
|
137
|
-
|
154
|
+
js_routes = Rails.application.routes.named_routes.to_a.sort_by(&:first).flat_map do |_, route|
|
155
|
+
[build_route_if_match(route)] + mounted_app_routes(route)
|
156
|
+
end.compact
|
138
157
|
"{\n" + js_routes.join(",\n") + "}\n"
|
139
158
|
end
|
140
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
|
+
|
141
171
|
def get_app_from_route(route)
|
142
172
|
# rails engine in Rails 4.2 use additional ActionDispatch::Routing::Mapper::Constraints, which contain app
|
143
173
|
if route.app.respond_to?(:app) && route.app.respond_to?(:constraints)
|
@@ -157,52 +187,42 @@ class JsRoutes
|
|
157
187
|
|
158
188
|
def any_match?(route, parent_route, matchers)
|
159
189
|
full_route = [parent_route.try(:name), route.name].compact.join('_')
|
160
|
-
|
190
|
+
|
161
191
|
matchers = Array(matchers)
|
162
192
|
matchers.any? {|regex| full_route =~ regex}
|
163
193
|
end
|
164
194
|
|
165
195
|
def build_js(route, parent_route)
|
166
196
|
name = [parent_route.try(:name), route.name].compact
|
167
|
-
parent_spec = parent_route.try(:path).try(:spec)
|
168
|
-
required_parts, optional_parts = route.required_parts.clone, route.optional_parts.clone
|
169
|
-
optional_parts.push(required_parts.delete :format) if required_parts.include?(:format)
|
170
197
|
route_name = generate_route_name(name, (:path unless @options[:compact]))
|
171
|
-
|
198
|
+
parent_spec = parent_route.try(:path).try(:spec)
|
199
|
+
route_arguments = route_js_arguments(route, parent_spec)
|
200
|
+
url_link = generate_url_link(name, route_name, route_arguments, route)
|
172
201
|
_ = <<-JS.strip!
|
173
202
|
// #{name.join('.')} => #{parent_spec}#{route.path.spec}
|
174
|
-
// function(#{
|
175
|
-
#{route_name}: Utils.route(#{
|
203
|
+
// function(#{build_params(route.required_parts)})
|
204
|
+
#{route_name}: Utils.route(#{route_arguments})#{",\n" + url_link if url_link.length > 0}
|
176
205
|
JS
|
177
206
|
end
|
178
207
|
|
179
|
-
def
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
208
|
+
def route_js_arguments(route, parent_spec)
|
209
|
+
required_parts = route.required_parts.clone
|
210
|
+
optional_parts = route.parts - required_parts
|
211
|
+
default_parts = route.defaults.select do |part, _|
|
212
|
+
FILTERED_DEFAULT_PARTS.exclude?(part) && URL_OPTIONS.include?(part) || required_parts.include?(part)
|
213
|
+
end
|
214
|
+
[
|
215
|
+
required_parts, optional_parts, serialize(route.path.spec, parent_spec), default_parts
|
216
|
+
].map do |argument|
|
217
|
+
json(argument)
|
218
|
+
end.join(", ")
|
186
219
|
end
|
187
220
|
|
188
|
-
def
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
return "#{@options[:url_links].inspect}"
|
194
|
-
else
|
195
|
-
protocol = route.defaults[:protocol] || @options[:default_url_options][:protocol] || 'http'
|
196
|
-
hostname = route.defaults[:host] || @options[:default_url_options][:host]
|
197
|
-
port = route.defaults[:port] || (@options[:default_url_options][:port] unless route.defaults[:host])
|
198
|
-
port = ":#{port}" if port
|
199
|
-
|
200
|
-
unless hostname
|
201
|
-
raise "A :default_url_options[:host] must be configured in order to generate *_url helpers"
|
202
|
-
end
|
203
|
-
|
204
|
-
return %Q|'#{protocol}://#{hostname}#{port}'|
|
205
|
-
end
|
221
|
+
def generate_url_link(name, route_name, route_arguments, route)
|
222
|
+
return "" unless @options[:url_links]
|
223
|
+
<<-JS.strip!
|
224
|
+
#{generate_route_name(name, :url)}: Utils.route(#{route_arguments}, true)
|
225
|
+
JS
|
206
226
|
end
|
207
227
|
|
208
228
|
def generate_route_name(name, suffix)
|
@@ -216,11 +236,7 @@ class JsRoutes
|
|
216
236
|
end
|
217
237
|
|
218
238
|
def build_params(required_parts)
|
219
|
-
params = required_parts
|
220
|
-
# prepending each parameter name with underscore
|
221
|
-
# to prevent conflict with JS reserved words
|
222
|
-
"_#{name}"
|
223
|
-
end << LAST_OPTIONS_KEY
|
239
|
+
params = required_parts + [LAST_OPTIONS_KEY]
|
224
240
|
params.join(", ")
|
225
241
|
end
|
226
242
|
|