Skip to content

Commit

Permalink
use turbo 8 page refresh morphing
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Dec 24, 2023
1 parent b11e2ce commit 24277eb
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -18,7 +18,7 @@ gem "puma", ">= 5.0"
gem "importmap-rails"

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
gem "turbo-rails", github: "hotwired/turbo-rails", branch: "main"

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
Expand Down
38 changes: 22 additions & 16 deletions Gemfile.lock
@@ -1,3 +1,13 @@
GIT
remote: https://github.com/hotwired/turbo-rails.git
revision: d1b420118e9b8769c21b5160c8aa055dc6a985c2
branch: main
specs:
turbo-rails (2.0.0.pre.beta.2)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -75,10 +85,10 @@ GEM
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
addressable (2.8.5)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
base64 (0.2.0)
bigdecimal (3.1.4)
bigdecimal (3.1.5)
bindex (0.8.1)
bootsnap (1.17.0)
msgpack (~> 1.2)
Expand All @@ -96,9 +106,9 @@ GEM
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.4)
debug (1.8.0)
irb (>= 1.5.0)
reline (>= 0.3.1)
debug (1.9.1)
irb (~> 1.10)
reline (>= 0.3.8)
drb (2.2.0)
ruby2_keywords
erubi (1.12.0)
Expand All @@ -110,8 +120,8 @@ GEM
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
railties (>= 6.0.0)
io-console (0.6.0)
irb (1.10.1)
io-console (0.7.1)
irb (1.11.0)
rdoc
reline (>= 0.3.8)
jbuilder (2.11.5)
Expand All @@ -131,7 +141,7 @@ GEM
minitest (5.20.0)
msgpack (1.7.2)
mutex_m (0.2.0)
net-imap (0.4.7)
net-imap (0.4.9)
date
net-protocol
net-pop (0.1.2)
Expand All @@ -147,7 +157,7 @@ GEM
racc (~> 1.4)
nokogiri (1.15.5-x86_64-linux)
racc (~> 1.4)
psych (5.1.1.1)
psych (5.1.2)
stringio
public_suffix (5.0.4)
puma (6.4.0)
Expand Down Expand Up @@ -191,11 +201,11 @@ GEM
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rake (13.1.0)
rdoc (6.6.1)
rdoc (6.6.2)
psych (>= 4.0.0)
redis (5.0.8)
redis-client (>= 0.17.0)
redis-client (0.19.0)
redis-client (0.19.1)
connection_pool
regexp_parser (2.8.3)
reline (0.4.1)
Expand All @@ -222,10 +232,6 @@ GEM
stringio (3.1.0)
thor (1.3.0)
timeout (0.4.1)
turbo-rails (1.5.0)
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
web-console (4.2.1)
Expand Down Expand Up @@ -260,7 +266,7 @@ DEPENDENCIES
sprockets-rails
sqlite3 (~> 1.4)
stimulus-rails
turbo-rails
turbo-rails!
tzinfo-data
web-console

Expand Down
5 changes: 4 additions & 1 deletion app/controllers/events_controller.rb
Expand Up @@ -30,6 +30,9 @@ def create
def update
if params[:like].present?
@event.increment!(:likes_count)
# @events = Event.order(start_date: :desc)
# return
Turbo::StreamsChannel.broadcast_refresh_to "events_broadcaster"
return redirect_to events_path
end
respond_to do |format|
Expand All @@ -43,7 +46,7 @@ def update

def destroy
@event.destroy!

Turbo::StreamsChannel.broadcast_refresh_to "events_broadcaster"
respond_to do |format|
format.html { redirect_to events_url, notice: "Event was successfully destroyed." }
end
Expand Down
28 changes: 28 additions & 0 deletions app/views/events/_table.html.erb
@@ -0,0 +1,28 @@
<table>
<thead>
<tr>
<th>Name</th>
<th colspan="2">Likes</th>
<th>Location</th>
<th>Start date</th>
<th colspan="3">Actions</th>
</tr>
</thead>

<tbody>
<% @events.each do |event| %>
<tr>
<td><%= event.name %></td>
<td><%= event.likes_count %></td>
<td>
<%= button_to "Like", event_path(event), method: :patch, params: { like: true } %>
</td>
<td><%= event.location %></td>
<td><%= event.start_date.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= button_to 'Destroy', event, method: :delete, data: { turbo_confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
36 changes: 9 additions & 27 deletions app/views/events/index.html.erb
@@ -1,32 +1,14 @@
<h1>Events</h1>

<%= turbo_stream_from "events_broadcaster" %>
<%= link_to 'New Event', new_event_path %>

<table>
<thead>
<tr>
<th>Name</th>
<th colspan="2">Likes</th>
<th>Location</th>
<th>Start date</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<details data-turbo-permanent>
<summary>Details</summary>
Something small enough to escape casual notice.
</details>

<tbody>
<% @events.each do |event| %>
<tr>
<td><%= event.name %></td>
<td><%= event.likes_count %></td>
<td>
<%= button_to "Like", event_path(event), method: :patch, params: { like: true } %>
</td>
<td><%= event.location %></td>
<td><%= event.start_date.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= button_to 'Destroy', event, method: :delete, data: { turbo_confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<div id="events_list">
<%= render 'table' %>
</div>
4 changes: 4 additions & 0 deletions app/views/events/update.turbo_stream.erb
@@ -0,0 +1,4 @@
<%= turbo_stream.update 'events_list' do %>
NOT USING THIS!
<%= render 'table', events: @events %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -8,6 +8,9 @@
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<%#= turbo_refreshes_with method: :replace, scroll: :reset %>
<%= turbo_refreshes_with method: :morph, scroll: :preserve %>
<%= yield :head %>
</head>

<body>
Expand Down

0 comments on commit 24277eb

Please sign in to comment.