turbo-rails 2.0.0.pre.beta.1 → 2.0.0.pre.beta.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,9 @@
1
1
  module Turbo::DriveHelper
2
- # Note: These helpers require a +yield :head+ provision in the layout.
2
+ # Helpers to configure Turbo Drive via meta directives. They come in two
3
+ # variants:
4
+ #
5
+ # The recommended option is to include +yield :head+ in the +<head>+ section
6
+ # of the layout. Then you can use the helpers in any view.
3
7
  #
4
8
  # ==== Example
5
9
  #
@@ -9,29 +13,76 @@ module Turbo::DriveHelper
9
13
  # # app/views/trays/index.html.erb
10
14
  # <% turbo_exempts_page_from_cache %>
11
15
  # <p>Page that shouldn't be cached by Turbo</p>
16
+ #
17
+ # Alternatively, you can use the +_tag+ variant of the helpers to only get the
18
+ # HTML for the meta directive.
12
19
 
13
20
  # Pages that are more likely than not to be a cache miss can skip turbo cache to avoid visual jitter.
14
21
  # Cannot be used along with +turbo_exempts_page_from_preview+.
15
22
  def turbo_exempts_page_from_cache
16
- provide :head, tag.meta(name: "turbo-cache-control", content: "no-cache")
23
+ provide :head, turbo_exempts_page_from_cache_tag
24
+ end
25
+
26
+ # See +turbo_exempts_page_from_cache+.
27
+ def turbo_exempts_page_from_cache_tag
28
+ tag.meta(name: "turbo-cache-control", content: "no-cache")
17
29
  end
18
30
 
19
31
  # Specify that a cached version of the page should not be shown as a preview during an application visit.
20
32
  # Cannot be used along with +turbo_exempts_page_from_cache+.
21
33
  def turbo_exempts_page_from_preview
22
- provide :head, tag.meta(name: "turbo-cache-control", content: "no-preview")
34
+ provide :head, turbo_exempts_page_from_preview_tag
35
+ end
36
+
37
+ # See +turbo_exempts_page_from_preview+.
38
+ def turbo_exempts_page_from_preview_tag
39
+ tag.meta(name: "turbo-cache-control", content: "no-preview")
23
40
  end
24
41
 
25
42
  # Force the page, when loaded by Turbo, to be cause a full page reload.
26
43
  def turbo_page_requires_reload
27
- provide :head, tag.meta(name: "turbo-visit-control", content: "reload")
44
+ provide :head, turbo_page_requires_reload_tag
45
+ end
46
+
47
+ # See +turbo_page_requires_reload+.
48
+ def turbo_page_requires_reload_tag
49
+ tag.meta(name: "turbo-visit-control", content: "reload")
28
50
  end
29
51
 
52
+ # Configure how to handle page refreshes. A page refresh happens when
53
+ # Turbo loads the current page again with a *replace* visit:
54
+ #
55
+ # === Parameters:
56
+ #
57
+ # * <tt>method</tt> - Method to update the +<body>+ of the page
58
+ # during a page refresh. It can be one of:
59
+ # * +replace:+: Replaces the existing +<body>+ with the new one. This is the
60
+ # default behavior.
61
+ # * +morph:+: Morphs the existing +<body>+ into the new one.
62
+ #
63
+ # * <tt>scroll</tt> - Controls the scroll behavior when a page refresh happens. It
64
+ # can be one of:
65
+ # * +reset:+: Resets scroll to the top, left corner. This is the default.
66
+ # * +preserve:+: Keeps the scroll.
67
+ #
68
+ # === Example Usage:
69
+ #
70
+ # turbo_refreshes_with(method: :morph, scroll: :preserve)
30
71
  def turbo_refreshes_with(method: :replace, scroll: :reset)
72
+ provide :head, turbo_refresh_method_tag(method)
73
+ provide :head, turbo_refresh_scroll_tag(scroll)
74
+ end
75
+
76
+ # Configure method to perform page refreshes. See +turbo_refreshes_with+.
77
+ def turbo_refresh_method_tag(method = :replace)
31
78
  raise ArgumentError, "Invalid refresh option '#{method}'" unless method.in?(%i[ replace morph ])
32
- raise ArgumentError, "Invalid scroll option '#{scroll}'" unless scroll.in?(%i[ reset preserve ])
79
+ tag.meta(name: "turbo-refresh-method", content: method)
80
+ end
33
81
 
34
- provide :head, tag.meta(name: "turbo-refresh-method", content: method)
35
- provide :head, tag.meta(name: "turbo-refresh-scroll", content: scroll)
82
+ # Configure scroll strategy for page refreshes. See +turbo_refreshes_with+.
83
+ def turbo_refresh_scroll_tag(scroll = :reset)
84
+ raise ArgumentError, "Invalid scroll option '#{scroll}'" unless scroll.in?(%i[ reset preserve ])
85
+ tag.meta(name: "turbo-refresh-scroll", content: scroll)
36
86
  end
37
87
  end
88
+
@@ -8,4 +8,6 @@ export { cable }
8
8
 
9
9
  import { encodeMethodIntoRequestBody } from "./fetch_requests"
10
10
 
11
+ window.Turbo = Turbo
12
+
11
13
  addEventListener("turbo:before-fetch-request", encodeMethodIntoRequestBody)
@@ -2,7 +2,7 @@
2
2
  # turbo stream templates.
3
3
  class Turbo::Streams::BroadcastJob < ActiveJob::Base
4
4
  discard_on ActiveJob::DeserializationError
5
-
5
+
6
6
  def perform(stream, **rendering)
7
7
  Turbo::StreamsChannel.broadcast_render_to stream, **rendering
8
8
  end
@@ -142,9 +142,11 @@ module Turbo::Broadcastable
142
142
  end
143
143
 
144
144
  # Same as <tt>#broadcasts_refreshes_to</tt>, but the designated stream for page refreshes is automatically set to
145
- # the current model.
146
- def broadcasts_refreshes
147
- after_commit -> { broadcast_refresh_later }
145
+ # the current model, for creates - to the model plural name, which can be overriden by passing <tt>stream</tt>.
146
+ def broadcasts_refreshes(stream = model_name.plural)
147
+ after_create_commit -> { broadcast_refresh_later_to(stream) }
148
+ after_update_commit -> { broadcast_refresh_later }
149
+ after_destroy_commit -> { broadcast_refresh }
148
150
  end
149
151
 
150
152
  # All default targets will use the return of this method. Overwrite if you want something else than <tt>model_name.plural</tt>.
@@ -292,7 +294,7 @@ module Turbo::Broadcastable
292
294
  end
293
295
 
294
296
  def broadcast_refresh_to(*streamables)
295
- Turbo::StreamsChannel.broadcast_refresh_to *streamables unless suppressed_turbo_broadcasts?
297
+ Turbo::StreamsChannel.broadcast_refresh_to(*streamables) unless suppressed_turbo_broadcasts?
296
298
  end
297
299
 
298
300
  def broadcast_refresh
@@ -22,6 +22,24 @@
22
22
  # <%= turbo_stream.append dom_id(topic_merge) do %>
23
23
  # <%= link_to topic_merge.topic.name, topic_path(topic_merge.topic) %>
24
24
  # <% end %>
25
+ #
26
+ # To integrate with custom actions, extend this class in response to the :turbo_streams_tag_builder load hook:
27
+ #
28
+ # ActiveSupport.on_load :turbo_streams_tag_builder do
29
+ # def highlight(target)
30
+ # action :highlight, target
31
+ # end
32
+ #
33
+ # def highlight_all(targets)
34
+ # action_all :highlight, targets
35
+ # end
36
+ # end
37
+ #
38
+ # turbo_stream.highlight "my-element"
39
+ # # => <turbo-stream action="highlight" target="my-element"><template></template></turbo-stream>
40
+ #
41
+ # turbo_stream.highlight_all ".my-selector"
42
+ # # => <turbo-stream action="highlight" targets=".my-selector"><template></template></turbo-stream>
25
43
  class Turbo::Streams::TagBuilder
26
44
  include Turbo::Streams::ActionHelper
27
45
 
@@ -246,4 +264,6 @@ class Turbo::Streams::TagBuilder
246
264
  @view_context.render(partial: record, formats: :html)
247
265
  end
248
266
  end
267
+
268
+ ActiveSupport.run_load_hooks :turbo_streams_tag_builder, self
249
269
  end
@@ -2,4 +2,4 @@ say "Import Turbo"
2
2
  append_to_file "app/javascript/application.js", %(import "@hotwired/turbo-rails"\n)
3
3
 
4
4
  say "Pin Turbo"
5
- append_to_file "config/importmap.rb", %(pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true\n)
5
+ append_to_file "config/importmap.rb", %(pin "@hotwired/turbo-rails", to: "turbo.min.js"\n)
data/lib/turbo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Turbo
2
- VERSION = "2.0.0-beta.1"
2
+ VERSION = "2.0.0-beta.3"
3
3
  end
data/lib/turbo-rails.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "turbo/engine"
2
+ require "active_support/core_ext/module/attribute_accessors_per_thread"
2
3
 
3
4
  module Turbo
4
5
  extend ActiveSupport::Autoload
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: 2.0.0.pre.beta.1
4
+ version: 2.0.0.pre.beta.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: 2023-11-23 00:00:00.000000000 Z
13
+ date: 2024-01-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activejob