js_from_routes 1.0.3 → 2.0.0

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.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -4
  3. data/README.md +4 -156
  4. data/lib/js_from_routes/generator.rb +20 -19
  5. data/lib/js_from_routes/template.js.erb +2 -15
  6. data/lib/js_from_routes/version.rb +1 -1
  7. metadata +3 -49
  8. data/spec/js_from_routes/js_from_routes_spec.rb +0 -78
  9. data/spec/spec_helper.rb +0 -7
  10. data/spec/support/sample_app/app/controllers/application_controller.rb +0 -2
  11. data/spec/support/sample_app/app/controllers/comments_controller.rb +0 -2
  12. data/spec/support/sample_app/app/controllers/user_preferences_controller.rb +0 -2
  13. data/spec/support/sample_app/app/controllers/video_clips_controller.rb +0 -17
  14. data/spec/support/sample_app/app/controllers/welcome_controller.rb +0 -2
  15. data/spec/support/sample_app/app/mailers/application_mailer.rb +0 -4
  16. data/spec/support/sample_app/config/application.rb +0 -15
  17. data/spec/support/sample_app/config/boot.rb +0 -3
  18. data/spec/support/sample_app/config/environment.rb +0 -5
  19. data/spec/support/sample_app/config/environments/development.rb +0 -25
  20. data/spec/support/sample_app/config/environments/test.rb +0 -5
  21. data/spec/support/sample_app/config/initializers/application_controller_renderer.rb +0 -8
  22. data/spec/support/sample_app/config/initializers/backtrace_silencers.rb +0 -7
  23. data/spec/support/sample_app/config/initializers/content_security_policy.rb +0 -37
  24. data/spec/support/sample_app/config/initializers/cookies_serializer.rb +0 -5
  25. data/spec/support/sample_app/config/initializers/filter_parameter_logging.rb +0 -4
  26. data/spec/support/sample_app/config/initializers/inflections.rb +0 -16
  27. data/spec/support/sample_app/config/initializers/mime_types.rb +0 -4
  28. data/spec/support/sample_app/config/initializers/wrap_parameters.rb +0 -14
  29. data/spec/support/sample_app/config/puma.rb +0 -38
  30. data/spec/support/sample_app/config/routes.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11da4a678c4081b4ce6d99bac78e007f819cf5fecd2321af94c2e7962e557a71
4
- data.tar.gz: 3c81aa21374aae94ec47493f70ed978938061a9d1a03ca9bba481939e853c7ab
3
+ metadata.gz: 120fa96855c034323a664b851019f5d1df7bd418de5578af5fa074b80451ca3e
4
+ data.tar.gz: 7a39747149294d21f74568296c4cab44f2e93ea172c85bad0d721e686c16ce74
5
5
  SHA512:
6
- metadata.gz: 8d71a098de83a7feab5bae4ce682c981a32d9d296db8392dc441371ce4f53b3237beefc155f670e69960af30d6780144bd426f6727fcf98d3a7260ac63c7fc12
7
- data.tar.gz: 70347defa686e95bae4264eda364e514eef9fa623078957a17825dc1c8894f77c0079b19c70f001586344980c5fe29c57812aaef0f74ba2ac8b92f910c39dc5e
6
+ metadata.gz: 77d1b1b6bef1a59d68bc1452dcb8ade3de38123ae907687bab9017347897a440b6f8b57ce6874f46d8873c6fcddf0e0480e38aa46a02fd91c39600c5c9980eca
7
+ data.tar.gz: 993bf91cfd887d1c645313b305f2f5d1c095ed31f3232d34b7dcbf575eefafdbadaeb3fb6948e00f1b1614e8f38e86217dd6c5bb2c55c508df771584441bde53
data/CHANGELOG.md CHANGED
@@ -1,18 +1,34 @@
1
- ## JsFromRoutes 1.0.3 (2021-03-10) ##
1
+ # [2.0.0](https://github.com/ElMassimo/js_from_routes/compare/v1.0.3...js_from_routes@2.0.0) (2021-03-13)
2
+
3
+ ### Features ⚡️
4
+
5
+ - Path helpers now support both making a request or obtaining the path
6
+ - Auto-detect the folder where javascript files live (`frontend`, `packs`, `javascript`, or `assets`)
7
+ - The new default template is more flexible and can be easily customized using the `client_library` setting
8
+
9
+ ### Breaking Changes 💥
10
+
11
+ - Created client libraries that provide the necessary functionality out of the box (optional)
12
+ - Changed the default template, now using `definePathHelper`
13
+ - Defaults for code generation have changed (`Requests` → `Api`)
14
+ - `path_only?` and `request_method?` have been removed from `Route` during code generation
15
+ - You may still check for it in your custom templates, using `route.export == :path_only`
16
+
17
+ ## 1.0.3 (2021-03-10) ##
2
18
 
3
19
  * Use `~/` instead of `@/` in the default template imports.
4
20
 
5
21
 
6
- ## JsFromRoutes 1.0.2 (2021-03-10) ##
22
+ ## 1.0.2 (2021-03-10) ##
7
23
 
8
24
  * Ensure a default `template.js.erb` ships with the gem.
9
25
 
10
26
 
11
- ## JsFromRoutes 1.0.1 (2020-06-21) ##
27
+ ## 1.0.1 (2020-06-21) ##
12
28
 
13
29
  * Expose `export_setting` in the `Route` API, to support custom workflows.
14
30
 
15
31
 
16
- ## JsFromRoutes 1.0.0 (2020-06-21) ##
32
+ ## 1.0.0 (2020-06-21) ##
17
33
 
18
34
  * Initial Release.
data/README.md CHANGED
@@ -6,33 +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
13
  [Vite Rails]: https://vite-ruby.netlify.app/
14
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
15
17
 
16
- _JS from Routes_ helps you by automatically generating path and API helpers from
17
- Rails route definitions, allowing you to save development effort and focus on
18
- the things that matter.
19
-
20
- Check out [this pull request](https://github.com/ElMassimo/pingcrm-vite/pull/2) to get a sense of how flexible it can be.
21
-
22
- ### Why? 🤔
23
-
24
- Path helpers in Rails are useful, and make it easier to build urls, avoiding
25
- typos and mistakes.
26
-
27
- With this library, it's possible the enjoy the same benefits in JS:
28
-
29
- - No need to manually specify the URL, preventing mistakes and saving development time.
30
- - If an action is renamed or removed, the helper ceases to exist, which causes
31
- an error that is easier to detect than a 404.
32
- - We can embed the the HTTP verb in the helper. Changing the verb in the route causes the JS
33
- code to be regenerated, no need to update the consumer!
34
-
35
- Read more about it in the [blog announcement](https://maximomussini.com/posts/js-from-routes/).
18
+ For more information, check the main [README].
36
19
 
37
20
  ### Installation 💿
38
21
 
@@ -49,138 +32,3 @@ And then execute:
49
32
  Or install it yourself as:
50
33
 
51
34
  $ gem install js_from_routes
52
-
53
- ### Usage 🚀
54
-
55
- #### 1. Specify the Rails routes you want to export
56
-
57
- 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.
58
-
59
- ```ruby
60
- Rails.application.routes.draw do
61
- resources :video_clips, export: true do
62
- get :download, on: :member, export: :path_only
63
- get :trending, on: :collection, export: false
64
- end
65
- end
66
- ```
67
-
68
- #### 2. Generate JS code from your routes
69
-
70
- This is usually done automatically the next time you make a request to your
71
- Rails server (such as when you refresh the page), which causes Rails reloader to
72
- kick in, and the routes to be generated.
73
-
74
- If you are not running a local development server, or prefer to do it manually,
75
- you can use a rake task instead:
76
-
77
- ```
78
- bin/rake js_from_routes:generate
79
- ```
80
-
81
- which can generate code such as:
82
-
83
- ```js
84
- import { formatUrl } from '~/helpers/UrlHelper'
85
- import { request } from '~/services/ApiService'
86
-
87
- export default {
88
- downloadPath: options =>
89
- formatUrl('/video_clips/:id/download', options),
90
-
91
- get: options =>
92
- request('get', '/video_clips/:id', options),
93
-
94
- update: options =>
95
- request('patch', '/video_clips/:id', options),
96
- }
97
- ```
98
-
99
- #### 3. Use the generated code in your JS application
100
-
101
- 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`:
102
-
103
- ```js
104
- import VideoClipsRequests from '@requests/VideoClipsRequests'
105
-
106
- VideoClipsRequests.get({ id: 'oHg5SJYRHA0' }).then(data => { this.video = data })
107
-
108
- const newVideo = { ...this.video, format: '.mp4' }
109
- VideoClipsRequests.update(newVideo)
110
-
111
- const path = VideoClipsRequests.downloadPath(newVideo)
112
- ```
113
-
114
- 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 it to your convenience.
115
-
116
- Read on to find out how to customize the generated code to suit your needs.
117
-
118
- ### Advanced Configuration 📖
119
-
120
- Since all projects are different, it's very unlikely that the default settings
121
- fulfill all your requirements.
122
-
123
- The following [settings](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L77-L80) are available:
124
-
125
- ##### [`file_suffix`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L77), default: `Requests.js`
126
-
127
- This suffix is added by default to all generated files. You can modify it to
128
- if you prefer a different convention, or if you use it to generate TypeScript.
129
-
130
- ##### [`helper_mappings`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L80)
131
-
132
- By default it maps `index` to `list` and `show` to `get`, which helps to make
133
- the JS code read more naturally.
134
-
135
- ##### [`output_folder`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L78), default: `app/javascript/requests`
136
-
137
- The directory where the generated files are created.
138
-
139
- Tip: It's highly recommended to [add a webpack alias](https://github.com/ElMassimo/js_from_routes/blob/webpack/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).
140
-
141
- If you use [Vite Rails], the [aliases] are already configured for you.
142
-
143
- ##### [`template_path`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L79)
144
-
145
- 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).
146
-
147
- You will probably want to use a custom template, such as:
148
-
149
- ```ruby
150
- # config/initializers/js_from_routes.rb
151
- if Rails.env.development?
152
- JsFromRoutes.config do |config|
153
- config.template_path = Rails.root.join('app', 'views', 'custom_js_from_routes.js.erb')
154
- end
155
- end
156
- ```
157
-
158
- A `routes` variable will be available in the template, which will contain the
159
- endpoints exported for a controller.
160
-
161
- Each `route` exposes properties such as `verb` and `path`, please [check the
162
- 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).
163
-
164
- ### How does it work? ⚙️
165
-
166
- By adding a hook to Rails' reload process in development, it's possible to
167
- automatically generate files from routes when a route is added, modified, or removed.
168
-
169
- In order to optimize file generation, the generated JS files are split by
170
- controller, and add a cache key based on the routes to avoid rewriting the file
171
- if the route definition hasn't changed.
172
-
173
- When the Vite.js or Webpack development server is running, it detects when a new
174
- file is generated, automatically triggering a new build, which can now use the
175
- generated request methods or path helpers 😃
176
-
177
- ### Take this idea 💡
178
-
179
- While the original use cases intended to generate code that targes a custom `ApiService`,
180
- it can be tweaked to generate TypeScript, [target jQuery](https://gist.github.com/ElMassimo/cab56e64e20ff797f3054b661a883646),
181
- 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).
182
-
183
- There are plenty of other opportunities for automatic code generation, such as keeping
184
- enums in sync between Ruby and JS.
185
-
186
- Let me know if you come up with new or creative ways to use this technique 😃
@@ -3,6 +3,7 @@
3
3
  require "digest"
4
4
  require "erubi"
5
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.
@@ -24,6 +25,11 @@ module JsFromRoutes
24
25
  Digest::MD5.hexdigest(routes.map(&:inspect).join + [File.read(@config.template_path), @config.helper_mappings.inspect].join)
25
26
  end
26
27
 
28
+ # Public: Exposes the preferred import library to the generator.
29
+ def client_library
30
+ @config.client_library
31
+ end
32
+
27
33
  # Internal: By performing the evaluation here, we ensure only "routes" is
28
34
  # exposed to the ERB template as a local variable.
29
35
  def evaluate(compiled_template)
@@ -37,21 +43,11 @@ module JsFromRoutes
37
43
  @route, @mappings = route, mappings
38
44
  end
39
45
 
40
- # Public: Whether it should export only the path.
41
- def path_only?
42
- export_setting == :path_only
43
- end
44
-
45
46
  # Public: The `export` setting specified for the action.
46
- def export_setting
47
+ def export
47
48
  @route.defaults[:export]
48
49
  end
49
50
 
50
- # Public: Whether it should export only the path.
51
- def request_method?
52
- !path_only?
53
- end
54
-
55
51
  # Public: The HTTP verb for the action. Example: 'patch'
56
52
  def verb
57
53
  @route.verb.downcase
@@ -65,8 +61,7 @@ module JsFromRoutes
65
61
  # Public: The name of the JS helper for the action. Example: 'destroyAll'
66
62
  def helper
67
63
  action = @route.requirements.fetch(:action).camelize(:lower)
68
- name = @mappings.fetch(action, action)
69
- path_only? ? "#{name}Path" : name
64
+ @mappings.fetch(action, action)
70
65
  end
71
66
 
72
67
  # Internal: Useful as a cache key for the route, and for debugging purposes.
@@ -78,12 +73,7 @@ module JsFromRoutes
78
73
  class << self
79
74
  # Public: Configuration of the code generator.
80
75
  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
- )
76
+ @config ||= OpenStruct.new(default_config(::Rails.root || Pathname.new(Dir.pwd)))
87
77
  yield(@config) if block_given?
88
78
  @config
89
79
  end
@@ -101,6 +91,17 @@ module JsFromRoutes
101
91
 
102
92
  private
103
93
 
94
+ def default_config(root)
95
+ dir = %w[frontend packs javascript assets].find { |dir| root.join("app", dir).exist? }
96
+ {
97
+ client_library: "@js-from-routes/client",
98
+ file_suffix: "Api.js",
99
+ helper_mappings: {"index" => "list", "show" => "get"},
100
+ output_folder: root.join("app", dir, "api"),
101
+ template_path: File.expand_path("template.js.erb", __dir__),
102
+ }
103
+ end
104
+
104
105
  # Internal: Returns exported routes grouped by controller name.
105
106
  def exported_routes_by_controller(routes)
106
107
  routes.select { |route|
@@ -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
6
  <% 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),
7
+ <%= route.helper %>: definePathHelper('<%= route.verb %>', '<%= route.path %>'),
21
8
  <% end %>
22
9
  }
@@ -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.3"
7
+ VERSION = "2.0.0"
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.3
4
+ version: 2.0.0
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-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -145,29 +145,6 @@ files:
145
145
  - lib/js_from_routes/railtie.rb
146
146
  - lib/js_from_routes/template.js.erb
147
147
  - lib/js_from_routes/version.rb
148
- - spec/js_from_routes/js_from_routes_spec.rb
149
- - spec/spec_helper.rb
150
- - spec/support/sample_app/app/controllers/application_controller.rb
151
- - spec/support/sample_app/app/controllers/comments_controller.rb
152
- - spec/support/sample_app/app/controllers/user_preferences_controller.rb
153
- - spec/support/sample_app/app/controllers/video_clips_controller.rb
154
- - spec/support/sample_app/app/controllers/welcome_controller.rb
155
- - spec/support/sample_app/app/mailers/application_mailer.rb
156
- - spec/support/sample_app/config/application.rb
157
- - spec/support/sample_app/config/boot.rb
158
- - spec/support/sample_app/config/environment.rb
159
- - spec/support/sample_app/config/environments/development.rb
160
- - spec/support/sample_app/config/environments/test.rb
161
- - spec/support/sample_app/config/initializers/application_controller_renderer.rb
162
- - spec/support/sample_app/config/initializers/backtrace_silencers.rb
163
- - spec/support/sample_app/config/initializers/content_security_policy.rb
164
- - spec/support/sample_app/config/initializers/cookies_serializer.rb
165
- - spec/support/sample_app/config/initializers/filter_parameter_logging.rb
166
- - spec/support/sample_app/config/initializers/inflections.rb
167
- - spec/support/sample_app/config/initializers/mime_types.rb
168
- - spec/support/sample_app/config/initializers/wrap_parameters.rb
169
- - spec/support/sample_app/config/puma.rb
170
- - spec/support/sample_app/config/routes.rb
171
148
  homepage: https://github.com/ElMassimo/js_from_routes
172
149
  licenses:
173
150
  - MIT
@@ -191,27 +168,4 @@ rubygems_version: 3.1.4
191
168
  signing_key:
192
169
  specification_version: 4
193
170
  summary: Generate JS automatically from Rails routes.
194
- test_files:
195
- - spec/spec_helper.rb
196
- - spec/js_from_routes/js_from_routes_spec.rb
197
- - spec/support/sample_app/app/mailers/application_mailer.rb
198
- - spec/support/sample_app/app/controllers/video_clips_controller.rb
199
- - spec/support/sample_app/app/controllers/application_controller.rb
200
- - spec/support/sample_app/app/controllers/comments_controller.rb
201
- - spec/support/sample_app/app/controllers/welcome_controller.rb
202
- - spec/support/sample_app/app/controllers/user_preferences_controller.rb
203
- - spec/support/sample_app/config/routes.rb
204
- - spec/support/sample_app/config/environments/development.rb
205
- - spec/support/sample_app/config/environments/test.rb
206
- - spec/support/sample_app/config/environment.rb
207
- - spec/support/sample_app/config/application.rb
208
- - spec/support/sample_app/config/puma.rb
209
- - spec/support/sample_app/config/boot.rb
210
- - spec/support/sample_app/config/initializers/application_controller_renderer.rb
211
- - spec/support/sample_app/config/initializers/backtrace_silencers.rb
212
- - spec/support/sample_app/config/initializers/mime_types.rb
213
- - spec/support/sample_app/config/initializers/filter_parameter_logging.rb
214
- - spec/support/sample_app/config/initializers/wrap_parameters.rb
215
- - spec/support/sample_app/config/initializers/cookies_serializer.rb
216
- - spec/support/sample_app/config/initializers/content_security_policy.rb
217
- - spec/support/sample_app/config/initializers/inflections.rb
171
+ test_files: []
@@ -1,78 +0,0 @@
1
- require "support/sample_app/config/application"
2
- require "support/sample_app/config/routes"
3
-
4
- describe JsFromRoutes do
5
- original_template_path = JsFromRoutes.config.template_path
6
-
7
- let(:output_dir) { Pathname.new File.expand_path("../support/generated", __dir__) }
8
- let(:sample_dir) { Rails.root.join("app", "javascript", "requests") }
9
- let(:different_template_path) { File.expand_path("../support/jquery_template.js.erb", __dir__) }
10
- let(:controllers_with_exported_routes) { %w[Comments UserPreferences VideoClips] }
11
-
12
- def file_for(dir, name)
13
- dir.join("#{name}Requests.js")
14
- end
15
-
16
- def sample_file_for(name)
17
- file_for(sample_dir, name)
18
- end
19
-
20
- def output_file_for(name)
21
- file_for(output_dir, name)
22
- end
23
-
24
- before do
25
- # Sanity checks
26
- expect(sample_dir.exist?).to eq true
27
- expect(Rails.application.routes.routes).to be_present
28
-
29
- # Remove directory from a previous test run.
30
- begin
31
- FileUtils.remove_dir(output_dir)
32
- rescue
33
- nil
34
- end
35
-
36
- # Change the configuration to use a different directory.
37
- JsFromRoutes.config do |config|
38
- config.output_folder = output_dir
39
- config.template_path = original_template_path
40
- end
41
- end
42
-
43
- # NOTE: We do a manual snapshot test for now, more tests coming in the future.
44
- it "should generate the files as expected" do
45
- expect(JsFromRoutes).to receive(:render_template).exactly(3).times.and_call_original
46
- JsFromRoutes.generate!
47
-
48
- # It does not generate routes that don't have `export: true`.
49
- expect(output_file_for("Welcome").exist?).to eq false
50
-
51
- # It generates one file per controller with exported routes.
52
- controllers_with_exported_routes.each do |file_name|
53
- expect(output_file_for(file_name).read).to eq sample_file_for(file_name).read
54
- end
55
-
56
- # It detects changes to the template, and regenerates the files.
57
- JsFromRoutes.config do |config|
58
- config.template_path = different_template_path
59
- end
60
- expect(JsFromRoutes).to receive(:render_template).exactly(3).times.and_call_original
61
- JsFromRoutes.generate!
62
-
63
- # These files should no longer match the sample ones.
64
- controllers_with_exported_routes.each do |file_name|
65
- expect(output_file_for(file_name).read).not_to eq sample_file_for(file_name).read
66
- end
67
-
68
- # It should not rewrite the files if the cache key has not changed.
69
- expect(JsFromRoutes).not_to receive(:render_template)
70
- JsFromRoutes.generate!
71
- end
72
-
73
- it "should have a rake task available" do
74
- Rails.application.load_tasks
75
- expect(JsFromRoutes).to receive(:render_template).exactly(3).times
76
- expect { Rake::Task["js_from_routes:generate"].invoke }.not_to raise_error
77
- end
78
- 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,17 +0,0 @@
1
- class VideoClipsController < ApplicationController
2
- def trending
3
- render json: [
4
- {title: "Smoke Signals"},
5
- {title: "Camino Libre"},
6
- {title: "Sin Querer"},
7
- {title: "Tabula Rasa"},
8
- {title: "Raindance"},
9
- {title: "Ragamuffin"},
10
- {title: "Vals Venezolano Nº 2"},
11
- {title: "Xaranga do Vovô"},
12
- {title: "Café 1930"},
13
- {title: "Milonga (Uruguay)"},
14
- {title: "Divagando"}
15
- ]
16
- end
17
- 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,15 +0,0 @@
1
- require_relative "boot"
2
-
3
- require "action_controller/railtie"
4
-
5
- # Require the gems listed in Gemfile, including any gems
6
- # you've limited to :test, :development, or :production.
7
- Bundler.require(*Rails.groups)
8
-
9
- module SampleApp
10
- class Application < Rails::Application
11
- # Application configuration can go into files in config/initializers
12
- # -- all .rb files in that directory are automatically loaded after loading
13
- # the framework and any gems in your application.
14
- end
15
- end
@@ -1,3 +0,0 @@
1
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
2
-
3
- require "bundler/setup" # Set up gems listed in the Gemfile.
@@ -1,5 +0,0 @@
1
- # Load the Rails application.
2
- require_relative "application"
3
-
4
- # Initialize the Rails application.
5
- Rails.application.initialize!
@@ -1,25 +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
- # Raises error for missing translations.
20
- # config.action_view.raise_on_missing_translations = true
21
-
22
- # Use an evented file watcher to asynchronously detect changes in source code,
23
- # routes, locales, etc. This feature depends on the listen gem.
24
- config.file_watcher = ActiveSupport::EventedFileUpdateChecker
25
- 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,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,37 +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
- # Allow @vite/client to hot reload changes in development
14
- # policy.script_src *policy.script_src, :unsafe_eval, "http://localhost:3036" if Rails.env.development?
15
-
16
- # You may need to enable this in production as well depending on your setup.
17
- # policy.script_src *policy.script_src, :blob if Rails.env.test?
18
- # policy.style_src :self, :https
19
- # # If you are using webpack-dev-server then specify webpack-dev-server host
20
- # policy.connect_src :self, :https, "http://localhost:3035", "ws://localhost:3035" if Rails.env.development?
21
- # Allow @vite/client to hot reload changes in development
22
- # policy.connect_src *policy.connect_src, "ws://#{ ViteRuby.config.host_with_port }" if Rails.env.development?
23
-
24
- # # Specify URI for violation reports
25
- # # policy.report_uri "/csp-violation-report-endpoint"
26
- # end
27
-
28
- # If you are using UJS then enable automatic nonce generation
29
- # Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
30
-
31
- # Set the nonce only to specific directives
32
- # Rails.application.config.content_security_policy_nonce_directives = %w(script-src)
33
-
34
- # Report CSP violations to a specified URI
35
- # For further information see the following documentation:
36
- # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
37
- # 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