From dcbbd0298fbcef715d511be0fc6e635dfbfd89b6 Mon Sep 17 00:00:00 2001
From: Lucas Mendelowski
Date: Sat, 5 Mar 2022 18:18:05 +0100
Subject: [PATCH] First Unpoly version
---
Gemfile | 2 +-
Gemfile.lock | 11 ++++++----
app/assets/stylesheets/application.sass.scss | 2 ++
app/controllers/application_controller.rb | 12 ++++++++++
app/controllers/quotes_controller.rb | 16 ++++----------
app/helpers/application_helper.rb | 4 +---
app/javascript/application.js | 23 +++++++++++++++++++-
app/models/application_record.rb | 1 +
app/models/quote.rb | 5 -----
app/views/layouts/_flash.html.erb | 8 +------
app/views/layouts/_flash_message.html.erb | 7 ++++++
app/views/quotes/_no_quotes.html.erb | 2 +-
app/views/quotes/_quote.html.erb | 21 +++++++++++-------
app/views/quotes/create.turbo_stream.erb | 3 ---
app/views/quotes/destroy.turbo_stream.erb | 2 --
app/views/quotes/edit.html.erb | 4 ++--
app/views/quotes/index.html.erb | 13 ++++++-----
app/views/quotes/new.html.erb | 4 ++--
app/views/quotes/update.turbo_stream.erb | 2 --
package.json | 3 ++-
yarn.lock | 5 +++++
21 files changed, 90 insertions(+), 60 deletions(-)
create mode 100644 app/views/layouts/_flash_message.html.erb
delete mode 100644 app/views/quotes/create.turbo_stream.erb
delete mode 100644 app/views/quotes/destroy.turbo_stream.erb
delete mode 100644 app/views/quotes/update.turbo_stream.erb
diff --git a/Gemfile b/Gemfile
index 176febd..b34155b 100644
--- a/Gemfile
+++ b/Gemfile
@@ -12,8 +12,8 @@ gem "simple_form"
gem "cssbundling-rails"
gem "jsbundling-rails"
gem "sprockets-rails"
-gem "turbo-rails"
gem "stimulus-rails"
+gem "unpoly-rails"
group :development, :test do
gem "debug", platforms: %i[ mri mingw x64_mingw ]
diff --git a/Gemfile.lock b/Gemfile.lock
index 65758e2..9d3ae47 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -113,6 +113,7 @@ GEM
mini_mime (>= 0.1.1)
marcel (1.0.2)
matrix (0.4.2)
+ memoized (1.0.2)
method_source (1.0.0)
mini_mime (1.1.2)
minitest (5.15.0)
@@ -199,11 +200,13 @@ GEM
strscan (3.0.1)
thor (1.2.1)
timeout (0.2.0)
- turbo-rails (1.0.1)
- actionpack (>= 6.0.0)
- railties (>= 6.0.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
+ unpoly-rails (2.5.2)
+ actionpack (>= 3.2)
+ activesupport (>= 3.2)
+ memoized
+ railties (>= 3.2)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.0)
@@ -239,7 +242,7 @@ DEPENDENCIES
sprockets-rails
sqlite3 (~> 1.4)
stimulus-rails
- turbo-rails
+ unpoly-rails
web-console
webdrivers
diff --git a/app/assets/stylesheets/application.sass.scss b/app/assets/stylesheets/application.sass.scss
index 661864e..16b60e9 100644
--- a/app/assets/stylesheets/application.sass.scss
+++ b/app/assets/stylesheets/application.sass.scss
@@ -1,3 +1,5 @@
+@import "unpoly/unpoly";
+
@import "config/variable";
@import "config/animations";
@import "config/reset";
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 077f63f..227c373 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -3,9 +3,21 @@ class ApplicationController < ActionController::Base
helper_method :current_company
+ def redirect_to(target, *args, **kwargs)
+ if kwargs[:notice].present?
+ up.emit('flash:notice', content: render_flash(kwargs[:notice]))
+ end
+
+ super
+ end
+
private
def current_company
@current_company ||= current_user.company if user_signed_in?
end
+
+ def render_flash(message)
+ render_to_string("layouts/_flash_message", locals: { message: message }, layout: false)
+ end
end
diff --git a/app/controllers/quotes_controller.rb b/app/controllers/quotes_controller.rb
index e64dd50..d7b583b 100644
--- a/app/controllers/quotes_controller.rb
+++ b/app/controllers/quotes_controller.rb
@@ -15,10 +15,7 @@ def new
def create
@quote = current_company.quotes.build(quote_params)
if @quote.save
- respond_to do |format|
- format.html { redirect_to quotes_path, notice: "Quote was successfully created." }
- format.turbo_stream { flash.now[:notice] = "Quote was successfully created." }
- end
+ redirect_to quotes_path, notice: "Quote was successfully created."
else
render :new, status: :unprocessable_entity
end
@@ -29,10 +26,8 @@ def edit
def update
if @quote.update(quote_params)
- respond_to do |format|
- format.html { redirect_to quotes_path, notice: "Quote was successfully updated." }
- format.turbo_stream { flash.now[:notice] = "Quote was successfully updated." }
- end
+ up.title = 'Updated'
+ redirect_to quotes_path, notice: "Quote was successfully updated."
else
render :edit, status: :unprocessable_entity
end
@@ -41,10 +36,7 @@ def update
def destroy
@quote.destroy
- respond_to do |format|
- format.html { redirect_to quotes_path, notice: "Quote was successfully deleted." }
- format.turbo_stream { flash.now[:notice] = "Quote was successfully deleted." }
- end
+ redirect_to quotes_path, notice: "Quote was successfully deleted."
end
private
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 71a5c37..a2f4870 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1,5 +1,3 @@
module ApplicationHelper
- def render_turbo_stream_flash_messages
- turbo_stream.prepend "flash", partial: "layouts/flash"
- end
+
end
diff --git a/app/javascript/application.js b/app/javascript/application.js
index 9ad78c5..5689fa3 100644
--- a/app/javascript/application.js
+++ b/app/javascript/application.js
@@ -1,2 +1,23 @@
-import "@hotwired/turbo-rails"
+import "unpoly/unpoly"
import "./controllers"
+
+up.form.config.submitSelectors.push(['form'])
+up.link.config.followSelectors.push('a[href]')
+up.radio.config.pollInterval = 10000
+
+up.on('flash:notice', function({ content }) {
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(content, 'text/html');
+ const html = doc.body.firstChild;
+
+ document.getElementById('flash').prepend(html);
+})
+
+up.compiler('[up-keep][updated]', function(element) {
+ element.addEventListener('up:fragment:keep', function(event) {
+ const next = new Date(event.newFragment.getAttribute('updated'))
+ const prev = new Date(element.getAttribute('updated'))
+
+ if (prev < next) event.preventDefault()
+ })
+})
diff --git a/app/models/application_record.rb b/app/models/application_record.rb
index b63caeb..4af2288 100644
--- a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -1,3 +1,4 @@
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
+
end
diff --git a/app/models/quote.rb b/app/models/quote.rb
index bfbbd4d..ba8eb0f 100644
--- a/app/models/quote.rb
+++ b/app/models/quote.rb
@@ -4,9 +4,4 @@ class Quote < ApplicationRecord
validates :name, presence: true
scope :ordered, -> { order(id: :desc) }
-
- # after_create_commit -> { broadcast_prepend_later_to("quotes") }
- # after_update_commit -> { broadcast_replace_later_to("quotes") }
- # after_destroy_commit -> { broadcast_remove_later_to("quotes") }
- broadcasts_to ->(quote) { [quote.company, "quotes"] }, inserts_by: :prepend
end
diff --git a/app/views/layouts/_flash.html.erb b/app/views/layouts/_flash.html.erb
index 4240185..3e0312c 100644
--- a/app/views/layouts/_flash.html.erb
+++ b/app/views/layouts/_flash.html.erb
@@ -1,9 +1,3 @@
<% flash.each do |flash_type, message| %>
-
- <%= message %>
-
+ <%= render partial: "layouts/flash_message", locals: { message: message } %>
<% end %>
diff --git a/app/views/layouts/_flash_message.html.erb b/app/views/layouts/_flash_message.html.erb
new file mode 100644
index 0000000..46d4a6e
--- /dev/null
+++ b/app/views/layouts/_flash_message.html.erb
@@ -0,0 +1,7 @@
+
+ <%= message %>
+
diff --git a/app/views/quotes/_no_quotes.html.erb b/app/views/quotes/_no_quotes.html.erb
index afeb065..b3bb0e2 100644
--- a/app/views/quotes/_no_quotes.html.erb
+++ b/app/views/quotes/_no_quotes.html.erb
@@ -3,5 +3,5 @@
You don't have any quotes yet!
- <%= link_to "Add quote", new_quote_path, class: "btn btn--primary", data: { turbo_frame: dom_id(Quote.new) } %>
+ <%= link_to "Add quote", new_quote_path, class: "btn btn--primary" %>
diff --git a/app/views/quotes/_quote.html.erb b/app/views/quotes/_quote.html.erb
index 89eaf2a..1f31d36 100644
--- a/app/views/quotes/_quote.html.erb
+++ b/app/views/quotes/_quote.html.erb
@@ -1,9 +1,14 @@
-<%= turbo_frame_tag quote do %>
-
- <%= link_to quote.name, quote_path(quote), data: { turbo_frame: "_top" } %>
-
- <%= button_to "Delete", quote_path(quote), method: :delete, class: "btn btn--light" %>
- <%= link_to "Edit", edit_quote_path(quote), class: "btn btn--light" %>
-
+
+ <%= link_to quote.name, quote_path(quote) %>
+
+ <%= button_to "Delete",
+ quote_path(quote),
+ method: :delete,
+ class: "btn btn--light",
+ "up-target" => "##{dom_id(quote)}" %>
+ <%= link_to "Edit",
+ edit_quote_path(quote),
+ class: "btn btn--light",
+ "up-target" => "##{dom_id(quote)}" %>
-<% end %>
+
diff --git a/app/views/quotes/create.turbo_stream.erb b/app/views/quotes/create.turbo_stream.erb
deleted file mode 100644
index 0d0cd84..0000000
--- a/app/views/quotes/create.turbo_stream.erb
+++ /dev/null
@@ -1,3 +0,0 @@
-<%= turbo_stream.prepend "quotes", @quote %>
-<%= turbo_stream.update Quote.new, "" %>
-<%= render_turbo_stream_flash_messages %>
diff --git a/app/views/quotes/destroy.turbo_stream.erb b/app/views/quotes/destroy.turbo_stream.erb
deleted file mode 100644
index c81b07b..0000000
--- a/app/views/quotes/destroy.turbo_stream.erb
+++ /dev/null
@@ -1,2 +0,0 @@
-<%= turbo_stream.remove @quote %>
-<%= turbo_stream.prepend "flash", partial: "layouts/flash" %>
diff --git a/app/views/quotes/edit.html.erb b/app/views/quotes/edit.html.erb
index 2d02743..0eed4e5 100644
--- a/app/views/quotes/edit.html.erb
+++ b/app/views/quotes/edit.html.erb
@@ -5,7 +5,7 @@
Edit quote
- <%= turbo_frame_tag @quote do %>
+
<%= render "form", quote: @quote %>
- <% end %>
+
diff --git a/app/views/quotes/index.html.erb b/app/views/quotes/index.html.erb
index 7e60041..44511d3 100644
--- a/app/views/quotes/index.html.erb
+++ b/app/views/quotes/index.html.erb
@@ -1,15 +1,16 @@
-<%= turbo_stream_from current_company, "quotes" %>
-
- <%= turbo_frame_tag Quote.new %>
+
- <%= turbo_frame_tag "quotes" do %>
+
<%= render "no_quotes" %>
<%= render @quotes %>
- <% end %>
+
diff --git a/app/views/quotes/new.html.erb b/app/views/quotes/new.html.erb
index 453290f..756d0a0 100644
--- a/app/views/quotes/new.html.erb
+++ b/app/views/quotes/new.html.erb
@@ -5,7 +5,7 @@
New quote
- <%= turbo_frame_tag @quote do %>
+
<%= render "form", quote: @quote %>
- <% end %>
+
diff --git a/app/views/quotes/update.turbo_stream.erb b/app/views/quotes/update.turbo_stream.erb
deleted file mode 100644
index 1a49722..0000000
--- a/app/views/quotes/update.turbo_stream.erb
+++ /dev/null
@@ -1,2 +0,0 @@
-<%= turbo_stream.replace @quote %>
-<%= render_turbo_stream_flash_messages %>
diff --git a/package.json b/package.json
index c5d730b..4af38ab 100644
--- a/package.json
+++ b/package.json
@@ -5,7 +5,8 @@
"@hotwired/stimulus": "^3.0.1",
"@hotwired/turbo-rails": "^7.1.1",
"esbuild": "^0.14.23",
- "sass": "^1.49.8"
+ "sass": "^1.49.8",
+ "unpoly": "^2.5.2"
},
"scripts": {
"build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",
diff --git a/yarn.lock b/yarn.lock
index bd9af6f..56b854b 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -265,3 +265,8 @@ to-regex-range@^5.0.1:
integrity sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==
dependencies:
is-number "^7.0.0"
+
+unpoly@^2.5.2:
+ version "2.5.2"
+ resolved "https://registry.yarnpkg.com/unpoly/-/unpoly-2.5.2.tgz#a213b0e089852810c3ca6f1cc712ef7d7bb2c792"
+ integrity sha512-s1UWKib8F4RWLpSWDyMDVZGInAsGEaGjzEJyG4XvWlUVsFLJumNW32h7wpUZwYJRf/7CKYoaHXFG3bcpnlOShQ==