Skip to content

Commit

Permalink
feat: add edit/update views for appointment
Browse files Browse the repository at this point in the history
we are not checking who is allowed to edit in the appointment partial
yet
  • Loading branch information
avogel3 committed Jan 27, 2025
1 parent ef0b677 commit bb82cbf
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 11 deletions.
30 changes: 27 additions & 3 deletions app/controllers/appointments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions app/models/appointment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 26 additions & 5 deletions app/views/appointments/_appointment.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
<div>
<%= appointment.requested_datetime %>
<%= appointment.notes %>
<%= appointment.created_by_user_id %>
</div>
<%= turbo_frame_tag(appointment) do %>
<div class="flex flex-wrap items-center justify-between gap-x-6 gap-y-4 py-5 sm:flex-nowrap">
<div>
<p class="text-sm/6 font-semibold text-gray-900">
<a href="#" class="hover:underline">Requested Time - <%= appointment.requested_datetime.strftime("%B %-d %Y at %I:%H%P") %></a>
</p>
<div class="mt-1 flex flex-col text-xs/5">
<p class="text-gray-400">
<%= truncate(appointment.notes, length: 40) %>
</p>
<div class="flex gap-x-4 text-gray-500">
<a href="#" class="hover:underline"><%= appointment.created_by.email_address %></a>
<time datetime="<%= appointment.updated_at %>">
<%= distance_of_time_in_words_to_now(appointment.updated_at, include_seconds: false) %> ago
</time>
</div>
</div>
</div>
<dl class="flex w-full flex-none justify-between gap-x-8 sm:w-auto">
<div class="flex w-16 gap-x-2.5">
<span class="sr-only">Actions</span>
<%= link_to "Edit", edit_appointment_path(appointment), class: "text-xs text-indigo-500 hover:text-indigo-400", data: { turbo: false } %>
</div>
</dl>
</div>
<% end %>
19 changes: 19 additions & 0 deletions app/views/appointments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="flex flex-col mx-auto">
<h1 class="text-3xl font-bold tracking-tight text-gray-900">Edit Appointment</h1>

<%= form_for appointment, html: { class: "mt-12" } do |f| %>
<div class="flex flex-col gap-y-4">
<div class="flex flex-col">
<%= f.label :requested_datetime, class: "font-semibold" %>
<%= f.datetime_field :requested_datetime %>
</div>

<div class="flex flex-col">
<%= f.label :notes, class: "font-semibold" %>
<%= f.text_area :notes, cols: 40, rows: 10 %>
</div>

<%= 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" %>
</div>
<% end %>
</div>
8 changes: 6 additions & 2 deletions app/views/appointments/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<h1>Appointments</h1>
<div class="flex flex-col mx-auto">
<h1 class="text-3xl font-bold tracking-tight text-gray-900">Appointments</h1>

<%= render appointments, as: :appointment %>
<div role="list" class="flex flex-col divide-y divide-gray-100">
<%= render appointments, as: :appointment %>
</div>
</div>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit bb82cbf

Please sign in to comment.