Skip to content

Commit

Permalink
Replace Turbolinks with Turbo (and upgrade Stimulus) (#1181)
Browse files Browse the repository at this point in the history
# What it does

* Replaces Turbolinks in the app with the newer version of the library,
Turbo.
* Upgrades Stimulus to the latest version
* Replaces our PortalRendering code with equivalent Turbo-based code.

Fixes #1184.

# Why it is important

Turbolinks is EOLed, and we need to upgrade to Turbo to get back on a
supported library. And by using Turbo instead of custom code, we have
less to maintain and it's easier for contributors to understand how
things work as there are many resources out there for learning Turbo.

# Implementation notes

* This is based on the main parts of #810, without the use of real-time
Turbo Streams as that requires Redis. We can add that later if needed,
but I don't want to pull in that dependency as a part of this.

# Your bandwidth for additional changes to this PR

_Please choose one of the following to help the project maintainers
provide the appropriate level of support:_

- [x] I have the time and interest to make additional changes to this PR
based on feedback.
- [ ] I am interested in feedback but don't need to make the changes
myself.
- [ ] I don't have time or interest in making additional changes to this
work.
- [ ] Other or not sure (please describe):

---------

Co-authored-by: derrick lannaman <derrick.lannaman@powerhrg.com>
  • Loading branch information
jim and derricklannaman authored Jan 15, 2024
1 parent 21caabf commit ddf61a1
Show file tree
Hide file tree
Showing 191 changed files with 924 additions and 838 deletions.
6 changes: 2 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ gem "pg", ">= 0.18", "< 2.0"
# Use Puma as the app server
gem "puma"

# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem "turbolinks", "~> 5"

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem "jbuilder", "~> 2.11"

Expand All @@ -32,7 +29,6 @@ gem "acts_as_tenant"
gem "devise"
gem "pundit"
gem "audited"
gem "turbolinks_render"
gem "money-rails"
gem "mjml-rails" # , github: "jim/mjml-rails", branch: "webpacker"
gem "pagy"
Expand Down Expand Up @@ -60,6 +56,8 @@ gem "dotenv-rails"
gem "appsignal"

gem "chronic"
gem "turbo-rails"
gem "stimulus-rails"

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", ">= 1.4.1", require: false
Expand Down
18 changes: 8 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ GEM
lint_roller (~> 1.0)
rubocop-rails (~> 2.20.2)
statsd-ruby (1.5.0)
stimulus-rails (1.3.0)
railties (>= 6.0.0)
store_model (0.13.0)
activerecord (>= 5.2)
stringio (3.0.9)
Expand All @@ -512,14 +514,10 @@ GEM
timeout (0.4.1)
translation (1.29)
gettext (~> 3.2, >= 3.2.5, <= 3.4.2)
turbolinks (5.2.1)
turbolinks-source (~> 5.2)
turbolinks-source (5.2.0)
turbolinks_render (0.9.21)
actionpack (>= 5.2.0)
activesupport (>= 5.2.0)
railties (>= 5.2.0)
turbolinks-source (~> 5.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)
unf (0.1.4)
Expand Down Expand Up @@ -597,11 +595,11 @@ DEPENDENCIES
square.rb
standard
standard-rails (~> 0.2.0)
stimulus-rails
store_model
sucker_punch
translation
turbolinks (~> 5)
turbolinks_render
turbo-rails
web-console (>= 3.3.0)

RUBY VERSION
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/overrides.scss
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ trix-toolbar {
}
}

// These forms exist only to wrap a button
form.button_to {
display: inline;
}

// Improve contrast on labels
.label {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
tbody tr {
.drag-handle {
cursor: grab;
width: 36px;
}
&.notified {
background: $gray-color-light;
Expand Down
4 changes: 1 addition & 3 deletions app/assets/stylesheets/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,7 @@ form.membership-amount {
}
}

.button_to {
display: inline;
}


.columns {
margin-top: 0 !important;
Expand Down
12 changes: 7 additions & 5 deletions app/controllers/account/appointments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create
else
load_holds_and_loans
load_appointment_slots
render :new, alert: @appointment.errors.full_messages
render :new, alert: @appointment.errors.full_messages, status: :unprocessable_entity
end
end

Expand All @@ -45,13 +45,13 @@ def update
else
load_holds_and_loans
load_appointment_slots
render :edit, alert: @appointment.errors.full_messages
render :edit, alert: @appointment.errors.full_messages, status: :unprocessable_entity
end
end

def destroy
@appointment.destroy
redirect_to account_appointments_path, flash: {success: "Appointment cancelled."}
redirect_to account_appointments_path, flash: {success: "Appointment cancelled."}, status: :see_other
end

private
Expand Down Expand Up @@ -85,12 +85,14 @@ def merge_and_complete_appointment_update
message = "Your appointment was scheduled for #{helpers.appointment_date_and_time(@appointment)}."
MemberMailer.with(member: @member, appointment: @appointment).appointment_confirmation.deliver_later
end
redirect_to account_appointments_path, success: message
redirect_to account_appointments_path, success: message, status: :see_other
end

def load_appointment_for_editing
@appointment = current_member.appointments.find(params[:id])
redirect_to account_appointments_path, alert: "Completed appointments can't be changed" if @appointment.completed?
if @appointment.completed?
redirect_to account_appointments_path, alert: "Completed appointments can't be changed", status: :see_other
end
end
end
end
6 changes: 3 additions & 3 deletions app/controllers/account/holds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def create
@new_hold.transaction do
if @new_hold.save
@new_hold.start! if @new_hold.ready_for_pickup?
redirect_to item_path(@item), success: "Hold placed."
redirect_to item_path(@item), success: "Hold placed.", status: :see_other
else
redirect_to item_path(@item), error: "Something went wrong!"
redirect_to item_path(@item), error: "Something went wrong!", status: :see_other
end
end
end
Expand All @@ -22,7 +22,7 @@ def destroy
hold = current_member.active_holds.find(params[:id])
hold.cancel!

redirect_to account_holds_path
redirect_to account_holds_path, status: :see_other
end

def history
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/account/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def edit
def update
respond_to do |format|
if current_member.update(member_params)
format.html { redirect_to account_member_url, success: "Profile was successfully updated." }
format.html { redirect_to account_member_url, success: "Profile was successfully updated.", status: :see_other }
format.json { render :show, status: :ok, location: current_member }
else
format.html { render :edit }
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @member.errors, status: :unprocessable_entity }
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/account/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def update
@user = current_user
if @user.update_with_password(user_params)
bypass_sign_in(@user)
redirect_to account_member_url, success: "Your password has been updated."
redirect_to account_member_url, success: "Your password has been updated.", status: :see_other
else
redirect_to edit_account_password_url, warning: @user.errors.full_messages.join(". ").to_s
redirect_to edit_account_password_url, warning: @user.errors.full_messages.join(". ").to_s, status: :see_other
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/account/renewal_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create
{error: "Something went wrong!"}
end

redirect_to account_loans_path, flash: flash_message
redirect_to account_loans_path, flash: flash_message, status: :see_other
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/account/renewals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def create
authorize @loan, :renew?

renew_loan(@loan)
redirect_to account_home_path, success: "#{@loan.item.name} was renewed."
redirect_to account_home_path, success: "#{@loan.item.name} was renewed.", status: :see_other
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/admin/appointment_checkins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create

redirect_to admin_appointment_path(@appointment), flash: {
success: "#{helpers.pluralize(returned_item_count, "Item")} checked-in."
}
}, status: :see_other
end
end
end
4 changes: 3 additions & 1 deletion app/controllers/admin/appointment_checkouts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class AppointmentCheckoutsController < BaseController

def create
create_loans_for_holds
redirect_to admin_appointment_path(appointment), flash: {success: "#{pluralize(checked_out_item_count, "Item")} checked-out."}
redirect_to admin_appointment_path(appointment), flash: {
success: "#{pluralize(checked_out_item_count, "Item")} checked-out."
}, status: :see_other
end

private
Expand Down
24 changes: 20 additions & 4 deletions app/controllers/admin/appointment_completions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
module Admin
class AppointmentCompletionsController < BaseController
before_action :are_appointments_enabled?
include ActionView::RecordIdentifier

include PortalRendering
before_action :are_appointments_enabled?

def create
@appointment = Appointment.find(params[:appointment_id])
@appointment.update!(completed_at: Time.current, staff_updating: true)
render_to_portal("admin/appointments/appointment", table_row: true, locals: {appointment: @appointment})

render_appointment_to_turbo_stream
end

def destroy
@appointment = Appointment.find(params[:appointment_id])
@appointment.update!(completed_at: nil, staff_updating: true)
render_to_portal("admin/appointments/appointment", table_row: true, locals: {appointment: @appointment})

render_appointment_to_turbo_stream
end

private

def render_appointment_to_turbo_stream
respond_to do |format|
format.turbo_stream {
render turbo_stream:
turbo_stream.replace(
dom_id(@appointment),
render_to_string(partial: "admin/appointments/appointment", locals: {appointment: @appointment})
)
}
end
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/admin/appointment_holds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class AppointmentHoldsController < ApplicationController

def create
add_new_appointment_hold
redirect_to admin_appointment_path(appointment), flash: create_flash_message
redirect_to admin_appointment_path(appointment), flash: create_flash_message, status: :see_other
end

def destroy
remove_appointment_hold
redirect_to admin_appointment_path(appointment), flash: {success: "Item removed from appointment."}
redirect_to admin_appointment_path(appointment), flash: {success: "Item removed from appointment."}, status: :see_other
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/appointment_loans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class AppointmentLoansController < ApplicationController

def create
add_new_appointment_loan
redirect_to admin_appointment_path(appointment), flash: create_flash_message
redirect_to admin_appointment_path(appointment), flash: create_flash_message, status: :see_other
end

def destroy
remove_appointment_loan
redirect_to admin_appointment_path(appointment), flash: {success: "Item removed from appointment."}
redirect_to admin_appointment_path(appointment), flash: {success: "Item removed from appointment."}, status: :see_other
end

private
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/admin/appointments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ def edit

def update
if @appointment.update(appointment_params)
redirect_to admin_appointments_path, flash: {success: "Appointment updated."}
redirect_to admin_appointments_path, flash: {success: "Appointment updated."}, status: :see_other
else
load_appointment_slots
render :edit
render :edit, status: :unprocessable_entity
end
end

def destroy
@appointment.destroy
redirect_to admin_appointments_path, flash: {success: "Appointment cancelled."}
redirect_to admin_appointments_path, flash: {success: "Appointment cancelled."}, status: :see_other
end

private
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ class BaseController < ApplicationController
before_action :require_staff
before_action :load_requested_renewal_request_count

layout "admin"
layout :custom_layout

private

def custom_layout
return "turbo_rails/frame" if turbo_frame_request?

"admin"
end

def require_staff
unless current_user.roles.include?(:staff)
redirect_to root_url, warning: "You do not have access to that page."
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/borrow_policies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def edit

def update
if @borrow_policy.update(borrow_policy_params)
redirect_to admin_borrow_policies_url, success: "Borrow policy was successfully updated."
redirect_to admin_borrow_policies_url, success: "Borrow policy was successfully updated.", status: :see_other
else
render :edit
render :edit, status: :unprocessable_entity
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/bulk_renewals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def update
renew_loan(loan) if loan.renewable?
end

redirect_to admin_member_url(@member.id)
redirect_to admin_member_url(@member.id), status: :see_other
end

private
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/admin/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ def create
@category = Category.new(category_params)

if @category.save
redirect_to admin_categories_url(anchor: dom_id(@category))
redirect_to admin_categories_url(anchor: dom_id(@category)), status: :see_other
else
render :new
render :new, status: :unprocessable_entity
end
end

def update
if @category.update(category_params)
redirect_to admin_categories_url(anchor: dom_id(@category))
redirect_to admin_categories_url(anchor: dom_id(@category)), status: :see_other
else
render :edit
render :edit, status: :unprocessable_entity
end
end

def destroy
@category.destroy
redirect_to admin_categories_url, success: "Category was successfully destroyed."
redirect_to admin_categories_url, success: "Category was successfully destroyed.", status: :see_other
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def edit
def update
respond_to do |format|
if @document.update(document_params)
format.html { redirect_to [:admin, @document], notice: "Document was successfully updated." }
format.html { redirect_to [:admin, @document], notice: "Document was successfully updated.", status: :see_other }
format.json { render :show, status: :ok, location: [:admin, @document] }
else
format.html { render :edit }
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end
Expand Down
Loading

0 comments on commit ddf61a1

Please sign in to comment.