From ce387092d6f03eb2bf857c10afb9f0d198f249e7 Mon Sep 17 00:00:00 2001 From: Andrew Vogel Date: Sun, 26 Jan 2025 23:33:45 -0500 Subject: [PATCH] feat: broadcast replace appointments with a lazy turbo frame --- app/models/appointment.rb | 8 ++++++++ app/views/appointments/_load_row.html.erb | 1 + app/views/appointments/row.html.erb | 1 + config/routes.rb | 6 +++++- 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 app/views/appointments/_load_row.html.erb create mode 100644 app/views/appointments/row.html.erb diff --git a/app/models/appointment.rb b/app/models/appointment.rb index 2c03bff..26fb918 100644 --- a/app/models/appointment.rb +++ b/app/models/appointment.rb @@ -3,6 +3,14 @@ class Appointment < ApplicationRecord validates :requested_datetime, presence: true + after_save_commit -> { + broadcast_replace_to( + "appointments_list", + partial: "appointments/load_row", + locals: { appointment: self } + ) + } + def created_by?(user) created_by == user end diff --git a/app/views/appointments/_load_row.html.erb b/app/views/appointments/_load_row.html.erb new file mode 100644 index 0000000..0a77516 --- /dev/null +++ b/app/views/appointments/_load_row.html.erb @@ -0,0 +1 @@ +<%= turbo_frame_tag(appointment, src: row_appointment_path(appointment)) %> diff --git a/app/views/appointments/row.html.erb b/app/views/appointments/row.html.erb new file mode 100644 index 0000000..41db675 --- /dev/null +++ b/app/views/appointments/row.html.erb @@ -0,0 +1 @@ +<%= render "appointment", appointment: %> diff --git a/config/routes.rb b/config/routes.rb index 5083402..673bd13 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,11 @@ Rails.application.routes.draw do resource :session resources :passwords, param: :token - resources :appointments, only: [:index, :edit, :update] + resources :appointments, only: [:index, :edit, :update] do + member do + get :row + end + end # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.