turbo-rails 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -33,8 +33,10 @@ module Turbo::Native::Navigation
33
33
 
34
34
  # :nodoc:
35
35
  def turbo_native_action_or_redirect(url, action, redirect_type, options = {})
36
+ native_params = options.delete(:native_params) || {}
37
+
36
38
  if turbo_native_app?
37
- redirect_to send("turbo_#{action}_historical_location_url", notice: options[:notice] || options.delete(:native_notice))
39
+ redirect_to send("turbo_#{action}_historical_location_url", notice: options[:notice], **native_params)
38
40
  elsif redirect_type == :back
39
41
  redirect_back fallback_location: url, **options
40
42
  else
@@ -15,18 +15,18 @@ module Turbo::Streams::ActionHelper
15
15
  template = action.to_sym == :remove ? "" : tag.template(template.to_s.html_safe)
16
16
 
17
17
  if target = convert_to_turbo_stream_dom_id(target)
18
- tag.turbo_stream(template, **attributes.merge(action: action, target: target))
18
+ tag.turbo_stream(template, **attributes, action: action, target: target)
19
19
  elsif targets = convert_to_turbo_stream_dom_id(targets, include_selector: true)
20
- tag.turbo_stream(template, **attributes.merge(action: action, targets: targets))
20
+ tag.turbo_stream(template, **attributes, action: action, targets: targets)
21
21
  else
22
- tag.turbo_stream(template, **attributes.merge(action: action))
22
+ tag.turbo_stream(template, **attributes, action: action)
23
23
  end
24
24
  end
25
25
 
26
26
  private
27
27
  def convert_to_turbo_stream_dom_id(target, include_selector: false)
28
28
  if target.respond_to?(:to_key)
29
- [ ("#" if include_selector), ActionView::RecordIdentifier.dom_id(target) ].compact.join
29
+ "#{"#" if include_selector}#{ActionView::RecordIdentifier.dom_id(target)}"
30
30
  else
31
31
  target
32
32
  end
@@ -25,4 +25,7 @@ class TurboCableStreamSourceElement extends HTMLElement {
25
25
  }
26
26
  }
27
27
 
28
- customElements.define("turbo-cable-stream-source", TurboCableStreamSourceElement)
28
+
29
+ if (customElements.get("turbo-cable-stream-source") === undefined) {
30
+ customElements.define("turbo-cable-stream-source", TurboCableStreamSourceElement)
31
+ }
@@ -3,13 +3,14 @@ export function encodeMethodIntoRequestBody(event) {
3
3
  const { target: form, detail: { fetchOptions } } = event
4
4
 
5
5
  form.addEventListener("turbo:submit-start", ({ detail: { formSubmission: { submitter } } }) => {
6
- const method = (submitter && submitter.formMethod) || (fetchOptions.body && fetchOptions.body.get("_method")) || form.getAttribute("method")
6
+ const body = isBodyInit(fetchOptions.body) ? fetchOptions.body : new URLSearchParams()
7
+ const method = determineFetchMethod(submitter, body, form)
7
8
 
8
9
  if (!/get/i.test(method)) {
9
10
  if (/post/i.test(method)) {
10
- fetchOptions.body.delete("_method")
11
+ body.delete("_method")
11
12
  } else {
12
- fetchOptions.body.set("_method", method)
13
+ body.set("_method", method)
13
14
  }
14
15
 
15
16
  fetchOptions.method = "post"
@@ -17,3 +18,33 @@ export function encodeMethodIntoRequestBody(event) {
17
18
  }, { once: true })
18
19
  }
19
20
  }
21
+
22
+ function determineFetchMethod(submitter, body, form) {
23
+ const formMethod = determineFormMethod(submitter)
24
+ const overrideMethod = body.get("_method")
25
+ const method = form.getAttribute("method") || "get"
26
+
27
+ if (typeof formMethod == "string") {
28
+ return formMethod
29
+ } else if (typeof overrideMethod == "string") {
30
+ return overrideMethod
31
+ } else {
32
+ return method
33
+ }
34
+ }
35
+
36
+ function determineFormMethod(submitter) {
37
+ if (submitter instanceof HTMLButtonElement || submitter instanceof HTMLInputElement) {
38
+ if (submitter.hasAttribute("formmethod")) {
39
+ return submitter.formMethod
40
+ } else {
41
+ return null
42
+ }
43
+ } else {
44
+ return null
45
+ }
46
+ }
47
+
48
+ function isBodyInit(body) {
49
+ return body instanceof FormData || body instanceof URLSearchParams
50
+ }
@@ -28,7 +28,7 @@
28
28
  #
29
29
  # You can also choose to render html instead of a partial inside of a broadcast
30
30
  # you do this by passing the html: option to any broadcast method that accepts the **rendering argument
31
- #
31
+ #
32
32
  # class Message < ApplicationRecord
33
33
  # belongs_to :user
34
34
  #
@@ -39,7 +39,7 @@
39
39
  # broadcast_update_to(user, :messages, target: "message-count", html: "<p> #{user.messages.count} </p>")
40
40
  # end
41
41
  # end
42
- #
42
+ #
43
43
  # There are four basic actions you can broadcast: <tt>remove</tt>, <tt>replace</tt>, <tt>append</tt>, and
44
44
  # <tt>prepend</tt>. As a rule, you should use the <tt>_later</tt> versions of everything except for remove when broadcasting
45
45
  # within a real-time path, like a controller or model, since all those updates require a rendering step, which can slow down
@@ -72,16 +72,16 @@ module Turbo::Broadcastable
72
72
  # broadcasts_to ->(message) { [ message.board, :messages ] }, partial: "messages/custom_message"
73
73
  # end
74
74
  def broadcasts_to(stream, inserts_by: :append, target: broadcast_target_default, **rendering)
75
- after_create_commit -> { broadcast_action_later_to stream.try(:call, self) || send(stream), action: inserts_by, target: target.try(:call, self) || target, **rendering }
76
- after_update_commit -> { broadcast_replace_later_to stream.try(:call, self) || send(stream), **rendering }
77
- after_destroy_commit -> { broadcast_remove_to stream.try(:call, self) || send(stream) }
75
+ after_create_commit -> { broadcast_action_later_to(stream.try(:call, self) || send(stream), action: inserts_by, target: target.try(:call, self) || target, **rendering) }
76
+ after_update_commit -> { broadcast_replace_later_to(stream.try(:call, self) || send(stream), **rendering) }
77
+ after_destroy_commit -> { broadcast_remove_to(stream.try(:call, self) || send(stream)) }
78
78
  end
79
79
 
80
80
  # Same as <tt>#broadcasts_to</tt>, but the designated stream for updates and destroys is automatically set to
81
81
  # the current model, for creates - to the model plural name, which can be overriden by passing <tt>stream</tt>.
82
82
  def broadcasts(stream = model_name.plural, inserts_by: :append, target: broadcast_target_default, **rendering)
83
- after_create_commit -> { broadcast_action_later_to stream, action: inserts_by, target: target.try(:call, self) || target, **rendering }
84
- after_update_commit -> { broadcast_replace_later **rendering }
83
+ after_create_commit -> { broadcast_action_later_to(stream, action: inserts_by, target: target.try(:call, self) || target, **rendering) }
84
+ after_update_commit -> { broadcast_replace_later(**rendering) }
85
85
  after_destroy_commit -> { broadcast_remove }
86
86
  end
87
87
 
data/lib/turbo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Turbo
2
- VERSION = "1.3.2"
2
+ VERSION = "1.3.3"
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.3.2
4
+ version: 1.3.3
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-10-21 00:00:00.000000000 Z
13
+ date: 2023-01-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activejob
@@ -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.3.20
118
+ rubygems_version: 3.4.5
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.