From bb82cbf195ab8767a28ca31493f6b853d4503612 Mon Sep 17 00:00:00 2001 From: Andrew Vogel Date: Sat, 25 Jan 2025 14:05:44 -0500 Subject: [PATCH] feat: add edit/update views for appointment we are not checking who is allowed to edit in the appointment partial yet --- app/controllers/appointments_controller.rb | 30 +++++++++++++++++-- app/models/appointment.rb | 4 +++ app/views/appointments/_appointment.html.erb | 31 ++++++++++++++++---- app/views/appointments/edit.html.erb | 19 ++++++++++++ app/views/appointments/index.html.erb | 8 +++-- config/routes.rb | 2 +- 6 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 app/views/appointments/edit.html.erb diff --git a/app/controllers/appointments_controller.rb b/app/controllers/appointments_controller.rb index 5769f51..225c751 100644 --- a/app/controllers/appointments_controller.rb +++ b/app/controllers/appointments_controller.rb @@ -1,12 +1,36 @@ class AppointmentsController < ApplicationController - helper_method :appointments + helper_method :appointments, :appointment - def index + before_action :check_created_by, only: [:edit, :update] + + def update + if appointment.update(appointment_params) + redirect_to appointments_path, notice: "Appointment updated!" + else + render :edit, status: :unprocessable_entity + end end private + def check_created_by + unless appointment.created_by?(Current.user) + redirect_to appointments_path, alert: "Can't modify appointment - you are not the creator." + end + end + + def appointment_params + params.require(:appointment).permit( + :requested_datetime, + :notes + ) + end + + def appointment + @appointment ||= Appointment.find(params[:id]) + end + def appointments - @appointments ||= Appointment.all + @appointments ||= Appointment.includes(:created_by).all end end diff --git a/app/models/appointment.rb b/app/models/appointment.rb index f0d75de..2c03bff 100644 --- a/app/models/appointment.rb +++ b/app/models/appointment.rb @@ -2,4 +2,8 @@ class Appointment < ApplicationRecord belongs_to :created_by, foreign_key: :created_by_user_id, class_name: "User" validates :requested_datetime, presence: true + + def created_by?(user) + created_by == user + end end diff --git a/app/views/appointments/_appointment.html.erb b/app/views/appointments/_appointment.html.erb index 1bb8f3d..69b8797 100644 --- a/app/views/appointments/_appointment.html.erb +++ b/app/views/appointments/_appointment.html.erb @@ -1,5 +1,26 @@ -
- <%= appointment.requested_datetime %> - <%= appointment.notes %> - <%= appointment.created_by_user_id %> -
+<%= turbo_frame_tag(appointment) do %> +
+
+

+ Requested Time - <%= appointment.requested_datetime.strftime("%B %-d %Y at %I:%H%P") %> +

+
+

+ <%= truncate(appointment.notes, length: 40) %> +

+
+ <%= appointment.created_by.email_address %> + +
+
+
+
+
+ Actions + <%= link_to "Edit", edit_appointment_path(appointment), class: "text-xs text-indigo-500 hover:text-indigo-400", data: { turbo: false } %> +
+
+
+<% end %> diff --git a/app/views/appointments/edit.html.erb b/app/views/appointments/edit.html.erb new file mode 100644 index 0000000..ec2d4c0 --- /dev/null +++ b/app/views/appointments/edit.html.erb @@ -0,0 +1,19 @@ +
+

Edit Appointment

+ + <%= form_for appointment, html: { class: "mt-12" } do |f| %> +
+
+ <%= f.label :requested_datetime, class: "font-semibold" %> + <%= f.datetime_field :requested_datetime %> +
+ +
+ <%= f.label :notes, class: "font-semibold" %> + <%= f.text_area :notes, cols: 40, rows: 10 %> +
+ + <%= f.submit class: "rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" %> +
+ <% end %> +
diff --git a/app/views/appointments/index.html.erb b/app/views/appointments/index.html.erb index 08e5eb6..6bc3c9e 100644 --- a/app/views/appointments/index.html.erb +++ b/app/views/appointments/index.html.erb @@ -1,3 +1,7 @@ -

Appointments

+
+

Appointments

-<%= render appointments, as: :appointment %> +
+ <%= render appointments, as: :appointment %> +
+
diff --git a/config/routes.rb b/config/routes.rb index a6b64da..5083402 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do resource :session resources :passwords, param: :token - resources :appointments, only: :index + resources :appointments, only: [:index, :edit, :update] # 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.