turbo-rails 1.1.1 → 1.3.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.
@@ -35,7 +35,7 @@ module Turbo::Streams::Broadcasts
35
35
 
36
36
  def broadcast_action_to(*streamables, action:, target: nil, targets: nil, **rendering)
37
37
  broadcast_stream_to(*streamables, content: turbo_stream_action_tag(action, target: target, targets: targets, template:
38
- rendering.delete(:content) || (rendering.any? ? render_format(:html, **rendering) : nil)
38
+ rendering.delete(:content) || rendering.delete(:html) || (rendering.any? ? render_format(:html, **rendering) : nil)
39
39
  ))
40
40
  end
41
41
 
@@ -1,6 +1,5 @@
1
1
  module Turbo::DriveHelper
2
- # Pages that are more likely than not to be a cache miss can skip turbo cache to avoid visual jitter.
3
- # Note: This requires a +yield :head+ provision in the application layout.
2
+ # Note: These helpers require a +yield :head+ provision in the layout.
4
3
  #
5
4
  # ==== Example
6
5
  #
@@ -10,7 +9,21 @@ module Turbo::DriveHelper
10
9
  # # app/views/trays/index.html.erb
11
10
  # <% turbo_exempts_page_from_cache %>
12
11
  # <p>Page that shouldn't be cached by Turbo</p>
12
+
13
+ # Pages that are more likely than not to be a cache miss can skip turbo cache to avoid visual jitter.
14
+ # Cannot be used along with +turbo_exempts_page_from_preview+.
13
15
  def turbo_exempts_page_from_cache
14
- provide :head, %(<meta name="turbo-cache-control" content="no-cache">).html_safe
16
+ provide :head, tag.meta(name: "turbo-cache-control", content: "no-cache")
17
+ end
18
+
19
+ # Specify that a cached version of the page should not be shown as a preview during an application visit.
20
+ # Cannot be used along with +turbo_exempts_page_from_cache+.
21
+ def turbo_exempts_page_from_preview
22
+ provide :head, tag.meta(name: "turbo-cache-control", content: "no-preview")
23
+ end
24
+
25
+ # Force the page, when loaded by Turbo, to be cause a full page reload.
26
+ def turbo_page_requires_reload
27
+ provide :head, tag.meta(name: "turbo-visit-control", content: "reload")
15
28
  end
16
29
  end
@@ -1,4 +1,6 @@
1
1
  module Turbo::Streams::ActionHelper
2
+ include ActionView::Helpers::TagHelper
3
+
2
4
  # Creates a `turbo-stream` tag according to the passed parameters. Examples:
3
5
  #
4
6
  # turbo_stream_action_tag "remove", target: "message_1"
@@ -9,15 +11,15 @@ module Turbo::Streams::ActionHelper
9
11
  #
10
12
  # turbo_stream_action_tag "replace", targets: "message_1", template: %(<div id="message_1">Hello!</div>)
11
13
  # # => <turbo-stream action="replace" targets="message_1"><template><div id="message_1">Hello!</div></template></turbo-stream>
12
- def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil)
13
- template = action.to_sym == :remove ? "" : "<template>#{template}</template>"
14
+ def turbo_stream_action_tag(action, target: nil, targets: nil, template: nil, **attributes)
15
+ template = action.to_sym == :remove ? "" : tag.template(template.to_s.html_safe)
14
16
 
15
17
  if target = convert_to_turbo_stream_dom_id(target)
16
- %(<turbo-stream action="#{action}" target="#{target}">#{template}</turbo-stream>).html_safe
18
+ tag.turbo_stream(template, **attributes.merge(action: action, target: target))
17
19
  elsif targets = convert_to_turbo_stream_dom_id(targets, include_selector: true)
18
- %(<turbo-stream action="#{action}" targets="#{targets}">#{template}</turbo-stream>).html_safe
20
+ tag.turbo_stream(template, **attributes.merge(action: action, targets: targets))
19
21
  else
20
- raise ArgumentError, "target or targets must be supplied"
22
+ tag.turbo_stream(template, **attributes.merge(action: action))
21
23
  end
22
24
  end
23
25
 
@@ -0,0 +1,19 @@
1
+ export function encodeMethodIntoRequestBody(event) {
2
+ if (event.target instanceof HTMLFormElement) {
3
+ const { target: form, detail: { fetchOptions } } = event
4
+
5
+ form.addEventListener("turbo:submit-start", ({ detail: { formSubmission: { submitter } } }) => {
6
+ const method = (submitter && submitter.formMethod) || (fetchOptions.body && fetchOptions.body.get("_method")) || form.getAttribute("method")
7
+
8
+ if (!/get/i.test(method)) {
9
+ if (/post/i.test(method)) {
10
+ fetchOptions.body.delete("_method")
11
+ } else {
12
+ fetchOptions.body.set("_method", method)
13
+ }
14
+
15
+ fetchOptions.method = "post"
16
+ }
17
+ }, { once: true })
18
+ }
19
+ }
@@ -1,5 +1,4 @@
1
1
  import "./cable_stream_source_element"
2
- import { overrideMethodWithFormmethod } from "./form_submissions"
3
2
 
4
3
  import * as Turbo from "@hotwired/turbo"
5
4
  export { Turbo }
@@ -7,4 +6,6 @@ export { Turbo }
7
6
  import * as cable from "./cable"
8
7
  export { cable }
9
8
 
10
- addEventListener("turbo:submit-start", overrideMethodWithFormmethod)
9
+ import { encodeMethodIntoRequestBody } from "./fetch_requests"
10
+
11
+ addEventListener("turbo:before-fetch-request", encodeMethodIntoRequestBody)
data/lib/turbo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Turbo
2
- VERSION = "1.1.1"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbo-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Stephenson
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-05-23 00:00:00.000000000 Z
13
+ date: 2022-09-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activejob
@@ -80,7 +80,7 @@ files:
80
80
  - app/helpers/turbo/streams_helper.rb
81
81
  - app/javascript/turbo/cable.js
82
82
  - app/javascript/turbo/cable_stream_source_element.js
83
- - app/javascript/turbo/form_submissions.js
83
+ - app/javascript/turbo/fetch_requests.js
84
84
  - app/javascript/turbo/index.js
85
85
  - app/javascript/turbo/snakeize.js
86
86
  - app/jobs/turbo/streams/action_broadcast_job.rb
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
117
  requirements: []
118
- rubygems_version: 3.2.33
118
+ rubygems_version: 3.3.20
119
119
  signing_key:
120
120
  specification_version: 4
121
121
  summary: The speed of a single-page web application without having to write any JavaScript.
@@ -1,7 +0,0 @@
1
- export function overrideMethodWithFormmethod({ detail: { formSubmission: { fetchRequest, submitter } } }) {
2
- const formMethod = submitter?.formMethod
3
-
4
- if (formMethod && fetchRequest.body.has("_method")) {
5
- fetchRequest.body.set("_method", formMethod)
6
- }
7
- }