Skip to content

Commit

Permalink
Cancel with a button, icons
Browse files Browse the repository at this point in the history
  • Loading branch information
PencilAmazing committed Dec 22, 2024
1 parent e417cc3 commit 6792aa2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
30 changes: 21 additions & 9 deletions app/controllers/locker_rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,38 @@ def create
end
end

def update
@locker_rental = LockerRental.find(params[:id])
if @locker_rental.update(locker_rental_params)
flash[:notice] = "Locker rental updated"
else
flash[:alert] = "Failed to update locker rental"
end
end

private

def locker_rental_params
if current_user.admin?
params
.require(:locker_rental)
.permit(
admin_params =
params.require(:locker_rental).permit(
:locker_type_id,
# admin can assign and approve requests
:rented_by_id,
:locker_specifier,
:state,
:owned_until
)
.reverse_merge(
rented_by_id:
User.find_by(
username: params.dig(:locker_rental, :rented_by_username)
)&.id
)

# FIXME replace that search with a different one, return ID instead
# If username is given (since search can do that)
rented_by_user =
User.find_by(username: params.dig(:locker_rental, :rented_by_username))
if rented_by_user
# then convert to user id
admin_params.reverse_merge!(rented_by_id: rented_by_user.id)
end
admin_params
else
# people pick where they want a locker
params
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/lockers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class LockersController < ApplicationController
before_action :current_user
before_action :signed_in

helper_method :rental_state_icon

def index
@locker_types = LockerType.all
@all_locker_rentals = LockerRental.all
Expand All @@ -23,4 +25,17 @@ def create

def types
end

private

def rental_state_icon(state)
case state
when "active"
"fa-lock"
when "cancelled"
"fa-clock-o text-danger"
else
""
end
end
end
5 changes: 3 additions & 2 deletions app/views/lockers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@
<tr>
<td><%= "#{rental.locker_type.short_form}##{rental.locker_specifier}" %></td>
<td><%= link_to rental.rented_by.username, user_path(rental.rented_by) %></td>
<td><%= rental.state.humanize %></td>
<td><%= tag.i class: "fa #{rental_state_icon(rental.state)}" %> <%= rental.state.humanize %></td>
<td><%= rental.updated_at&.to_date %></td>
<td><%= rental.owned_until&.to_date || 'Unapproved yet' %></td>
<td><%= button_to 'End rental',
locker_rental_path(rental, params: { locker_rental: { id: rental.id, state: :cancelled } }),
locker_rental_path(rental),
data: { confirm: 'Are you sure you want to cancel this locker rental?' },
params: { locker_rental: { state: :cancelled } },
method: :put,
class: 'btn btn-danger' %></td>
</tr>
Expand Down

0 comments on commit 6792aa2

Please sign in to comment.