turbo_form 0.8.1 → 0.9.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc9794957819ec051bc06e1b0169a5584a2f183d46532a062ad1ddd056b43a3d
4
- data.tar.gz: 3cd694c43cb2cdd452b7a0e1a134699b162c7efc2853af9c17c053a40e658c74
3
+ metadata.gz: cb59d187ec74c47b4ed52d8067f54faa85997be5e5551d22771b12c4224017fe
4
+ data.tar.gz: 376442d6aaf5c3d403355bc2ba1f4c653d4794fdd5b22288aa3650c0a132e3b2
5
5
  SHA512:
6
- metadata.gz: 25d42edc3636bf6be5cd766461242a9723b1f1bbf591aaa9e347ca415598846ccabee52dd8ebcfe13ae6d72fb7ee5b7a0d08a185d0a47347cf334e5e93417d19
7
- data.tar.gz: f6ddefa9a45e55fe4e1cab8f13768773d649d1174d802aaa12ac8a8dd9b1673730f23d88c2dbc48bfcc1838e4e76dcd98bd0fdbc63165bdf0b25a5a4b6278501
6
+ metadata.gz: 344e3f436b6fa24fcbbcd6f65e04a0212ff588b77766f121e7941decfa4ad4bcaa2ef181cc295c60a4eb80af1b7f14390387c74ced7c8ac106c67de1ffca7bdb
7
+ data.tar.gz: 5eb9d4403823fe67d7eaaf438d429e46d28c33e6163fd144b8f7aecd4a3e4ea478bd9c59ce066196da8bb2b5c67c40588000361b271339ea50c3d4dbf9b9e7ec
data/README.md CHANGED
@@ -36,21 +36,12 @@ Then run the installer:
36
36
  rails generate turbo_form:install
37
37
  ```
38
38
 
39
- It includes `TurboForm::Controller` in your ApplicationController and declares
40
- the route concern:
41
-
42
- ```ruby
43
- class ApplicationController < ActionController::Base
44
- include TurboForm::Controller
45
- end
46
- ```
47
-
48
- ```ruby
49
- concern :turbo_form, TurboForm::Routes
50
- ```
39
+ It imports the script into `app/javascript/application.js`, adding the npm
40
+ package first if your app bundles with esbuild, Vite, Bun or webpack; see
41
+ [JavaScript](#javascript) for doing that by hand.
51
42
 
52
- Draw the concern on each resource with a dynamic form — the installer leaves
53
- which ones to you:
43
+ The engine does the rest. Draw the `:turbo_form` route concern on each resource
44
+ with a dynamic form:
54
45
 
55
46
  ```ruby
56
47
  resources :widgets, concerns: :turbo_form
@@ -58,11 +49,8 @@ resources :widgets, concerns: :turbo_form
58
49
 
59
50
  It draws a PATCH beside whichever of `new` and `edit` the resource has, so
60
51
  `resources :widgets, only: %i[new create], concerns: :turbo_form` gets only the
61
- one for `new`.
62
-
63
- It also imports the script into `app/javascript/application.js`, adding the npm
64
- package first if your app bundles with esbuild, Vite, Bun or webpack; see
65
- [JavaScript](#javascript) for doing that by hand.
52
+ one for `new`. Every controller inheriting from `ActionController::Base` already
53
+ knows how to answer it.
66
54
 
67
55
  ## The options
68
56
 
@@ -70,7 +58,7 @@ package first if your app bundles with esbuild, Vite, Bun or webpack; see
70
58
 
71
59
  `dynamic: true` makes the form dynamic: a trigger sends it to the `new` or `edit`
72
60
  page of its model — `edit` once the record is saved — which is where
73
- `TurboForm::Routes` listens.
61
+ the route concern listens.
74
62
 
75
63
  ### `dynamic_trigger:` on a field
76
64
 
@@ -2,9 +2,8 @@ require 'rails/generators'
2
2
 
3
3
  module TurboForm
4
4
  module Generators
5
- # Every app gets TurboForm::Controller, the route concern its resources opt
6
- # into and an import of the script. On importmap-rails the engine pins the
7
- # script itself; bundled apps get the npm package to import instead.
5
+ # Every app gets an import of the script. On importmap-rails the engine pins
6
+ # the script itself; bundled apps get the npm package to import instead.
8
7
  class InstallGenerator < Rails::Generators::Base
9
8
  LOCKFILES = {
10
9
  'yarn.lock' => 'yarn add', 'pnpm-lock.yaml' => 'pnpm add',
@@ -14,23 +13,11 @@ module TurboForm
14
13
  ENTRYPOINT = 'app/javascript/application.js'
15
14
 
16
15
  def install
17
- include_controller
18
- declare_route_concern
19
16
  install_package unless importmap?
20
17
  import_script
21
18
  end
22
19
 
23
20
  private
24
- def include_controller
25
- inject_into_class 'app/controllers/application_controller.rb', 'ApplicationController', " include TurboForm::Controller\n"
26
- end
27
-
28
- # Declared once, drawn on no resource: which forms are dynamic is the
29
- # app's call, one `concerns: :turbo_form` at a time.
30
- def declare_route_concern
31
- route 'concern :turbo_form, TurboForm::Routes'
32
- end
33
-
34
21
  def importmap? = Rails.root.join('config/importmap.rb').exist?
35
22
 
36
23
  def install_package
@@ -1,5 +1,5 @@
1
1
  module TurboForm
2
- # Included into the host's ApplicationController. A request that
2
+ # Included into ActionController::Base by the engine. A request that
3
3
  # TurboForm::Routes marks runs `new` or `edit` as usual, callbacks and all,
4
4
  # then assigns the form to what the action built just before it renders --
5
5
  # implicitly, or with its own `render layout: 'panel'`.
@@ -51,6 +51,14 @@ module TurboForm
51
51
  end
52
52
  end
53
53
 
54
+ initializer 'turbo_form.controller' do
55
+ ActiveSupport.on_load(:action_controller_base) { include TurboForm::Controller }
56
+ end
57
+
58
+ initializer 'turbo_form.routes' do
59
+ ActionDispatch::Routing::Mapper.prepend(TurboForm::Routes::Declared)
60
+ end
61
+
54
62
  # Deliberately eager rather than `ActiveSupport.on_load(:action_view)`: that
55
63
  # hook doesn't fire until Action View is first loaded, which in an app that
56
64
  # isn't eager loading is partway through rendering the first view -- late
@@ -3,7 +3,6 @@ module TurboForm
3
3
  # and `edit` it has, at the same paths and onto the same actions, marked so the
4
4
  # controller renders them from what the form currently holds.
5
5
  #
6
- # concern :turbo_form, TurboForm::Routes
7
6
  # resources :widgets, concerns: :turbo_form
8
7
  module Routes
9
8
  def self.call(mapper, _options = {})
@@ -12,5 +11,15 @@ module TurboForm
12
11
  mapper.new { mapper.patch :new, as: nil, defaults: { dynamic_form: true } } if actions.include?(:new)
13
12
  mapper.patch :edit, on: :member, as: nil, defaults: { dynamic_form: true } if actions.include?(:edit)
14
13
  end
14
+
15
+ # Rails keeps concerns per Mapper, and builds a fresh one for every `draw`,
16
+ # `prepend` and `append`, so declaring it once from the engine can't reach
17
+ # them all. An app's own `concern :turbo_form` still replaces this one.
18
+ module Declared
19
+ def initialize(...)
20
+ super
21
+ concern :turbo_form, TurboForm::Routes
22
+ end
23
+ end
15
24
  end
16
25
  end
@@ -1,3 +1,3 @@
1
1
  module TurboForm
2
- VERSION = '0.8.1'
2
+ VERSION = '0.9.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbo_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Cohen
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  requirements: []
112
- rubygems_version: 4.0.20
112
+ rubygems_version: 4.0.16
113
113
  specification_version: 4
114
114
  summary: Turbo-Stream backed dynamic forms, by convention.
115
115
  test_files: []