turbo_form 0.6.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ea880fbc0453b32344bf09706c5bc68c095de36fdbea018f00fdf4adc7acc03
4
- data.tar.gz: 49bf3d131c1a1179d8ce8b64772e23681f1c54332797bca9cd46589c4d0c90d3
3
+ metadata.gz: 4cb1af8f341d1112a85f86bc4fe28470e83f867994be6029a32fbd996da03677
4
+ data.tar.gz: 97afa78d913454ea096c0f988ecb85d99f7ebe7027f02f2e19cc40af534b624e
5
5
  SHA512:
6
- metadata.gz: 300aabd3ad77727c57ddf99980ce56036e4f5fdbf6c001a31908dcef1f0f53408b4100cf21fa1b88abfd663f2adab10dd14003d90e1915136231e62576b1e2d6
7
- data.tar.gz: 24ec9eabed896e9de6f14c71b70e3f6aa8bc299244fcf3858146b55bf5d78c76549e4e08acdaaa6c0f77b13b6713183fabc2c469347c3a59df7e7dd477ed6ff9
6
+ metadata.gz: 221fbbcd5e31c6191a208cefb92089f70094ef5709547cdb63984a4bb419342d7109dfe4001135853ee7380537b63d82a8cb2fef64af550d40a1a713dfa93f20
7
+ data.tar.gz: 2e7cd4ecec1194cc7605035e9a51c9dcd1542f3c45410c6497a74221e8ae6a037991650a6d928bdf230097af4cfbc0e7aaf73751326a912f291649b9e22221b4
data/README.md CHANGED
@@ -133,9 +133,9 @@ For a `WidgetsController`:
133
133
  - `new` and `edit` set `@widget`,
134
134
  - `widget_params` permits the form's fields — the same method `create` and
135
135
  `update` already use, so a trigger assigns nothing a save wouldn't,
136
- - `new` and `edit` leave rendering to Rails. The values are assigned just before
137
- Rails' implicit render, so an action that calls `render` itself renders its
138
- object as it built it, and one that redirects still redirects.
136
+ - `new` and `edit` render their own template, implicitly or with `render`
137
+ themselves — `render layout: 'panel'` is fine. The values are assigned just
138
+ before it renders; an action that redirects still redirects.
139
139
 
140
140
  ### Switching an STI subclass
141
141
 
@@ -26,7 +26,7 @@ class FormRefresh {
26
26
  async start() {
27
27
  this.requestStarted()
28
28
  try {
29
- const response = await Turbo.fetch(this.location, { method: "PATCH", body: new FormData(this.formElement), headers: this.target.headers })
29
+ const response = await Turbo.fetch(this.location, { method: "PATCH", body: new FormData(this.formElement), headers: { ...this.target.headers, "X-CSRF-Token": csrfToken() } })
30
30
 
31
31
  this.target.render(response.status, await response.text())
32
32
  } finally {
@@ -88,6 +88,13 @@ function targetFor(form) {
88
88
  return frame ? new FrameTarget(frame) : new PageTarget()
89
89
  }
90
90
 
91
+ // The form's own authenticity token is scoped to its action and method when
92
+ // per-form tokens are on, as they are by default, so it can't vouch for this
93
+ // PATCH. The page's token can, as it does for Turbo's own submissions.
94
+ function csrfToken() {
95
+ return document.querySelector('meta[name="csrf-token"]')?.content
96
+ }
97
+
91
98
  const triggerSelector = "[data-turbo-form-trigger]"
92
99
 
93
100
  // Shaped like Turbo's observers. One listener per event the page's triggers
@@ -1,22 +1,24 @@
1
1
  module TurboForm
2
2
  # Included into the host's ApplicationController. A request that
3
3
  # TurboForm::Routes marks runs `new` or `edit` as usual, callbacks and all,
4
- # then assigns the form to what the action built just before the implicit
5
- # render.
4
+ # then assigns the form to what the action built just before it renders --
5
+ # implicitly, or with its own `render layout: 'panel'`.
6
6
  #
7
- # Leans on the scaffold's conventions: the action builds `@<resource>`,
8
- # `<resource>_params` permits the form, and the action leaves rendering to Rails.
7
+ # Leans on the scaffold's conventions: the action builds `@<resource>` and
8
+ # `<resource>_params` permits the form.
9
9
  module Controller
10
+ # Public, as Rails' own is. ActionController::Base's public methods are never
11
+ # actions, so this one isn't either.
12
+ def render(...)
13
+ dynamic_form_assignment if dynamic_form?
14
+ super
15
+ end
16
+
10
17
  private
11
18
  def process_action(...)
12
19
  dynamic_form? ? discarding_writes { super } : super
13
20
  end
14
21
 
15
- def default_render
16
- dynamic_form_assignment if dynamic_form?
17
- super
18
- end
19
-
20
22
  def dynamic_form_assignment
21
23
  resource = turbo_form_resource.extend(TurboForm::Resource).dynamic_assign(turbo_form_resource_params)
22
24
  instance_variable_set(:"@#{turbo_form_resource_name}", resource)
@@ -1,3 +1,3 @@
1
1
  module TurboForm
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.1'
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.6.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Cohen