js-routes 1.4.9 → 1.4.11

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
  SHA256:
3
- metadata.gz: bcc3b758ce05ed0444162cda94d5442b811a582330ea4418cf816763e4e482b0
4
- data.tar.gz: 3bfd8cc484ba00a6cd7ff0e5ffb90c3f3ec11c1d7aa9171572e93bc66b1cab42
3
+ metadata.gz: 9c3f51f23d5304f6e3df508ebada74e685ec6305b478f556cb76c8d6ad99a7ff
4
+ data.tar.gz: '081dda3bca55d88f5ad34089e9754a219c3603af20987aef0781beb1e57ed79e'
5
5
  SHA512:
6
- metadata.gz: a51ee9fc01642e16287b648085debd75749b983b2602a2407068cc7159cd302f8b04bd065b1ea28f7314cd19ae60e535cede86af4e7d7073648ffe2b3222d006
7
- data.tar.gz: a979758f820232a129f9befd597a248511c76b05eab8aae5b62b56bf31a2f94580b889371aa52e584aac0fdfd5802a03f1632eaee65713b2438d98e29265846b
6
+ metadata.gz: 98370a987b6d01b7d3eeab333767b50f668ed67cadeb51a5b7ce9298c85e51ef9edae8081a4396af46096a1dfa0b9854e32b548baef18e1774031c0ed31b0b38
7
+ data.tar.gz: e55fa57008421373e99cb9674701d64dc59dc79db419ab216a004bb76405950be5d8c906e4fbdc066d16c74fef0292b7de750da8a314703514736e936dcdaff0
@@ -1,5 +1,13 @@
1
1
  ## master
2
2
 
3
+ * Use app/javascript/routes.js as a default file location if app/javascript directory exists
4
+ * Add `default` export for better experience when used as es6 module
5
+
6
+
7
+ ## v1.4.10
8
+
9
+ * Require engine only when sprockets is loaded #257.
10
+
3
11
  ## v1.4.9
4
12
 
5
13
  * Allow to specify null namespace and receive routes as an object without assigning it anywhere #247
data/Readme.md CHANGED
@@ -40,7 +40,7 @@ JsRoutes.setup do |config|
40
40
  end
41
41
  ```
42
42
 
43
- Or dynamically in JavaScript, although not all configuration options are supported:
43
+ Or dynamically in JavaScript, although only [Formatter Options](#formatter-options) are supported (see below)
44
44
 
45
45
  ``` js
46
46
  Routes.configure({
@@ -49,47 +49,57 @@ Routes.configure({
49
49
  Routes.config(); // current config
50
50
  ```
51
51
 
52
- Available options:
52
+ #### Available Options
53
+
54
+ ##### Generator Options
55
+
56
+ Options to configure JavaScript file generator:
53
57
 
54
- * `default_url_options` - default parameters used when generating URLs
55
- * Option is configurable at JS level with `Routes.configure()`
56
- * Example: {:format => "json", :trailing\_slash => true, :protocol => "https", :subdomain => "api", :host => "example.com", :port => 3000}
57
- * Default: {}
58
58
  * `exclude` - Array of regexps to exclude from routes.
59
- * Default: []
59
+ * Default: `[]`
60
60
  * The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
61
61
  * `include` - Array of regexps to include in routes.
62
- * Default: []
62
+ * Default: `[]`
63
63
  * The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
64
64
  * `namespace` - global object used to access routes.
65
65
  * Supports nested namespace like `MyProject.routes`
66
66
  * Default: `Routes`
67
+ * `camel_case` - Generate camel case route names.
68
+ * Default: `false`
69
+ * `url_links` - Generate `*_url` helpers (in addition to the default `*_path` helpers).
70
+ * Example: `true`
71
+ * Default: `false`
72
+ * 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`.
73
+ * `compact` - Remove `_path` suffix in path routes(`*_url` routes stay untouched if they were enabled)
74
+ * Default: `false`
75
+ * Sample route call when option is set to true: Routes.users() => `/users`
76
+ * `application` - a key to specify which rails engine you want to generate routes too.
77
+ * This option allows to only generate routes for a specific rails engine, that is mounted into routes instead of all Rails app routes
78
+ * Default: `Rails.application`
79
+ * `file` - a file location where generated routes are stored
80
+ * Default: `app/assets/javascripts/routes.js` if `app/assets/javascripts` directory exists, otherwise `app/javascript/routes.js`.
81
+
82
+ ##### Formatter Options
83
+
84
+ Options to configure routes formatting:
85
+
86
+ * `default_url_options` - default parameters used when generating URLs
87
+ * Option is configurable at JS level with `Routes.configure()`
88
+ * Example: `{format: "json", trailing_slash: true, protocol: "https", subdomain: "api", host: "example.com", port: 3000}`
89
+ * Default: `{}`
67
90
  * `prefix` - String representing a url path to prepend to all paths.
68
91
  * Option is configurable at JS level with `Routes.configure()`
69
92
  * Example: `http://yourdomain.com`. This will cause route helpers to generate full path only.
70
93
  * Default: `Rails.application.config.relative_url_root`
71
- * `camel_case` (version >= 0.8.8) - Generate camel case route names.
72
- * Default: false
73
- * `url_links` (version >= 0.8.9) - Generate `*_url` helpers (in addition to the default `*_path` helpers).
74
- * Example: true
75
- * Default: false
76
- * 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`.
77
- * `compact` (version > 0.9.9) - Remove `_path` suffix in path routes(`*_url` routes stay untouched if they were enabled)
78
- * Default: false
79
- * Sample route call when option is set to true: Routes.users() => `/users`
80
- * `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"`.
81
- * Default: `nil`. Uses built-in serializer
94
+ * `serializer` - a JS function that serializes a Javascript Hash object into URL paramters like `{a: 1, b: 2} => "a=1&b=2"`.
95
+ * Default: `nil`. Uses built-in serializer compatible with Rails
82
96
  * Option is configurable at JS level with `Routes.configure()`
83
97
  * Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
84
- * Example: `MyApp.custom_serialize` - use completely custom serializer of your application.
85
-
98
+ * Example: `function (object) { ... }` - use completely custom serializer of your application.
86
99
  * `special_options_key` - a special key that helps JsRoutes to destinguish serialized model from options hash
87
- * This option is required because JS doesn't provide a difference between an object and a hash
100
+ * This option exists because JS doesn't provide a difference between an object and a hash
88
101
  * Option is configurable at JS level with `Routes.configure()`
89
102
  * Default: `_options`
90
- * `application` - a key to specify which rails engine you want to generate routes too.
91
- * This option allows to only generate routes for a specific rails engine, that is mounted into routes instead of all Rails app routes
92
- * Default: `Rails.application`
93
103
 
94
104
  ### Very Advanced Setup
95
105
 
@@ -122,9 +132,9 @@ If you want to generate the routes files outside of the asset pipeline, you can
122
132
 
123
133
  ``` ruby
124
134
  path = "app/assets/javascripts"
125
- JsRoutes.generate!("#{path}/app_routes.js", :namespace => "AppRoutes", :exclude => [/^admin_/, /^api_/])
126
- JsRoutes.generate!("#{path}/adm_routes.js", :namespace => "AdmRoutes", :include => /^admin_/)
127
- JsRoutes.generate!("#{path}/api_routes.js", :namespace => "ApiRoutes", :include => /^api_/, :default_url_options => {:format => "json"})
135
+ JsRoutes.generate!("#{path}/app_routes.js", namespace: "AppRoutes", exclude: [/^admin_/, /^api_/])
136
+ JsRoutes.generate!("#{path}/adm_routes.js", namespace: "AdmRoutes", include: /^admin_/)
137
+ JsRoutes.generate!("#{path}/api_routes.js", namespace: "ApiRoutes", include: /^api_/, default_url_options: {format: "json"})
128
138
  ```
129
139
 
130
140
  ### Rails relative URL root
@@ -206,7 +216,7 @@ Routes.company_project_path({company_id: 1, id: 2, _options: true}) // => "/comp
206
216
 
207
217
  ## What about security?
208
218
 
209
- JsRoutes itself do not have security holes. It makes URLs
219
+ JsRoutes itself does not have security holes. It makes URLs
210
220
  without access protection more reachable by potential attacker.
211
221
  In order to prevent this use `:exclude` option for sensitive urls like `/admin_/`
212
222
 
@@ -1,5 +1,7 @@
1
1
  require 'uri'
2
- require 'js_routes/engine' if defined?(Rails)
2
+ if defined?(::Rails) && defined?(::Sprockets::Railtie)
3
+ require 'js_routes/engine'
4
+ end
3
5
  require 'js_routes/version'
4
6
 
5
7
  class JsRoutes
@@ -8,13 +10,16 @@ class JsRoutes
8
10
  # OPTIONS
9
11
  #
10
12
 
11
- DEFAULT_PATH = File.join('app','assets','javascripts','routes.js')
12
-
13
13
  DEFAULTS = {
14
- namespace: "Routes",
14
+ namespace: -> { defined?(Webpacker) ? nil : "Routes" },
15
15
  exclude: [],
16
16
  include: //,
17
- file: DEFAULT_PATH,
17
+ file: -> do
18
+ sprockets_dir = Rails.root.join('app','assets','javascripts')
19
+ sprockets_file = sprockets_dir.join('routes.js')
20
+ webpacker_file = Rails.root.join('app', 'javascript', 'routes.js')
21
+ Dir.exists?(sprockets_dir) ? sprockets_file : webpacker_file
22
+ end,
18
23
  prefix: -> { Rails.application.config.relative_url_root || "" },
19
24
  url_links: false,
20
25
  camel_case: false,
@@ -23,7 +28,7 @@ class JsRoutes
23
28
  serializer: nil,
24
29
  special_options_key: "_options",
25
30
  application: -> { Rails.application }
26
- }
31
+ } #:nodoc:
27
32
 
28
33
  NODE_TYPES = {
29
34
  GROUP: 1,
@@ -34,11 +39,11 @@ class JsRoutes
34
39
  LITERAL: 6,
35
40
  SLASH: 7,
36
41
  DOT: 8
37
- }
42
+ } #:nodoc:
38
43
 
39
- LAST_OPTIONS_KEY = "options".freeze
40
- FILTERED_DEFAULT_PARTS = [:controller, :action]
41
- URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain]
44
+ LAST_OPTIONS_KEY = "options".freeze #:nodoc:
45
+ FILTERED_DEFAULT_PARTS = [:controller, :action] #:nodoc:
46
+ URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain] #:nodoc:
42
47
 
43
48
  class Configuration < Struct.new(*DEFAULTS.keys)
44
49
  def initialize(attributes = nil)
@@ -215,7 +220,7 @@ class JsRoutes
215
220
  parent_spec = parent_route.try(:path).try(:spec)
216
221
  route_arguments = route_js_arguments(route, parent_spec)
217
222
  url_link = generate_url_link(name, route_arguments)
218
- _ = <<-JS.strip!
223
+ <<-JS.strip!
219
224
  // #{name.join('.')} => #{parent_spec}#{route.path.spec}
220
225
  // function(#{build_params(route.required_parts)})
221
226
  #{route_name}: Utils.route(#{route_arguments})#{",\n" + url_link if url_link.length > 0}
@@ -47,7 +47,6 @@ class Engine < ::Rails::Engine
47
47
  when -> (v) { v2.match?('', v) },
48
48
  -> (v) { vgte3.match?('', v) }
49
49
 
50
- # Other rails version, assumed newer
51
50
  Rails.application.config.assets.configure do |config|
52
51
  config.register_preprocessor(
53
52
  "application/javascript",
@@ -1,3 +1,3 @@
1
1
  class JsRoutes
2
- VERSION = "1.4.9"
2
+ VERSION = "1.4.11"
3
3
  end
@@ -497,16 +497,13 @@ Based on Rails RAILS_VERSION routes of APP_CLASS
497
497
  routes.config = function() {
498
498
  return Utils.config();
499
499
  };
500
- Object.defineProperty(routes, 'defaults', {
501
- get: function() {
502
- throw new Error(NAMESPACE + ".defaults is removed. Use " + NAMESPACE + ".configure() instead.");
503
- },
504
- set: function(value) {}
505
- });
506
500
  routes.default_serializer = function(object, prefix) {
507
501
  return Utils.default_serializer(object, prefix);
508
502
  };
509
- return Utils.namespace(root, NAMESPACE, routes);
503
+ Utils.namespace(root, NAMESPACE, routes);
504
+ return Object.assign({
505
+ "default": routes
506
+ }, routes);
510
507
  }
511
508
  };
512
509
 
@@ -393,15 +393,11 @@ Utils =
393
393
  routes = ROUTES
394
394
  routes.configure = (config) -> Utils.configure(config)
395
395
  routes.config = -> Utils.config()
396
- Object.defineProperty routes, 'defaults',
397
- get: ->
398
- throw new Error("#{NAMESPACE}.defaults is removed. Use #{NAMESPACE}.configure() instead.")
399
- set: (value) ->
400
-
401
396
  routes.default_serializer = (object, prefix) ->
402
397
  Utils.default_serializer(object, prefix)
403
398
  # Browser globals
404
399
  Utils.namespace(root, NAMESPACE, routes)
400
+ Object.assign({default: routes}, routes)
405
401
 
406
402
  result = Utils.make()
407
403
  # Set up Routes appropriately for the environment.
@@ -0,0 +1,2 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../stylesheets .css
@@ -39,4 +39,7 @@ EOF
39
39
  expect(evaljs("require(['js-routes'], function(r){ return r.inboxes_path(); })")).to eq(test_routes.inboxes_path())
40
40
  end
41
41
 
42
+ it "should define default export for es6 modules" do
43
+ expect(evaljs("require(['js-routes'], function(r){ return r.default.inboxes_path(); })")).to eq(test_routes.inboxes_path())
44
+ end
42
45
  end
@@ -60,11 +60,6 @@ describe JsRoutes do
60
60
  end
61
61
 
62
62
  it "should not generate file before initialization" do
63
- # This method is alread fixed in Rails master
64
- # But in 3.2 stable we need to hack it like this
65
- if Rails.application.instance_variable_get("@initialized")
66
- pending
67
- end
68
63
  expect(File.exists?(name)).to be_falsey
69
64
  end
70
65
 
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.4.9
4
+ version: 1.4.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-19 00:00:00.000000000 Z
11
+ date: 2020-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -172,6 +172,7 @@ files:
172
172
  - lib/routes.js
173
173
  - lib/routes.js.coffee
174
174
  - lib/tasks/js_routes.rake
175
+ - spec/dummy/app/assets/config/manifest.js
175
176
  - spec/dummy/app/assets/javascripts/.gitkeep
176
177
  - spec/dummy/config/routes.rb
177
178
  - spec/js_routes/amd_compatibility_spec.rb
@@ -186,7 +187,7 @@ homepage: http://github.com/railsware/js-routes
186
187
  licenses:
187
188
  - MIT
188
189
  metadata: {}
189
- post_install_message:
190
+ post_install_message:
190
191
  rdoc_options: []
191
192
  require_paths:
192
193
  - lib
@@ -201,9 +202,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
202
  - !ruby/object:Gem::Version
202
203
  version: '0'
203
204
  requirements: []
204
- rubyforge_project:
205
- rubygems_version: 2.7.8
206
- signing_key:
205
+ rubygems_version: 3.0.8
206
+ signing_key:
207
207
  specification_version: 4
208
208
  summary: Brings Rails named routes to javascript
209
209
  test_files: []