js_from_routes 1.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -3
  3. data/LICENSE.txt +22 -0
  4. data/README.md +6 -153
  5. data/lib/js_from_routes.rb +2 -2
  6. data/lib/js_from_routes/generator.rb +129 -71
  7. data/lib/js_from_routes/railtie.rb +4 -2
  8. data/lib/js_from_routes/template.js.erb +3 -16
  9. data/lib/js_from_routes/template_all.js.erb +11 -0
  10. data/lib/js_from_routes/version.rb +1 -1
  11. metadata +91 -63
  12. data/spec/js_from_routes/js_from_routes_spec.rb +0 -75
  13. data/spec/spec_helper.rb +0 -7
  14. data/spec/support/sample_app/app/controllers/application_controller.rb +0 -2
  15. data/spec/support/sample_app/app/controllers/comments_controller.rb +0 -2
  16. data/spec/support/sample_app/app/controllers/user_preferences_controller.rb +0 -2
  17. data/spec/support/sample_app/app/controllers/video_clips_controller.rb +0 -2
  18. data/spec/support/sample_app/app/controllers/welcome_controller.rb +0 -2
  19. data/spec/support/sample_app/app/mailers/application_mailer.rb +0 -4
  20. data/spec/support/sample_app/config/application.rb +0 -20
  21. data/spec/support/sample_app/config/boot.rb +0 -4
  22. data/spec/support/sample_app/config/environment.rb +0 -5
  23. data/spec/support/sample_app/config/environments/development.rb +0 -33
  24. data/spec/support/sample_app/config/environments/test.rb +0 -5
  25. data/spec/support/sample_app/config/initializers/application_controller_renderer.rb +0 -8
  26. data/spec/support/sample_app/config/initializers/assets.rb +0 -14
  27. data/spec/support/sample_app/config/initializers/backtrace_silencers.rb +0 -7
  28. data/spec/support/sample_app/config/initializers/content_security_policy.rb +0 -30
  29. data/spec/support/sample_app/config/initializers/cookies_serializer.rb +0 -5
  30. data/spec/support/sample_app/config/initializers/filter_parameter_logging.rb +0 -4
  31. data/spec/support/sample_app/config/initializers/inflections.rb +0 -16
  32. data/spec/support/sample_app/config/initializers/mime_types.rb +0 -4
  33. data/spec/support/sample_app/config/initializers/wrap_parameters.rb +0 -14
  34. data/spec/support/sample_app/config/puma.rb +0 -38
  35. data/spec/support/sample_app/config/routes.rb +0 -22
  36. 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: 8202e9323cb738556cdc4e126c7c343eaae6b01d52a72bdff00eb93b7ab925af
4
- data.tar.gz: 3caa18595ef98a85e8dd7f162e0f530d708a464c2bcc1fd823b4cfbaea947af7
3
+ metadata.gz: db1718354ae9ce8148221f6cb5331398809cb684450cf1d88d43636f5b2f0c40
4
+ data.tar.gz: d883f5e22c950ed87481ac9d8d1fca08ef669c5fe56b7fd51cc48124d9640077
5
5
  SHA512:
6
- metadata.gz: 3bba34efe8b9a91114708326e556dcdfe7549231d61aeb366bb16759471615f8aedb87692eab8420b0039695b359e02da5be4f974c604a535a4ae19efa131ecc
7
- data.tar.gz: f54793177eb17df4e42548775f1dbd935b366f838d49488947100f606008eefc48d96b6ad38c0611d95bc5d267c52a24aead758856ddfdd0c87adc3b745e46bf
6
+ metadata.gz: 319b235a6fcc6bf8564517486de5fb9790fc5076fa0b3030b711eb49ea058c5ba877fc7b8a217976f9556cd330c8f78114e5a2361a11367536d25fad233ef013
7
+ data.tar.gz: 8ec656121b7a10bb0123ede54409a5327d215124d6fdd11f38f5aad34d1f56e0e982dc5139cada10a978037d6fe0767ed94ef1abdcdded1d61d104438a12ff9f
data/CHANGELOG.md CHANGED
@@ -1,13 +1,60 @@
1
- ## JsFromRoutes 1.0.2 (2021-03-10) ##
1
+ ## [2.0.3](https://github.com/ElMassimo/js_from_routes/compare/js_from_routes@2.0.2...js_from_routes@2.0.3) (2021-03-16)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Ensure changing the client library triggers code generation ([6cf2bdf](https://github.com/ElMassimo/js_from_routes/commit/6cf2bdf4896dafe0d1e80668551665c46bfcadc6))
7
+
8
+
9
+ ### Features
10
+
11
+ * Allow passing JS_FROM_ROUTES_FORCE=true to ignore cache keys ([8a6d2a8](https://github.com/ElMassimo/js_from_routes/commit/8a6d2a807e0a9926c6b24e1fc9127f917ec0ed5d))
12
+
13
+
14
+
15
+ ## [2.0.2](https://github.com/ElMassimo/js_from_routes/compare/js_from_routes@2.0.1...js_from_routes@2.0.2) (2021-03-14)
16
+
17
+ ### Improvements
18
+
19
+ - Remove underscores from namespaced controllers in global file ([90fdcc2](https://github.com/ElMassimo/js_from_routes/commit/90fdcc2))
20
+
21
+ ## [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)
22
+
23
+ ### Features ⚡️
24
+
25
+ - Enable generation of index combining and exporting all helpers (#9)
26
+
27
+ # [2.0.0](https://github.com/ElMassimo/js_from_routes/compare/v1.0.3...js_from_routes@2.0.0) (2021-03-13)
28
+
29
+ ### Features ⚡️
30
+
31
+ - Path helpers now support both making a request or obtaining the path
32
+ - Auto-detect the folder where javascript files live (`frontend`, `packs`, `javascript`, or `assets`)
33
+ - The new default template is more flexible and can be easily customized using the `client_library` setting
34
+
35
+ ### Breaking Changes 💥
36
+
37
+ - Created client libraries that provide the necessary functionality out of the box (optional)
38
+ - Changed the default template, now using `definePathHelper`
39
+ - Defaults for code generation have changed (`Requests` → `Api`)
40
+ - `path_only?` and `request_method?` have been removed from `Route` during code generation
41
+ - You may still check for it in your custom templates, using `route.export == :path_only`
42
+
43
+ ## 1.0.3 (2021-03-10) ##
44
+
45
+ * Use `~/` instead of `@/` in the default template imports.
46
+
47
+
48
+ ## 1.0.2 (2021-03-10) ##
2
49
 
3
50
  * Ensure a default `template.js.erb` ships with the gem.
4
51
 
5
52
 
6
- ## JsFromRoutes 1.0.1 (2020-06-21) ##
53
+ ## 1.0.1 (2020-06-21) ##
7
54
 
8
55
  * Expose `export_setting` in the `Route` API, to support custom workflows.
9
56
 
10
57
 
11
- ## JsFromRoutes 1.0.0 (2020-06-21) ##
58
+ ## 1.0.0 (2020-06-21) ##
12
59
 
13
60
  * 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
@@ -6,30 +6,16 @@ JS From Rails Routes
6
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
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
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/master/LICENSE.txt"><img alt="License" src="https://img.shields.io/badge/license-MIT-428F7E.svg"/></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
- _JS from Routes_ helps you by automatically generating path and API helpers from
14
- Rails route definitions, allowing you to save development effort and focus on
15
- the things that matter.
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
16
17
 
17
- Check out [this pull request](https://github.com/ElMassimo/pingcrm-vite/pull/2) to get a sense of how flexible it can be.
18
-
19
- ### Why? 🤔
20
-
21
- Path helpers in Rails are useful, and make it easier to build urls, avoiding
22
- typos and mistakes.
23
-
24
- With this library, it's possible the enjoy the same benefits in JS:
25
-
26
- - No need to manually specify the URL, preventing mistakes and saving development time.
27
- - If an action is renamed or removed, the helper ceases to exist, which causes
28
- an error that is easier to detect than a 404.
29
- - We can embed the the HTTP verb in the helper. Changing the verb in the route causes the JS
30
- code to be regenerated, no need to update the consumer!
31
-
32
- Read more about it in the [blog announcement](https://maximomussini.com/posts/js-from-routes/).
18
+ For more information, check the main [README].
33
19
 
34
20
  ### Installation 💿
35
21
 
@@ -46,136 +32,3 @@ And then execute:
46
32
  Or install it yourself as:
47
33
 
48
34
  $ gem install js_from_routes
49
-
50
- ### Usage 🚀
51
-
52
- #### 1. Specify the Rails routes you want to export
53
-
54
- 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.
55
-
56
- ```ruby
57
- Rails.application.routes.draw do
58
- resources :video_clips, export: true do
59
- get :download, on: :member, export: :path_only
60
- get :trending, on: :collection, export: false
61
- end
62
- end
63
- ```
64
-
65
- #### 2. Generate JS code from your routes
66
-
67
- This is usually done automatically the next time you make a request to your
68
- Rails server (such as when you refresh the page), which causes Rails reloader to
69
- kick in, and the routes to be generated.
70
-
71
- If you are not running a local development server, or prefer to do it manually,
72
- you can use a rake task instead:
73
-
74
- ```
75
- bin/rake js_from_routes:generate
76
- ```
77
-
78
- which can generate code such as:
79
-
80
- ```js
81
- import { formatUrl } from '@helpers/UrlHelper'
82
- import { request } from '@services/ApiService'
83
-
84
- export default {
85
- downloadPath: options =>
86
- formatUrl('/video_clips/:id/download', options),
87
-
88
- get: options =>
89
- request('get', '/video_clips/:id', options),
90
-
91
- update: options =>
92
- request('patch', '/video_clips/:id', options),
93
- }
94
- ```
95
-
96
- #### 3. Use the generated code in your JS application
97
-
98
- 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, in combination with [`axios`](https://github.com/axios/axios) or `fetch`:
99
-
100
- ```js
101
- import VideoClipsRequests from '@requests/VideoClipsRequests'
102
-
103
- VideoClipsRequests.get({ id: 'oHg5SJYRHA0' }).then(data => { this.video = data })
104
-
105
- const newVideo = { ...this.video, format: '.mp4' }
106
- VideoClipsRequests.update(newVideo)
107
-
108
- const path = VideoClipsRequests.downloadPath(newVideo)
109
- ```
110
-
111
- 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.
112
-
113
- Read on to find out how to customize the generated code to suit your needs.
114
-
115
- ### Advanced Configuration 📖
116
-
117
- Since all projects are different, it's very unlikely that the default settings
118
- fulfill all your requirements.
119
-
120
- The following [settings](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L77-L80) are available:
121
-
122
- ##### [`file_suffix`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L77), default: `Requests.js`
123
-
124
- This suffix is added by default to all generated files. You can modify it to
125
- if you prefer a different convention, or if you use it to generate TypeScript.
126
-
127
- ##### [`helper_mappings`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L80)
128
-
129
- By default it maps `index` to `list` and `show` to `get`, which helps to make
130
- the JS code read more naturally.
131
-
132
- ##### [`output_folder`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L78), default: `app/javascript/requests`
133
-
134
- The directory where the generated files are created.
135
-
136
- 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).
137
-
138
- ##### [`template_path`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L79)
139
-
140
- 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).
141
-
142
- You will probably want to use a custom template, such as:
143
-
144
- ```ruby
145
- # config/initializers/js_from_routes.rb
146
- if Rails.env.development?
147
- JsFromRoutes.config do |config|
148
- config.template_path = Rails.root.join('app', 'views', 'custom_js_from_routes.js.erb')
149
- end
150
- end
151
- ```
152
-
153
- A `routes` variable will be available in the template, which will contain the
154
- endpoints exported for a controller.
155
-
156
- Each `route` exposes properties such as `verb` and `path`, please [check the
157
- 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).
158
-
159
- ### How does it work? ⚙️
160
-
161
- By adding a hook to Rails' reload process in development, it's possible to
162
- automatically generate files from routes when a route is added, modified, or removed.
163
-
164
- In order to optimize file generation, the generated JS files are split by
165
- controller, and add a cache key based on the routes to avoid rewriting the file
166
- if the route definition hasn't changed.
167
-
168
- When the Webpack development server is running, it detects when a new file is
169
- generated, automatically triggering a new build, which can now use the generated
170
- request methods or path helpers 😃
171
-
172
- ### Take this idea 💡
173
-
174
- While the original use cases intended to generate code that targes a custom `ApiService`,
175
- it can be tweaked to generate TypeScript, [target jQuery](https://gist.github.com/ElMassimo/cab56e64e20ff797f3054b661a883646),
176
- or use it only to generate [path helpers](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/requests/UserPreferencesRequests.js#L11-L15).
177
-
178
- There are plenty of other opportunities for automatic code generation, such as keeping
179
- enums in sync between Ruby and JS.
180
-
181
- Let me know if you come up with new or creative ways to use this technique 😃
@@ -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 'js_from_routes/generator'
7
- require 'js_from_routes/railtie'
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 'digest'
4
- require 'erubi'
5
- require 'fileutils'
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 Routes
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
- Digest::MD5.hexdigest(routes.map(&:inspect).join + [File.read(@config.template_path), @config.helper_mappings.inspect].join)
47
+ routes.map(&:inspect).join + [File.read(@config.template_path), @config.helper_mappings.inspect, @config.client_library].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
- # Internal: By performing the evaluation here, we ensure only "routes" is
28
- # exposed to the ERB template as a local variable.
29
- def evaluate(compiled_template)
30
- instance_eval(compiled_template)
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,21 +79,11 @@ module JsFromRoutes
37
79
  @route, @mappings = route, mappings
38
80
  end
39
81
 
40
- # Public: Whether it should export only the path.
41
- def path_only?
42
- export_setting == :path_only
43
- end
44
-
45
82
  # Public: The `export` setting specified for the action.
46
- def export_setting
83
+ def export
47
84
  @route.defaults[:export]
48
85
  end
49
86
 
50
- # Public: Whether it should export only the path.
51
- def request_method?
52
- !path_only?
53
- end
54
-
55
87
  # Public: The HTTP verb for the action. Example: 'patch'
56
88
  def verb
57
89
  @route.verb.downcase
@@ -59,88 +91,114 @@ module JsFromRoutes
59
91
 
60
92
  # Public: The path for the action. Example: '/users/:id/edit'
61
93
  def path
62
- @route.path.spec.to_s.chomp('(.:format)')
94
+ @route.path.spec.to_s.chomp("(.:format)")
63
95
  end
64
96
 
65
97
  # Public: The name of the JS helper for the action. Example: 'destroyAll'
66
98
  def helper
67
99
  action = @route.requirements.fetch(:action).camelize(:lower)
68
- name = @mappings.fetch(action, action)
69
- path_only? ? "#{ name }Path" : name
100
+ @mappings.fetch(action, action)
70
101
  end
71
102
 
72
103
  # Internal: Useful as a cache key for the route, and for debugging purposes.
73
104
  def inspect
74
- "#{ verb } #{ helper } #{ path }"
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: Returns true if the cache key has changed since the last codegen.
130
+ def stale?(file, cache_key_comment)
131
+ ENV["JS_FROM_ROUTES_FORCE"] || file.gets != cache_key_comment
132
+ end
133
+
134
+ # Internal: Writes if the file does not exist or the cache key has changed.
135
+ # The cache strategy consists of a comment on the first line of the file.
136
+ #
137
+ # Yields to receive the rendered file content when it needs to.
138
+ def write_file_if_changed(name, cache_key)
139
+ FileUtils.mkdir_p(name.dirname)
140
+ cache_key_comment = "// JsFromRoutes CacheKey #{Digest::MD5.hexdigest(cache_key)}\n"
141
+ File.open(name, "a+") { |file|
142
+ if stale?(file, cache_key_comment)
143
+ file.truncate(0)
144
+ file.write(cache_key_comment)
145
+ file.write(yield)
146
+ end
147
+ }
75
148
  end
76
149
  end
77
150
 
78
151
  class << self
79
152
  # Public: Configuration of the code generator.
80
153
  def config
81
- @config ||= OpenStruct.new(
82
- file_suffix: 'Requests.js',
83
- output_folder: ::Rails.root&.join('app', 'javascript', 'requests'),
84
- template_path: File.expand_path('template.js.erb', __dir__),
85
- helper_mappings: { 'index' => 'list', 'show' => 'get' },
86
- )
154
+ @config ||= OpenStruct.new(default_config(::Rails.root || Pathname.new(Dir.pwd)))
87
155
  yield(@config) if block_given?
88
156
  @config
89
157
  end
90
158
 
91
159
  # Public: Generates code for the specified routes with { export: true }.
92
160
  def generate!(app_or_routes = Rails.application)
93
- raise ArgumentError, 'A Rails app must be defined, or you must specify a custom `output_folder`' if config.output_folder.blank?
161
+ raise ArgumentError, "A Rails app must be defined, or you must specify a custom `output_folder`" if config.output_folder.blank?
94
162
  rails_routes = app_or_routes.is_a?(::Rails::Engine) ? app_or_routes.routes.routes : app_or_routes
95
- @compiled_template = nil # Clear on every code reload in case the template changed.
96
- exported_routes_by_controller(rails_routes).each do |controller, controller_routes|
97
- routes = Routes.new(controller_routes, config)
98
- write_if_changed(filename_for(controller), routes.cache_key) { render_template(routes) }
99
- end
163
+ generate_files exported_routes_by_controller(rails_routes)
100
164
  end
101
165
 
102
- private
166
+ private
103
167
 
104
- # Internal: Returns exported routes grouped by controller name.
105
- def exported_routes_by_controller(routes)
106
- routes.select { |route|
107
- route.defaults.fetch(:export, false)
108
- }.group_by { |route|
109
- route.requirements.fetch(:controller)
168
+ def generate_files(exported_routes)
169
+ template = Template.new(config.template_path)
170
+ generate_file_for_all exported_routes.map { |controller, routes|
171
+ ControllerRoutes.new(controller, routes, config).tap do |routes|
172
+ template.write_if_changed routes
173
+ end
110
174
  }
111
175
  end
112
176
 
113
- # Internal: Name of the JS file with helpers for the the given controller.
114
- def filename_for(controller)
115
- config.output_folder.join("#{ controller.camelize }#{ config.file_suffix }".tr_s(':', '/'))
116
- end
177
+ def generate_file_for_all(routes)
178
+ return unless config.all_helpers_file && !routes.empty?
117
179
 
118
- # Internal: Returns a String with the JS generated for a controller's routes.
119
- def render_template(routes)
120
- routes.evaluate(compiled_template)
180
+ Template.new(config.template_all_path).write_if_changed AllRoutes.new(routes, config)
121
181
  end
122
182
 
123
- # Internal: Returns the compiled ERB to generate JS from a set of routes.
124
- def compiled_template
125
- @compiled_template ||= begin
126
- template = File.read(config.template_path)
127
- Erubi::Engine.new(template, filename: config.template_path).src
128
- end
183
+ def default_config(root)
184
+ dir = %w[frontend packs javascript assets].find { |dir| root.join("app", dir).exist? }
185
+ {
186
+ all_helpers_file: true,
187
+ client_library: "@js-from-routes/client",
188
+ file_suffix: "Api.js",
189
+ helper_mappings: {"index" => "list", "show" => "get"},
190
+ output_folder: root.join("app", dir, "api"),
191
+ template_path: File.expand_path("template.js.erb", __dir__),
192
+ template_all_path: File.expand_path("template_all.js.erb", __dir__),
193
+ }
129
194
  end
130
195
 
131
- # Internal: Writes if the file does not exist or the cache key has changed.
132
- # The cache strategy consists of a comment on the first line of the file.
133
- #
134
- # Yields to receive the rendered file content when it needs to.
135
- def write_if_changed(name, cache_key)
136
- FileUtils.mkdir_p(name.dirname)
137
- cache_key_comment = "// JsFromRoutes CacheKey #{ cache_key }\n"
138
- File.open(name, 'a+') { |file|
139
- if file.gets != cache_key_comment
140
- file.truncate(0)
141
- file.write(cache_key_comment)
142
- file.write(yield)
143
- end
196
+ # Internal: Returns exported routes grouped by controller name.
197
+ def exported_routes_by_controller(routes)
198
+ routes.select { |route|
199
+ route.defaults.fetch(:export, false)
200
+ }.group_by { |route|
201
+ route.requirements.fetch(:controller)
144
202
  }
145
203
  end
146
204
  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 'js_from_routes.reloader' do |app|
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 'Generates JavaScript files from Rails routes, one file per controller, and one function per route.'
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
@@ -1,22 +1,9 @@
1
1
  //
2
2
  // DO NOT MODIFY: This file was automatically generated by JsFromRoutes.
3
- <%
4
- if routes.any?(&:path_only?)
5
- %>import { formatUrl } from '@/helpers/UrlHelper'<%
6
- end
7
- %><% if routes.any?(&:path_only?) && routes.any?(&:request_method?) %><%= "\n" %><% end %><%
8
- if routes.any?(&:request_method?)
9
- %>import { request } from '@/services/ApiService'<%
10
- end
11
- %>
3
+ import { definePathHelper } from '<%= client_library %>'
12
4
 
13
5
  export default {
14
- <% routes.each_with_index do |route, index| %>
15
- <% if index > 0 %><%= "\n" %><% end
16
- %> <%= route.helper %>: options =>
17
- <% if route.path_only?
18
- %>formatUrl(<% else
19
- %>request('<%= route.verb %>', <% end
20
- %>'<%= route.path %>', options),
6
+ <% routes.each do |route| %>
7
+ <%= route.helper %>: definePathHelper('<%= route.verb %>', '<%= route.path %>'),
21
8
  <% end %>
22
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
+ }
@@ -4,5 +4,5 @@
4
4
  # Generates one file per controller, and one function per route.
5
5
  module JsFromRoutes
6
6
  # Public: This library adheres to semantic versioning.
7
- VERSION = '1.0.2'
7
+ VERSION = "2.0.3"
8
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: 1.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Máximo Mussini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-10 00:00:00.000000000 Z
11
+ date: 2021-03-16 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: '0'
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: '0'
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: '0'
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: '0'
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: '0'
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.
@@ -63,37 +139,14 @@ extra_rdoc_files:
63
139
  - README.md
64
140
  files:
65
141
  - CHANGELOG.md
142
+ - LICENSE.txt
66
143
  - README.md
67
144
  - lib/js_from_routes.rb
68
145
  - lib/js_from_routes/generator.rb
69
146
  - lib/js_from_routes/railtie.rb
70
147
  - lib/js_from_routes/template.js.erb
148
+ - lib/js_from_routes/template_all.js.erb
71
149
  - lib/js_from_routes/version.rb
72
- - spec/js_from_routes/js_from_routes_spec.rb
73
- - spec/spec_helper.rb
74
- - spec/support/sample_app/app/controllers/application_controller.rb
75
- - spec/support/sample_app/app/controllers/comments_controller.rb
76
- - spec/support/sample_app/app/controllers/user_preferences_controller.rb
77
- - spec/support/sample_app/app/controllers/video_clips_controller.rb
78
- - spec/support/sample_app/app/controllers/welcome_controller.rb
79
- - spec/support/sample_app/app/mailers/application_mailer.rb
80
- - spec/support/sample_app/config/application.rb
81
- - spec/support/sample_app/config/boot.rb
82
- - spec/support/sample_app/config/environment.rb
83
- - spec/support/sample_app/config/environments/development.rb
84
- - spec/support/sample_app/config/environments/test.rb
85
- - spec/support/sample_app/config/initializers/application_controller_renderer.rb
86
- - spec/support/sample_app/config/initializers/assets.rb
87
- - spec/support/sample_app/config/initializers/backtrace_silencers.rb
88
- - spec/support/sample_app/config/initializers/content_security_policy.rb
89
- - spec/support/sample_app/config/initializers/cookies_serializer.rb
90
- - spec/support/sample_app/config/initializers/filter_parameter_logging.rb
91
- - spec/support/sample_app/config/initializers/inflections.rb
92
- - spec/support/sample_app/config/initializers/mime_types.rb
93
- - spec/support/sample_app/config/initializers/wrap_parameters.rb
94
- - spec/support/sample_app/config/puma.rb
95
- - spec/support/sample_app/config/routes.rb
96
- - spec/support/sample_app/node_modules/node-sass/src/libsass/extconf.rb
97
150
  homepage: https://github.com/ElMassimo/js_from_routes
98
151
  licenses:
99
152
  - MIT
@@ -117,29 +170,4 @@ rubygems_version: 3.1.4
117
170
  signing_key:
118
171
  specification_version: 4
119
172
  summary: Generate JS automatically from Rails routes.
120
- test_files:
121
- - spec/spec_helper.rb
122
- - spec/js_from_routes/js_from_routes_spec.rb
123
- - spec/support/sample_app/app/mailers/application_mailer.rb
124
- - spec/support/sample_app/app/controllers/video_clips_controller.rb
125
- - spec/support/sample_app/app/controllers/application_controller.rb
126
- - spec/support/sample_app/app/controllers/comments_controller.rb
127
- - spec/support/sample_app/app/controllers/welcome_controller.rb
128
- - spec/support/sample_app/app/controllers/user_preferences_controller.rb
129
- - spec/support/sample_app/config/routes.rb
130
- - spec/support/sample_app/config/environments/development.rb
131
- - spec/support/sample_app/config/environments/test.rb
132
- - spec/support/sample_app/config/environment.rb
133
- - spec/support/sample_app/config/application.rb
134
- - spec/support/sample_app/config/puma.rb
135
- - spec/support/sample_app/config/boot.rb
136
- - spec/support/sample_app/config/initializers/application_controller_renderer.rb
137
- - spec/support/sample_app/config/initializers/backtrace_silencers.rb
138
- - spec/support/sample_app/config/initializers/mime_types.rb
139
- - spec/support/sample_app/config/initializers/filter_parameter_logging.rb
140
- - spec/support/sample_app/config/initializers/wrap_parameters.rb
141
- - spec/support/sample_app/config/initializers/assets.rb
142
- - spec/support/sample_app/config/initializers/cookies_serializer.rb
143
- - spec/support/sample_app/config/initializers/content_security_policy.rb
144
- - spec/support/sample_app/config/initializers/inflections.rb
145
- - 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/jquery_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,7 +0,0 @@
1
- require 'simplecov'
2
- SimpleCov.start { add_filter '/spec/' }
3
-
4
- require 'rails'
5
- require 'js_from_routes'
6
- require 'rspec/given'
7
- require 'pry-byebug'
@@ -1,2 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- end
@@ -1,2 +0,0 @@
1
- class CommentsController < ApplicationController
2
- end
@@ -1,2 +0,0 @@
1
- class UserPreferencesController < ApplicationController
2
- end
@@ -1,2 +0,0 @@
1
- class VideoClipsController < ApplicationController
2
- end
@@ -1,2 +0,0 @@
1
- class WelcomeController < ApplicationController
2
- end
@@ -1,4 +0,0 @@
1
- class ApplicationMailer < ActionMailer::Base
2
- default from: 'from@example.com'
3
- layout 'mailer'
4
- end
@@ -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,4 +0,0 @@
1
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
2
-
3
- require 'bundler/setup' # Set up gems listed in the Gemfile.
4
- require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
@@ -1,5 +0,0 @@
1
- # Load the Rails application.
2
- require_relative 'application'
3
-
4
- # Initialize the Rails application.
5
- Rails.application.initialize!
@@ -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,5 +0,0 @@
1
- Rails.application.configure do
2
- # Settings specified here will take precedence over those in config/application.rb.
3
- config.cache_classes = true
4
- config.eager_load = true
5
- end
@@ -1,8 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # ActiveSupport::Reloader.to_prepare do
4
- # ApplicationController.renderer.defaults.merge!(
5
- # http_host: 'example.org',
6
- # https: false
7
- # )
8
- # 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,5 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Specify a serializer for the signed and encrypted cookie jars.
4
- # Valid options are :json, :marshal, and :hybrid.
5
- Rails.application.config.action_dispatch.cookies_serializer = :json
@@ -1,4 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Configure sensitive parameters which will be filtered from the log file.
4
- Rails.application.config.filter_parameters += [:password]
@@ -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,4 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Add new mime types for use in respond_to blocks:
4
- # Mime::Type.register "text/richtext", :rtf
@@ -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
@@ -1,6 +0,0 @@
1
- require 'mkmf'
2
- # .. more stuff
3
- #$LIBPATH.push(Config::CONFIG['libdir'])
4
- $CFLAGS << " #{ENV["CFLAGS"]}"
5
- $LIBS << " #{ENV["LIBS"]}"
6
- create_makefile("libsass")