js_from_routes 1.0.0 → 2.0.1
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/CHANGELOG.md +38 -0
- data/LICENSE.txt +22 -0
- data/README.md +10 -128
- data/lib/js_from_routes.rb +2 -2
- data/lib/js_from_routes/generator.rb +126 -68
- data/lib/js_from_routes/railtie.rb +4 -2
- data/lib/js_from_routes/template.js.erb +9 -0
- data/lib/js_from_routes/template_all.js.erb +11 -0
- data/lib/js_from_routes/version.rb +3 -1
- metadata +97 -65
- data/spec/js_from_routes/js_from_routes_spec.rb +0 -75
- data/spec/spec_helper.rb +0 -7
- data/spec/support/sample_app/app/controllers/application_controller.rb +0 -2
- data/spec/support/sample_app/app/controllers/comments_controller.rb +0 -2
- data/spec/support/sample_app/app/controllers/user_preferences_controller.rb +0 -2
- data/spec/support/sample_app/app/controllers/video_clips_controller.rb +0 -2
- data/spec/support/sample_app/app/controllers/welcome_controller.rb +0 -2
- data/spec/support/sample_app/app/mailers/application_mailer.rb +0 -4
- data/spec/support/sample_app/config/application.rb +0 -20
- data/spec/support/sample_app/config/boot.rb +0 -4
- data/spec/support/sample_app/config/environment.rb +0 -5
- data/spec/support/sample_app/config/environments/development.rb +0 -33
- data/spec/support/sample_app/config/initializers/application_controller_renderer.rb +0 -8
- data/spec/support/sample_app/config/initializers/assets.rb +0 -14
- data/spec/support/sample_app/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/support/sample_app/config/initializers/content_security_policy.rb +0 -30
- data/spec/support/sample_app/config/initializers/cookies_serializer.rb +0 -5
- data/spec/support/sample_app/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/support/sample_app/config/initializers/inflections.rb +0 -16
- data/spec/support/sample_app/config/initializers/mime_types.rb +0 -4
- data/spec/support/sample_app/config/initializers/wrap_parameters.rb +0 -14
- data/spec/support/sample_app/config/puma.rb +0 -38
- data/spec/support/sample_app/config/routes.rb +0 -22
- data/spec/support/sample_app/node_modules/node-sass/src/libsass/extconf.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5663dc5fb9fd9062e81af864e59d7fb8ec61625d5db06c0f55c04307afde8314
|
4
|
+
data.tar.gz: 5dc7aa69994d396c22472c249e216045a4b6466b4041d7df6974f9bf6f6e5f75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 782f8ee6ca92ab3bcb84220e4012bb235b8aeafeeff88e54499f93d35fa75fbfcde6799460a53f4dd6dd2990ac9a3934dd07632a7e1e4b7ecea527f66a324e1c
|
7
|
+
data.tar.gz: ebdc1552cfae84ce61f36ad7f70fc302c6b1994b29ad06956391ab9701cd8a639dbb4d8317d2122907b474b114f01dd36e61de22d7b3aa118f694349d64a2a12
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
## [2.0.1](https://github.com/ElMassimo/js_from_routes/compare/js_from_routes@2.0.0...js_from_routes@2.0.1) (2021-03-14)
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
# [2.0.0](https://github.com/ElMassimo/js_from_routes/compare/v1.0.3...js_from_routes@2.0.0) (2021-03-13)
|
6
|
+
|
7
|
+
### Features ⚡️
|
8
|
+
|
9
|
+
- Path helpers now support both making a request or obtaining the path
|
10
|
+
- Auto-detect the folder where javascript files live (`frontend`, `packs`, `javascript`, or `assets`)
|
11
|
+
- The new default template is more flexible and can be easily customized using the `client_library` setting
|
12
|
+
|
13
|
+
### Breaking Changes 💥
|
14
|
+
|
15
|
+
- Created client libraries that provide the necessary functionality out of the box (optional)
|
16
|
+
- Changed the default template, now using `definePathHelper`
|
17
|
+
- Defaults for code generation have changed (`Requests` → `Api`)
|
18
|
+
- `path_only?` and `request_method?` have been removed from `Route` during code generation
|
19
|
+
- You may still check for it in your custom templates, using `route.export == :path_only`
|
20
|
+
|
21
|
+
## 1.0.3 (2021-03-10) ##
|
22
|
+
|
23
|
+
* Use `~/` instead of `@/` in the default template imports.
|
24
|
+
|
25
|
+
|
26
|
+
## 1.0.2 (2021-03-10) ##
|
27
|
+
|
28
|
+
* Ensure a default `template.js.erb` ships with the gem.
|
29
|
+
|
30
|
+
|
31
|
+
## 1.0.1 (2020-06-21) ##
|
32
|
+
|
33
|
+
* Expose `export_setting` in the `Route` API, to support custom workflows.
|
34
|
+
|
35
|
+
|
36
|
+
## 1.0.0 (2020-06-21) ##
|
37
|
+
|
38
|
+
* Initial Release.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2021 Máximo Mussini
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,35 +1,25 @@
|
|
1
1
|
<h1 align="center">
|
2
2
|
JS From Rails Routes
|
3
3
|
<p align="center">
|
4
|
-
<a href="https://rubygems.org/gems/js_from_routes"><img alt="Gem Version" src="https://img.shields.io/gem/v/js_from_routes.svg?colorB=e9573f"/></a>
|
5
4
|
<a href="https://travis-ci.org/ElMassimo/js_from_routes"><img alt="Build Status" src="https://travis-ci.org/ElMassimo/js_from_routes.svg"/></a>
|
6
|
-
<a href="https://codeclimate.com/github/ElMassimo/js_from_routes"><img alt="Coverage Status" src="https://codeclimate.com/github/ElMassimo/request_store_rails/badges/coverage.svg"/></a>
|
7
|
-
<a href="https://codeclimate.com/github/ElMassimo/js_from_routes"><img alt="Code Quality" src="https://codeclimate.com/github/ElMassimo/js_from_routes/badges/gpa.svg"/></a>
|
8
5
|
<a href="http://inch-ci.org/github/ElMassimo/js_from_routes"><img alt="Inline docs" src="http://inch-ci.org/github/ElMassimo/js_from_routes.svg"/></a>
|
9
|
-
<a href="https://
|
6
|
+
<a href="https://codeclimate.com/github/ElMassimo/js_from_routes"><img alt="Maintainability" src="https://codeclimate.com/github/ElMassimo/js_from_routes/badges/gpa.svg"/></a>
|
7
|
+
<a href="https://codeclimate.com/github/ElMassimo/js_from_routes"><img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/js_from_routes/badges/coverage.svg"/></a>
|
8
|
+
<a href="https://rubygems.org/gems/js_from_routes"><img alt="Gem Version" src="https://img.shields.io/gem/v/js_from_routes.svg?colorB=e9573f"/></a>
|
9
|
+
<a href="https://github.com/ElMassimo/js_from_routes/blob/main/LICENSE.txt"><img alt="License" src="https://img.shields.io/badge/license-MIT-428F7E.svg"/></a>
|
10
10
|
</p>
|
11
11
|
</h1>
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
## Why? 🤔
|
18
|
-
|
19
|
-
Path helpers in Rails are useful to ensure the constructed URLs match endpoints
|
20
|
-
defined in the app.
|
13
|
+
[Vite Rails]: https://vite-ruby.netlify.app/
|
14
|
+
[aliases]: https://vite-ruby.netlify.app/guide/development.html#import-aliases-%F0%9F%91%89
|
15
|
+
[config options]: https://github.com/ElMassimo/js_from_routes/blob/main/lib/js_from_routes/generator.rb#L82-L85
|
16
|
+
[readme]: https://github.com/ElMassimo/js_from_routes
|
21
17
|
|
22
|
-
|
23
|
-
|
24
|
-
- No need to manually specify the URL, preventing mistakes and saving development time.
|
25
|
-
- If an action is renamed or removed, the helper ceases to exist, which causes
|
26
|
-
an error that is easier to detect than a 404.
|
27
|
-
- We can embed the the HTTP verb in the helper. Changing the verb in the route causes the JS
|
28
|
-
code to be regenerated, no need to update the consumer!
|
18
|
+
For more information, check the main [README].
|
29
19
|
|
30
20
|
### Installation 💿
|
31
21
|
|
32
|
-
Add this line to your application's Gemfile:
|
22
|
+
Add this line to your application's Gemfile in the `development` group:
|
33
23
|
|
34
24
|
```ruby
|
35
25
|
gem 'js_from_routes'
|
@@ -42,111 +32,3 @@ And then execute:
|
|
42
32
|
Or install it yourself as:
|
43
33
|
|
44
34
|
$ gem install js_from_routes
|
45
|
-
|
46
|
-
### Usage 🚀
|
47
|
-
|
48
|
-
#### 1. Specify the Rails routes you want to export
|
49
|
-
|
50
|
-
Use the `export` attribute to determine which [routes](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/config/routes.rb#L6) should be taken into account when generating JS.
|
51
|
-
|
52
|
-
```ruby
|
53
|
-
Rails.application.routes.draw do
|
54
|
-
resources :video_clips, export: true do
|
55
|
-
get :download, on: :member, export: :path_only
|
56
|
-
get :trending, on: :collection, export: false
|
57
|
-
end
|
58
|
-
end
|
59
|
-
```
|
60
|
-
|
61
|
-
#### 2. Generate JS code from your routes
|
62
|
-
|
63
|
-
This is usually done automatically the next time you make a request to your
|
64
|
-
Rails server (such as when you refresh the page), which causes Rails reloader to
|
65
|
-
kick in, and the routes to be generated.
|
66
|
-
|
67
|
-
If you are not running a local development server, or prefer to do it manually,
|
68
|
-
you can use a rake task instead:
|
69
|
-
|
70
|
-
```
|
71
|
-
bin/rake js_from_routes:generate
|
72
|
-
```
|
73
|
-
|
74
|
-
#### 3. Use the generated code in your JS application
|
75
|
-
|
76
|
-
This can happen in many [different ways](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/Videos.vue#L10), but to illustrate using the example above:
|
77
|
-
|
78
|
-
```js
|
79
|
-
import VideoClipsRequests from '@requests/VideoClipsRequests'
|
80
|
-
|
81
|
-
VideoClipsRequests.get({ id: 'oHg5SJYRHA0' }).then(displayVideo)
|
82
|
-
|
83
|
-
const path = VideoClipsRequests.downloadPath({ id })
|
84
|
-
```
|
85
|
-
|
86
|
-
Check the [examples](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/Videos.vue) for ideas on how to [use it](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/Videos.vue), and how you can [configure](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/config/webpack/aliases.js#L11) Webpack to your convenience.
|
87
|
-
|
88
|
-
Read on to find out how to customize the generated code to suit your needs.
|
89
|
-
|
90
|
-
### Advanced Configuration 📖
|
91
|
-
|
92
|
-
Since all projects are different, it's very unlikely that the default settings
|
93
|
-
fulfill all your requirements.
|
94
|
-
|
95
|
-
The following [settings](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L77-L80) are available:
|
96
|
-
|
97
|
-
##### [`file_suffix`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L77), default: `Requests.js`
|
98
|
-
|
99
|
-
This suffix is added by default to all generated files. You can modify it to
|
100
|
-
if you prefer a different convention.
|
101
|
-
|
102
|
-
##### [`helper_mappings`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L80)
|
103
|
-
|
104
|
-
By default it maps `index` to `list` and `show` to `get`, which helps to make
|
105
|
-
the JS code read more natural.
|
106
|
-
|
107
|
-
##### [`output_folder`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L78), default: `app/javascript/requests`
|
108
|
-
|
109
|
-
The directory where the generated files are created.
|
110
|
-
|
111
|
-
Tip: It's highly recommended to [add a webpack alias](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/config/webpack/aliases.js#L11), to simplify [imports](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/Videos.vue#2).
|
112
|
-
|
113
|
-
##### [`template_path`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L79)
|
114
|
-
|
115
|
-
A [default template](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/template.js.erb) is provided, but it makes assumptions about the [available](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/services/ApiService.js#L17) [code](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/helpers/UrlHelper.js#L28).
|
116
|
-
|
117
|
-
You will probably want to use a custom template, such as:
|
118
|
-
|
119
|
-
```ruby
|
120
|
-
# config/initializers/js_from_routes.rb
|
121
|
-
JsFromRoutes.config do |config|
|
122
|
-
config.template = Rails.root.join('app', 'views', 'custom_js_from_routes.js.erb')
|
123
|
-
end
|
124
|
-
```
|
125
|
-
|
126
|
-
A `routes` variable will be available in the template, which will contain the
|
127
|
-
endpoints exported for a controller.
|
128
|
-
|
129
|
-
Each `route` exposes properties such as `verb` and `path`, please [check the
|
130
|
-
source code](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L34-L71) for details on the [API](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L34-L71).
|
131
|
-
|
132
|
-
### How does it work? ⚙️
|
133
|
-
|
134
|
-
By adding a hook to Rails' reload process in development, it's possible to
|
135
|
-
automatically generate files from routes when a route is added, modified, or removed.
|
136
|
-
|
137
|
-
In order to optimize file generation, the generated JS files are split by
|
138
|
-
controller, and add a cache key based on the routes to avoid rewriting the file
|
139
|
-
if the route definition hasn't changed.
|
140
|
-
|
141
|
-
When the Webpack development server is running, it detects when a new file is
|
142
|
-
generated, automatically triggering a new build, which can now use the generated
|
143
|
-
request methods or path helpers 😃
|
144
|
-
|
145
|
-
### Take this idea 💡
|
146
|
-
|
147
|
-
There are plenty of opportunities for automatic code generation, such as keeping
|
148
|
-
enums in sync between Ruby and JS.
|
149
|
-
|
150
|
-
Let me know if you come up with new or creative ways to use this technique 😃
|
151
|
-
|
152
|
-
|
data/lib/js_from_routes.rb
CHANGED
@@ -3,5 +3,5 @@
|
|
3
3
|
# Splitting the generator file allows consumers to skip the Railtie if desired:
|
4
4
|
# - gem 'js_from_routes', require: false
|
5
5
|
# - require 'js_from_routes/generator'
|
6
|
-
require
|
7
|
-
require
|
6
|
+
require "js_from_routes/generator"
|
7
|
+
require "js_from_routes/railtie"
|
@@ -1,18 +1,41 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "digest"
|
4
|
+
require "erubi"
|
5
|
+
require "fileutils"
|
6
|
+
require "pathname"
|
6
7
|
|
7
8
|
# Public: Automatically generates JS for Rails routes with { export: true }.
|
8
9
|
# Generates one file per controller, and one function per route.
|
9
10
|
module JsFromRoutes
|
11
|
+
# Internal: Helper class used as a presenter for the all helpers template.
|
12
|
+
class AllRoutes
|
13
|
+
attr_reader :helpers
|
14
|
+
|
15
|
+
def initialize(helpers, config)
|
16
|
+
@helpers, @config = helpers, config
|
17
|
+
end
|
18
|
+
|
19
|
+
# Public: Used to check whether the file should be generated again, changes
|
20
|
+
# based on the configuration, and route definition.
|
21
|
+
def cache_key
|
22
|
+
helpers.map(&:import_filename).join + File.read(@config.template_all_path)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Internal: Name of the JS file where all helpers will be exported.
|
26
|
+
def filename
|
27
|
+
path = @config.all_helpers_file
|
28
|
+
path = "index#{File.extname(@config.file_suffix)}" if path == true
|
29
|
+
@config.output_folder.join(path)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
10
33
|
# Internal: Helper class used as a presenter for the routes template.
|
11
|
-
class
|
34
|
+
class ControllerRoutes
|
12
35
|
attr_reader :routes
|
13
36
|
|
14
|
-
def initialize(routes, config)
|
15
|
-
@config = config
|
37
|
+
def initialize(controller, routes, config)
|
38
|
+
@controller, @config = controller, config
|
16
39
|
@routes = routes
|
17
40
|
.uniq { |route| route.requirements.fetch(:action) }
|
18
41
|
.map { |route| Route.new(route, config.helper_mappings) }
|
@@ -21,13 +44,32 @@ module JsFromRoutes
|
|
21
44
|
# Public: Used to check whether the file should be generated again, changes
|
22
45
|
# based on the configuration, and route definition.
|
23
46
|
def cache_key
|
24
|
-
|
47
|
+
routes.map(&:inspect).join + [File.read(@config.template_path), @config.helper_mappings.inspect].join
|
48
|
+
end
|
49
|
+
|
50
|
+
# Public: Exposes the preferred import library to the generator.
|
51
|
+
def client_library
|
52
|
+
@config.client_library
|
53
|
+
end
|
54
|
+
|
55
|
+
# Internal: Name of the JS file with helpers for the the given controller.
|
56
|
+
def filename
|
57
|
+
@config.output_folder.join(basename)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Public: Name of the JS file with helpers for the the given controller.
|
61
|
+
def import_filename
|
62
|
+
@config.output_folder.basename.join((basename.split(".")[0]).to_s)
|
25
63
|
end
|
26
64
|
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
65
|
+
# Public: Name of the file as a valid JS variable.
|
66
|
+
def js_name
|
67
|
+
@controller.camelize(:lower).tr(":", "_")
|
68
|
+
end
|
69
|
+
|
70
|
+
# Internal: The base name of the JS file to be written.
|
71
|
+
def basename
|
72
|
+
"#{@controller.camelize}#{@config.file_suffix}".tr_s(":", "/")
|
31
73
|
end
|
32
74
|
end
|
33
75
|
|
@@ -37,14 +79,9 @@ module JsFromRoutes
|
|
37
79
|
@route, @mappings = route, mappings
|
38
80
|
end
|
39
81
|
|
40
|
-
# Public:
|
41
|
-
def
|
42
|
-
@route.defaults[:export]
|
43
|
-
end
|
44
|
-
|
45
|
-
# Public: Whether it should export only the path.
|
46
|
-
def request_method?
|
47
|
-
!path_only?
|
82
|
+
# Public: The `export` setting specified for the action.
|
83
|
+
def export
|
84
|
+
@route.defaults[:export]
|
48
85
|
end
|
49
86
|
|
50
87
|
# Public: The HTTP verb for the action. Example: 'patch'
|
@@ -54,88 +91,109 @@ module JsFromRoutes
|
|
54
91
|
|
55
92
|
# Public: The path for the action. Example: '/users/:id/edit'
|
56
93
|
def path
|
57
|
-
@route.path.spec.to_s.chomp(
|
94
|
+
@route.path.spec.to_s.chomp("(.:format)")
|
58
95
|
end
|
59
96
|
|
60
97
|
# Public: The name of the JS helper for the action. Example: 'destroyAll'
|
61
98
|
def helper
|
62
99
|
action = @route.requirements.fetch(:action).camelize(:lower)
|
63
|
-
|
64
|
-
path_only? ? "#{ name }Path" : name
|
100
|
+
@mappings.fetch(action, action)
|
65
101
|
end
|
66
102
|
|
67
103
|
# Internal: Useful as a cache key for the route, and for debugging purposes.
|
68
104
|
def inspect
|
69
|
-
"#{
|
105
|
+
"#{verb} #{helper} #{path}"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# Internal: Represents a compiled template that can write itself to a file.
|
110
|
+
class Template
|
111
|
+
def initialize(template_path)
|
112
|
+
# NOTE: The compiled ERB template, used to generate JS code.
|
113
|
+
@compiled_template = Erubi::Engine.new(File.read(template_path), filename: template_path).src
|
114
|
+
end
|
115
|
+
|
116
|
+
# Public: Checks if the cache is fresh, or renders the template with the
|
117
|
+
# specified variables, and writes the updated result to a file.
|
118
|
+
def write_if_changed(object)
|
119
|
+
write_file_if_changed(object.filename, object.cache_key) { render_template(object) }
|
120
|
+
end
|
121
|
+
|
122
|
+
private
|
123
|
+
|
124
|
+
# Internal: Returns a String with the generated JS code.
|
125
|
+
def render_template(object)
|
126
|
+
object.instance_eval(@compiled_template)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Internal: Writes if the file does not exist or the cache key has changed.
|
130
|
+
# The cache strategy consists of a comment on the first line of the file.
|
131
|
+
#
|
132
|
+
# Yields to receive the rendered file content when it needs to.
|
133
|
+
def write_file_if_changed(name, cache_key)
|
134
|
+
FileUtils.mkdir_p(name.dirname)
|
135
|
+
cache_key_comment = "// JsFromRoutes CacheKey #{Digest::MD5.hexdigest(cache_key)}\n"
|
136
|
+
File.open(name, "a+") { |file|
|
137
|
+
if file.gets != cache_key_comment
|
138
|
+
file.truncate(0)
|
139
|
+
file.write(cache_key_comment)
|
140
|
+
file.write(yield)
|
141
|
+
end
|
142
|
+
}
|
70
143
|
end
|
71
144
|
end
|
72
145
|
|
73
146
|
class << self
|
74
147
|
# Public: Configuration of the code generator.
|
75
148
|
def config
|
76
|
-
@config ||= OpenStruct.new(
|
77
|
-
file_suffix: 'Requests.js',
|
78
|
-
output_folder: ::Rails.root&.join('app', 'javascript', 'requests'),
|
79
|
-
template_path: File.expand_path('template.js.erb', __dir__),
|
80
|
-
helper_mappings: { 'index' => 'list', 'show' => 'get' },
|
81
|
-
)
|
149
|
+
@config ||= OpenStruct.new(default_config(::Rails.root || Pathname.new(Dir.pwd)))
|
82
150
|
yield(@config) if block_given?
|
83
151
|
@config
|
84
152
|
end
|
85
153
|
|
86
154
|
# Public: Generates code for the specified routes with { export: true }.
|
87
155
|
def generate!(app_or_routes = Rails.application)
|
88
|
-
raise ArgumentError,
|
156
|
+
raise ArgumentError, "A Rails app must be defined, or you must specify a custom `output_folder`" if config.output_folder.blank?
|
89
157
|
rails_routes = app_or_routes.is_a?(::Rails::Engine) ? app_or_routes.routes.routes : app_or_routes
|
90
|
-
|
91
|
-
exported_routes_by_controller(rails_routes).each do |controller, controller_routes|
|
92
|
-
routes = Routes.new(controller_routes, config)
|
93
|
-
write_if_changed(filename_for(controller), routes.cache_key) { render_template(routes) }
|
94
|
-
end
|
158
|
+
generate_files exported_routes_by_controller(rails_routes)
|
95
159
|
end
|
96
160
|
|
97
|
-
|
161
|
+
private
|
98
162
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
163
|
+
def generate_files(exported_routes)
|
164
|
+
template = Template.new(config.template_path)
|
165
|
+
generate_file_for_all exported_routes.map { |controller, routes|
|
166
|
+
ControllerRoutes.new(controller, routes, config).tap do |routes|
|
167
|
+
template.write_if_changed routes
|
168
|
+
end
|
105
169
|
}
|
106
170
|
end
|
107
171
|
|
108
|
-
|
109
|
-
|
110
|
-
config.output_folder.join("#{ controller.camelize }#{ config.file_suffix }".tr_s(':', '/'))
|
111
|
-
end
|
172
|
+
def generate_file_for_all(routes)
|
173
|
+
return unless config.all_helpers_file && !routes.empty?
|
112
174
|
|
113
|
-
|
114
|
-
def render_template(routes)
|
115
|
-
routes.evaluate(compiled_template)
|
175
|
+
Template.new(config.template_all_path).write_if_changed AllRoutes.new(routes, config)
|
116
176
|
end
|
117
177
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
178
|
+
def default_config(root)
|
179
|
+
dir = %w[frontend packs javascript assets].find { |dir| root.join("app", dir).exist? }
|
180
|
+
{
|
181
|
+
all_helpers_file: true,
|
182
|
+
client_library: "@js-from-routes/client",
|
183
|
+
file_suffix: "Api.js",
|
184
|
+
helper_mappings: {"index" => "list", "show" => "get"},
|
185
|
+
output_folder: root.join("app", dir, "api"),
|
186
|
+
template_path: File.expand_path("template.js.erb", __dir__),
|
187
|
+
template_all_path: File.expand_path("template_all.js.erb", __dir__),
|
188
|
+
}
|
124
189
|
end
|
125
190
|
|
126
|
-
# Internal:
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
cache_key_comment = "// JsFromRoutes CacheKey #{ cache_key }\n"
|
133
|
-
File.open(name, 'a+') { |file|
|
134
|
-
if file.gets != cache_key_comment
|
135
|
-
file.truncate(0)
|
136
|
-
file.write(cache_key_comment)
|
137
|
-
file.write(yield)
|
138
|
-
end
|
191
|
+
# Internal: Returns exported routes grouped by controller name.
|
192
|
+
def exported_routes_by_controller(routes)
|
193
|
+
routes.select { |route|
|
194
|
+
route.defaults.fetch(:export, false)
|
195
|
+
}.group_by { |route|
|
196
|
+
route.requirements.fetch(:controller)
|
139
197
|
}
|
140
198
|
end
|
141
199
|
end
|
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "rails/railtie"
|
4
|
+
|
3
5
|
# NOTE: Not strictly required, but it helps to simplify the setup.
|
4
6
|
class JsFromRoutes::Railtie < Rails::Railtie
|
5
7
|
railtie_name :js_from_routes
|
6
8
|
|
7
9
|
if Rails.env.development?
|
8
10
|
# Allows to automatically trigger code generation after updating routes.
|
9
|
-
initializer
|
11
|
+
initializer "js_from_routes.reloader" do |app|
|
10
12
|
app.config.to_prepare do
|
11
13
|
JsFromRoutes.generate!(app)
|
12
14
|
end
|
@@ -16,7 +18,7 @@ class JsFromRoutes::Railtie < Rails::Railtie
|
|
16
18
|
# Suitable when triggering code generation manually.
|
17
19
|
rake_tasks do |app|
|
18
20
|
namespace :js_from_routes do
|
19
|
-
desc
|
21
|
+
desc "Generates JavaScript files from Rails routes, one file per controller, and one function per route."
|
20
22
|
task generate: :environment do
|
21
23
|
JsFromRoutes.generate!(app)
|
22
24
|
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
//
|
2
|
+
// DO NOT MODIFY: This file was automatically generated by JsFromRoutes.
|
3
|
+
import { definePathHelper } from '<%= client_library %>'
|
4
|
+
|
5
|
+
export default {
|
6
|
+
<% routes.each do |route| %>
|
7
|
+
<%= route.helper %>: definePathHelper('<%= route.verb %>', '<%= route.path %>'),
|
8
|
+
<% end %>
|
9
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
//
|
2
|
+
// DO NOT MODIFY: This file was automatically generated by JsFromRoutes.
|
3
|
+
<% helpers.each do |helper| %>
|
4
|
+
import <%= helper.js_name %> from '~/<%= helper.import_filename %>'
|
5
|
+
<% end %>
|
6
|
+
|
7
|
+
export default {
|
8
|
+
<% helpers.each do |helper| %>
|
9
|
+
<%= helper.js_name %>,
|
10
|
+
<% end %>
|
11
|
+
}
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# Public: Automatically generates JS for Rails routes with { export: true }.
|
4
|
+
# Generates one file per controller, and one function per route.
|
3
5
|
module JsFromRoutes
|
4
6
|
# Public: This library adheres to semantic versioning.
|
5
|
-
VERSION =
|
7
|
+
VERSION = "2.0.1"
|
6
8
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js_from_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Máximo Mussini
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -16,42 +16,118 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.1'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '8'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
29
|
+
version: '5.1'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '8'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: bundler
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
31
|
-
- - "
|
37
|
+
- - "~>"
|
32
38
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
39
|
+
version: '2'
|
34
40
|
type: :development
|
35
41
|
prerelease: false
|
36
42
|
version_requirements: !ruby/object:Gem::Requirement
|
37
43
|
requirements:
|
38
|
-
- - "
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: listen
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.2'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
39
59
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
60
|
+
version: '3.2'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: pry-byebug
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.9'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '3.9'
|
41
75
|
- !ruby/object:Gem::Dependency
|
42
76
|
name: rake
|
43
77
|
requirement: !ruby/object:Gem::Requirement
|
44
78
|
requirements:
|
45
|
-
- - "
|
79
|
+
- - "~>"
|
46
80
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
81
|
+
version: '13'
|
48
82
|
type: :development
|
49
83
|
prerelease: false
|
50
84
|
version_requirements: !ruby/object:Gem::Requirement
|
51
85
|
requirements:
|
52
|
-
- - "
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '13'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rspec-given
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '3.8'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '3.8'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: simplecov
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "<"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0.18'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "<"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0.18'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: standard
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '1.0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
53
129
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
130
|
+
version: '1.0'
|
55
131
|
description: js_from_routes helps you by automatically generating path and API helpers
|
56
132
|
from Rails route definitions, allowing you to save development effort and focus
|
57
133
|
on the things that matter.
|
@@ -62,40 +138,20 @@ extensions: []
|
|
62
138
|
extra_rdoc_files:
|
63
139
|
- README.md
|
64
140
|
files:
|
141
|
+
- CHANGELOG.md
|
142
|
+
- LICENSE.txt
|
65
143
|
- README.md
|
66
144
|
- lib/js_from_routes.rb
|
67
145
|
- lib/js_from_routes/generator.rb
|
68
146
|
- lib/js_from_routes/railtie.rb
|
147
|
+
- lib/js_from_routes/template.js.erb
|
148
|
+
- lib/js_from_routes/template_all.js.erb
|
69
149
|
- lib/js_from_routes/version.rb
|
70
|
-
- spec/js_from_routes/js_from_routes_spec.rb
|
71
|
-
- spec/spec_helper.rb
|
72
|
-
- spec/support/sample_app/app/controllers/application_controller.rb
|
73
|
-
- spec/support/sample_app/app/controllers/comments_controller.rb
|
74
|
-
- spec/support/sample_app/app/controllers/user_preferences_controller.rb
|
75
|
-
- spec/support/sample_app/app/controllers/video_clips_controller.rb
|
76
|
-
- spec/support/sample_app/app/controllers/welcome_controller.rb
|
77
|
-
- spec/support/sample_app/app/mailers/application_mailer.rb
|
78
|
-
- spec/support/sample_app/config/application.rb
|
79
|
-
- spec/support/sample_app/config/boot.rb
|
80
|
-
- spec/support/sample_app/config/environment.rb
|
81
|
-
- spec/support/sample_app/config/environments/development.rb
|
82
|
-
- spec/support/sample_app/config/initializers/application_controller_renderer.rb
|
83
|
-
- spec/support/sample_app/config/initializers/assets.rb
|
84
|
-
- spec/support/sample_app/config/initializers/backtrace_silencers.rb
|
85
|
-
- spec/support/sample_app/config/initializers/content_security_policy.rb
|
86
|
-
- spec/support/sample_app/config/initializers/cookies_serializer.rb
|
87
|
-
- spec/support/sample_app/config/initializers/filter_parameter_logging.rb
|
88
|
-
- spec/support/sample_app/config/initializers/inflections.rb
|
89
|
-
- spec/support/sample_app/config/initializers/mime_types.rb
|
90
|
-
- spec/support/sample_app/config/initializers/wrap_parameters.rb
|
91
|
-
- spec/support/sample_app/config/puma.rb
|
92
|
-
- spec/support/sample_app/config/routes.rb
|
93
|
-
- spec/support/sample_app/node_modules/node-sass/src/libsass/extconf.rb
|
94
150
|
homepage: https://github.com/ElMassimo/js_from_routes
|
95
151
|
licenses:
|
96
152
|
- MIT
|
97
153
|
metadata: {}
|
98
|
-
post_install_message:
|
154
|
+
post_install_message:
|
99
155
|
rdoc_options: []
|
100
156
|
require_paths:
|
101
157
|
- lib
|
@@ -110,32 +166,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
166
|
- !ruby/object:Gem::Version
|
111
167
|
version: '0'
|
112
168
|
requirements: []
|
113
|
-
rubygems_version: 3.1.
|
114
|
-
signing_key:
|
169
|
+
rubygems_version: 3.1.4
|
170
|
+
signing_key:
|
115
171
|
specification_version: 4
|
116
172
|
summary: Generate JS automatically from Rails routes.
|
117
|
-
test_files:
|
118
|
-
- spec/spec_helper.rb
|
119
|
-
- spec/js_from_routes/js_from_routes_spec.rb
|
120
|
-
- spec/support/sample_app/app/mailers/application_mailer.rb
|
121
|
-
- spec/support/sample_app/app/controllers/video_clips_controller.rb
|
122
|
-
- spec/support/sample_app/app/controllers/application_controller.rb
|
123
|
-
- spec/support/sample_app/app/controllers/comments_controller.rb
|
124
|
-
- spec/support/sample_app/app/controllers/welcome_controller.rb
|
125
|
-
- spec/support/sample_app/app/controllers/user_preferences_controller.rb
|
126
|
-
- spec/support/sample_app/config/routes.rb
|
127
|
-
- spec/support/sample_app/config/environments/development.rb
|
128
|
-
- spec/support/sample_app/config/environment.rb
|
129
|
-
- spec/support/sample_app/config/application.rb
|
130
|
-
- spec/support/sample_app/config/puma.rb
|
131
|
-
- spec/support/sample_app/config/boot.rb
|
132
|
-
- spec/support/sample_app/config/initializers/application_controller_renderer.rb
|
133
|
-
- spec/support/sample_app/config/initializers/backtrace_silencers.rb
|
134
|
-
- spec/support/sample_app/config/initializers/mime_types.rb
|
135
|
-
- spec/support/sample_app/config/initializers/filter_parameter_logging.rb
|
136
|
-
- spec/support/sample_app/config/initializers/wrap_parameters.rb
|
137
|
-
- spec/support/sample_app/config/initializers/assets.rb
|
138
|
-
- spec/support/sample_app/config/initializers/cookies_serializer.rb
|
139
|
-
- spec/support/sample_app/config/initializers/content_security_policy.rb
|
140
|
-
- spec/support/sample_app/config/initializers/inflections.rb
|
141
|
-
- spec/support/sample_app/node_modules/node-sass/src/libsass/extconf.rb
|
173
|
+
test_files: []
|
@@ -1,75 +0,0 @@
|
|
1
|
-
require 'support/sample_app/config/application'
|
2
|
-
require 'support/sample_app/config/routes'
|
3
|
-
|
4
|
-
|
5
|
-
describe JsFromRoutes do
|
6
|
-
ORIGINAL_TEMPLATE_PATH = JsFromRoutes.config.template_path
|
7
|
-
|
8
|
-
let(:output_dir) { Pathname.new File.expand_path('../support/generated', __dir__) }
|
9
|
-
let(:sample_dir) { Rails.root.join('app', 'javascript', 'requests') }
|
10
|
-
let(:different_template_path) { File.expand_path('../support/different_template.js.erb', __dir__) }
|
11
|
-
let(:controllers_with_exported_routes) { %w[Comments UserPreferences VideoClips] }
|
12
|
-
|
13
|
-
def file_for(dir, name)
|
14
|
-
dir.join("#{ name }Requests.js")
|
15
|
-
end
|
16
|
-
|
17
|
-
def sample_file_for(name)
|
18
|
-
file_for(sample_dir, name)
|
19
|
-
end
|
20
|
-
|
21
|
-
def output_file_for(name)
|
22
|
-
file_for(output_dir, name)
|
23
|
-
end
|
24
|
-
|
25
|
-
before do
|
26
|
-
# Sanity checks
|
27
|
-
expect(sample_dir.exist?).to eq true
|
28
|
-
expect(Rails.application.routes.routes).to be_present
|
29
|
-
|
30
|
-
# Remove directory from a previous test run.
|
31
|
-
FileUtils.remove_dir(output_dir) rescue nil
|
32
|
-
|
33
|
-
# Change the configuration to use a different directory.
|
34
|
-
JsFromRoutes.config do |config|
|
35
|
-
config.output_folder = output_dir
|
36
|
-
config.template_path = ORIGINAL_TEMPLATE_PATH
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# NOTE: We do a manual snapshot test for now, more tests coming in the future.
|
41
|
-
it 'should generate the files as expected' do
|
42
|
-
expect(JsFromRoutes).to receive(:render_template).exactly(3).times.and_call_original
|
43
|
-
JsFromRoutes.generate!
|
44
|
-
|
45
|
-
# It does not generate routes that don't have `export: true`.
|
46
|
-
expect(output_file_for('Welcome').exist?).to eq false
|
47
|
-
|
48
|
-
# It generates one file per controller with exported routes.
|
49
|
-
controllers_with_exported_routes.each do |file_name|
|
50
|
-
expect(output_file_for(file_name).read).to eq sample_file_for(file_name).read
|
51
|
-
end
|
52
|
-
|
53
|
-
# It detects changes to the template, and regenerates the files.
|
54
|
-
JsFromRoutes.config do |config|
|
55
|
-
config.template_path = different_template_path
|
56
|
-
end
|
57
|
-
expect(JsFromRoutes).to receive(:render_template).exactly(3).times.and_call_original
|
58
|
-
JsFromRoutes.generate!
|
59
|
-
|
60
|
-
# These files should no longer match the sample ones.
|
61
|
-
controllers_with_exported_routes.each do |file_name|
|
62
|
-
expect(output_file_for(file_name).read).not_to eq sample_file_for(file_name).read
|
63
|
-
end
|
64
|
-
|
65
|
-
# It should not rewrite the files if the cache key has not changed.
|
66
|
-
expect(JsFromRoutes).not_to receive(:render_template)
|
67
|
-
JsFromRoutes.generate!
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'should have a rake task available' do
|
71
|
-
Rails.application.load_tasks
|
72
|
-
expect(JsFromRoutes).to receive(:render_template).exactly(3).times
|
73
|
-
expect { Rake::Task['js_from_routes:generate'].invoke }.not_to raise_error
|
74
|
-
end
|
75
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require_relative 'boot'
|
2
|
-
|
3
|
-
require "action_controller/railtie"
|
4
|
-
require "sprockets/railtie"
|
5
|
-
|
6
|
-
# Require the gems listed in Gemfile, including any gems
|
7
|
-
# you've limited to :test, :development, or :production.
|
8
|
-
Bundler.require(*Rails.groups)
|
9
|
-
|
10
|
-
module SampleApp
|
11
|
-
class Application < Rails::Application
|
12
|
-
# Initialize configuration defaults for originally generated Rails version.
|
13
|
-
config.load_defaults 6.0
|
14
|
-
|
15
|
-
# Settings in config/environments/* take precedence over those specified here.
|
16
|
-
# Application configuration can go into files in config/initializers
|
17
|
-
# -- all .rb files in that directory are automatically loaded after loading
|
18
|
-
# the framework and any gems in your application.
|
19
|
-
end
|
20
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
Rails.application.configure do
|
2
|
-
# Settings specified here will take precedence over those in config/application.rb.
|
3
|
-
|
4
|
-
# In the development environment your application's code is reloaded on
|
5
|
-
# every request. This slows down response time but is perfect for development
|
6
|
-
# since you don't have to restart the web server when you make code changes.
|
7
|
-
config.cache_classes = false
|
8
|
-
|
9
|
-
# Do not eager load code on boot.
|
10
|
-
config.eager_load = false
|
11
|
-
|
12
|
-
# Show full error reports.
|
13
|
-
config.consider_all_requests_local = true
|
14
|
-
|
15
|
-
# Store uploaded files on the local file system (see config/storage.yml for options).
|
16
|
-
# Print deprecation notices to the Rails logger.
|
17
|
-
config.active_support.deprecation = :log
|
18
|
-
|
19
|
-
# Debug mode disables concatenation and preprocessing of assets.
|
20
|
-
# This option may cause significant delays in view rendering with a large
|
21
|
-
# number of complex assets.
|
22
|
-
config.assets.debug = true
|
23
|
-
|
24
|
-
# Suppress logger output for asset requests.
|
25
|
-
config.assets.quiet = true
|
26
|
-
|
27
|
-
# Raises error for missing translations.
|
28
|
-
# config.action_view.raise_on_missing_translations = true
|
29
|
-
|
30
|
-
# Use an evented file watcher to asynchronously detect changes in source code,
|
31
|
-
# routes, locales, etc. This feature depends on the listen gem.
|
32
|
-
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
|
33
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file.
|
2
|
-
|
3
|
-
# Version of your assets, change this if you want to expire all your assets.
|
4
|
-
Rails.application.config.assets.version = '1.0'
|
5
|
-
|
6
|
-
# Add additional assets to the asset load path.
|
7
|
-
# Rails.application.config.assets.paths << Emoji.images_path
|
8
|
-
# Add Yarn node_modules folder to the asset load path.
|
9
|
-
Rails.application.config.assets.paths << Rails.root.join('node_modules')
|
10
|
-
|
11
|
-
# Precompile additional assets.
|
12
|
-
# application.js, application.css, and all non-JS/CSS in the app/assets
|
13
|
-
# folder are already added.
|
14
|
-
# Rails.application.config.assets.precompile += %w( admin.js admin.css )
|
@@ -1,7 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file.
|
2
|
-
|
3
|
-
# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
|
4
|
-
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
|
5
|
-
|
6
|
-
# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
|
7
|
-
# Rails.backtrace_cleaner.remove_silencers!
|
@@ -1,30 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file.
|
2
|
-
|
3
|
-
# Define an application-wide content security policy
|
4
|
-
# For further information see the following documentation
|
5
|
-
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
|
6
|
-
|
7
|
-
# Rails.application.config.content_security_policy do |policy|
|
8
|
-
# policy.default_src :self, :https
|
9
|
-
# policy.font_src :self, :https, :data
|
10
|
-
# policy.img_src :self, :https, :data
|
11
|
-
# policy.object_src :none
|
12
|
-
# policy.script_src :self, :https
|
13
|
-
# policy.style_src :self, :https
|
14
|
-
# # If you are using webpack-dev-server then specify webpack-dev-server host
|
15
|
-
# policy.connect_src :self, :https, "http://localhost:3035", "ws://localhost:3035" if Rails.env.development?
|
16
|
-
|
17
|
-
# # Specify URI for violation reports
|
18
|
-
# # policy.report_uri "/csp-violation-report-endpoint"
|
19
|
-
# end
|
20
|
-
|
21
|
-
# If you are using UJS then enable automatic nonce generation
|
22
|
-
# Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
|
23
|
-
|
24
|
-
# Set the nonce only to specific directives
|
25
|
-
# Rails.application.config.content_security_policy_nonce_directives = %w(script-src)
|
26
|
-
|
27
|
-
# Report CSP violations to a specified URI
|
28
|
-
# For further information see the following documentation:
|
29
|
-
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
|
30
|
-
# Rails.application.config.content_security_policy_report_only = true
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file.
|
2
|
-
|
3
|
-
# Add new inflection rules using the following format. Inflections
|
4
|
-
# are locale specific, and you may define rules for as many different
|
5
|
-
# locales as you wish. All of these examples are active by default:
|
6
|
-
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
7
|
-
# inflect.plural /^(ox)$/i, '\1en'
|
8
|
-
# inflect.singular /^(ox)en/i, '\1'
|
9
|
-
# inflect.irregular 'person', 'people'
|
10
|
-
# inflect.uncountable %w( fish sheep )
|
11
|
-
# end
|
12
|
-
|
13
|
-
# These inflection rules are supported but not enabled by default:
|
14
|
-
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
15
|
-
# inflect.acronym 'RESTful'
|
16
|
-
# end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file.
|
2
|
-
|
3
|
-
# This file contains settings for ActionController::ParamsWrapper which
|
4
|
-
# is enabled by default.
|
5
|
-
|
6
|
-
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
|
7
|
-
ActiveSupport.on_load(:action_controller) do
|
8
|
-
wrap_parameters format: [:json]
|
9
|
-
end
|
10
|
-
|
11
|
-
# To enable root element in JSON for ActiveRecord objects.
|
12
|
-
# ActiveSupport.on_load(:active_record) do
|
13
|
-
# self.include_root_in_json = true
|
14
|
-
# end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
# Puma can serve each request in a thread from an internal thread pool.
|
2
|
-
# The `threads` method setting takes two numbers: a minimum and maximum.
|
3
|
-
# Any libraries that use thread pools should be configured to match
|
4
|
-
# the maximum value specified for Puma. Default is set to 5 threads for minimum
|
5
|
-
# and maximum; this matches the default thread size of Active Record.
|
6
|
-
#
|
7
|
-
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
|
8
|
-
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
|
9
|
-
threads min_threads_count, max_threads_count
|
10
|
-
|
11
|
-
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
|
12
|
-
#
|
13
|
-
port ENV.fetch("PORT") { 3000 }
|
14
|
-
|
15
|
-
# Specifies the `environment` that Puma will run in.
|
16
|
-
#
|
17
|
-
environment ENV.fetch("RAILS_ENV") { "development" }
|
18
|
-
|
19
|
-
# Specifies the `pidfile` that Puma will use.
|
20
|
-
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
|
21
|
-
|
22
|
-
# Specifies the number of `workers` to boot in clustered mode.
|
23
|
-
# Workers are forked web server processes. If using threads and workers together
|
24
|
-
# the concurrency of the application would be max `threads` * `workers`.
|
25
|
-
# Workers do not work on JRuby or Windows (both of which do not support
|
26
|
-
# processes).
|
27
|
-
#
|
28
|
-
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
|
29
|
-
|
30
|
-
# Use the `preload_app!` method when specifying a `workers` number.
|
31
|
-
# This directive tells Puma to first boot the application and load code
|
32
|
-
# before forking the application. This takes advantage of Copy On Write
|
33
|
-
# process behavior so workers use less memory.
|
34
|
-
#
|
35
|
-
# preload_app!
|
36
|
-
|
37
|
-
# Allow puma to be restarted by `rails restart` command.
|
38
|
-
plugin :tmp_restart
|
@@ -1,22 +0,0 @@
|
|
1
|
-
Rails.application.routes.draw do
|
2
|
-
root to: 'welcome#home'
|
3
|
-
|
4
|
-
resources :welcome
|
5
|
-
|
6
|
-
resources :video_clips, only: [:new, :edit, :create, :update, :destroy], export: true do
|
7
|
-
get :download, on: :member, export: :path_only
|
8
|
-
patch :add_to_playlist, on: :member
|
9
|
-
patch :remove_from_playlist, on: :member
|
10
|
-
get :trending, on: :collection
|
11
|
-
get '/thumbnail/:thumbnail_id', as: :thumbnail, action: :thumbnail, on: :member
|
12
|
-
|
13
|
-
resources :comments, only: [:show, :index], shallow: true, export: true
|
14
|
-
end
|
15
|
-
|
16
|
-
resources :user_preferences, only: [], export: true do
|
17
|
-
patch :switch_to_classic_navbar, on: :collection
|
18
|
-
get :switch_to_beta_navbar, on: :collection, export: false
|
19
|
-
get '/switch_to_classic/:page', action: :switch_to_classic, on: :collection, export: :path_only
|
20
|
-
get '/switch_to_beta/:page', action: :switch_to_beta, on: :collection, as: :switch_to_beta_page, export: :path_only
|
21
|
-
end
|
22
|
-
end
|