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| %> -
+ <%= 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 @@ + 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 %> -