Skip to content

Commit

Permalink
Merge pull request #132 from lsa-mis/LRA-955-room-ready-delete-and-ar…
Browse files Browse the repository at this point in the history
…chive-common-attributes

Lra 955 room ready delete and archive common attributes
  • Loading branch information
britaumich authored Jul 8, 2024
2 parents aa88db4 + c95dc4e commit 6354d3b
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def redirect_rover_to_correct_state(room:, room_state:, step:, mode: "new")
index = steps.find_index(step) + 1
redirect = {}

CommonAttribute.all.present? ? redirect["common_attributes"] = true : redirect["common_attributes"] = false
CommonAttribute.active.present? ? redirect["common_attributes"] = true : redirect["common_attributes"] = false
room.active_specific_attributes.present? ? redirect["specific_attributes"] = true : redirect["specific_attributes"] = false
room.resources.present? ? redirect["resources"] = true : redirect["resources"] = false

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/common_attribute_states_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def new

authorize CommonAttributeState

@common_attribute_states = CommonAttribute.all.map do |common_attribute|
@common_attribute_states = CommonAttribute.active.map do |common_attribute|
common_attribute.common_attribute_states.new
end
end
Expand Down
38 changes: 29 additions & 9 deletions app/controllers/common_attributes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
class CommonAttributesController < ApplicationController
before_action :auth_user
before_action :set_common_attribute, only: %i[edit update destroy ]
before_action :set_common_attribute, only: %i[edit update destroy unarchive]

# GET /common_attributes or /common_attributes.json
def index
@common_attributes = CommonAttribute.all
@show_archived = params[:show_archived] == "1"
if @show_archived
@common_attributes = CommonAttribute.archived
@option_header = "Unarchive"
else
@common_attributes = CommonAttribute.active
@option_header = "Delete/Archive"
end

@new_common_attribute = CommonAttribute.new
authorize @common_attributes
end
Expand Down Expand Up @@ -53,14 +61,26 @@ def update

# DELETE /common_attributes/1 or /common_attributes/1.json
def destroy
@common_attribute.destroy!

respond_to do |format|
notice = "Common attribute was successfully deleted."
format.turbo_stream do
flash.now[:notice] = notice
if @common_attribute.has_state?
if @common_attribute.update(archived: true)
@common_attributes = CommonAttribute.active
@option_header = "Delete/Archive"
end
flash.now[:notice] = "Common attribute was successfully archived."
else
if @common_attribute.destroy
@common_attributes = CommonAttribute.active
@option_header = "Delete/Archive"
end
format.html { redirect_to common_attributes_url, notice: notice }
flash.now[:notice] = "Common attribute was successfully deleted."
end
end

def unarchive
if @common_attribute.update(archived: false)
@common_attributes = CommonAttribute.archived
flash.now[:notice] = "Common attribute was successfully unarchived."
@option_header = "Unarchive"
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index

# GET /rooms/1 or /rooms/1.json
def show
@common_attributes = CommonAttribute.all
@common_attributes = CommonAttribute.active
@new_note = Note.new(room: @room)
@notes = @room.notes.order("created_at DESC")
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/common_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class CommonAttribute < ApplicationRecord
validates :description, presence: true, uniqueness: true
validate :needs_checkbox_or_quantity_box

scope :active, -> { where(archived: false) }
scope :archived, -> { where(archived: true) }

def has_state?
self.common_attribute_states.present?
end

private

def needs_checkbox_or_quantity_box
Expand Down
2 changes: 1 addition & 1 deletion app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Room < ApplicationRecord
validates :rmrecnbr, presence: true, uniqueness: true
validates :room_number, :room_type, presence: true

accepts_nested_attributes_for :specific_attributes
accepts_nested_attributes_for :active_specific_attributes

scope :active, -> { where(archived: false) }
scope :archived, -> { where(archived: true) }
Expand Down
2 changes: 1 addition & 1 deletion app/models/room_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(room)
end

def common_attributes_exist?
CommonAttribute.count > 0
CommonAttribute.active.count > 0
end

def specific_attributes_exist?
Expand Down
4 changes: 4 additions & 0 deletions app/policies/common_attribute_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ def edit?
def destroy?
is_admin?
end

def unarchive?
is_admin?
end
end
22 changes: 18 additions & 4 deletions app/views/common_attributes/_common_attribute.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@
<% end %>
</td>
<td>
<%= link_to edit_common_attribute_path(common_attribute), 'aria-label': "edit_#{common_attribute.id}" do %>
<i class="bi bi-pencil-square text-primary"></i>
<% unless common_attribute.archived %>
<%= link_to edit_common_attribute_path(common_attribute), 'aria-label': "edit_#{common_attribute.id}" do %>
<i class="bi bi-pencil-square text-primary"></i>
<% end %>
<% end %>
</td>
<td>
<%= link_to common_attribute_path(common_attribute), 'aria-label': "delete_#{common_attribute.id}", data: { turbo_confirm: "Are you sure you want to delete this common attribute?" , turbo_method: :delete } do %>
<i class="bi bi-trash-fill text-danger"></i>
<% if common_attribute.archived %>
<%= link_to unarchive_common_attribute_path(common_attribute), data: { turbo_confirm: "Are you sure you want to unarchive this common attribute?" , turbo_method: :post }, 'aria-label': "unarchive_#{common_attribute.id}" do %>
<i class="bi bi-archive text-success"></i>
<% end %>
<% else %>
<% if common_attribute.has_state? %>
<%= link_to common_attribute_path(common_attribute), 'aria-label': "delete_#{common_attribute.id}", data: { turbo_confirm: "Are you sure you want to archive this common attribute?" , turbo_method: :delete } do %>
<i class="bi bi-archive-fill text-success"></i>
<% end %>
<% else %>
<%= link_to common_attribute_path(common_attribute), 'aria-label': "delete_#{common_attribute.id}", data: { turbo_confirm: "Are you sure you want to delete this common attribute?" , turbo_method: :delete } do %>
<i class="bi bi-trash-fill text-danger"></i>
<% end %>
<% end %>
<% end %>
</td>
</tr>
16 changes: 16 additions & 0 deletions app/views/common_attributes/_common_attributes_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<table class="table text-center mt-4">
<thead class="table-group-divider">
<tr>
<th scope="col" class="text-start">Description</th>
<th scope="col">Needs Checkbox?</th>
<th scope="col">Needs Quantity Box?</th>
<th scope="col">Edit</th>
<th scope="col"><%= @option_header %></th>
</tr>
</thead>
<tbody class="table-group-divider" id="common_attributes">
<% @common_attributes.each do |common_attribute| %>
<%= render common_attribute %>
<% end %>
</tbody>
</table>
6 changes: 4 additions & 2 deletions app/views/common_attributes/destroy.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<%= turbo_stream.remove @common_attribute %>
<%= turbo_stream.update "flash", partial: 'layouts/flash' %>
<%= turbo_stream.update 'common_attributes_list' do %>
<%= render 'common_attributes_list' %>
<% end %>
<%= render_flash_stream %>
26 changes: 10 additions & 16 deletions app/views/common_attributes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
<%= render 'form', common_attribute: @new_common_attribute %>
<% end %>

<table class="table text-center mt-4">
<thead class="table-group-divider">
<tr>
<th scope="col" class="text-start">Description</th>
<th scope="col">Needs Checkbox?</th>
<th scope="col">Needs Quantity Box?</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody class="table-group-divider" id="common_attributes">
<% @common_attributes.each do |common_attribute| %>
<%= render common_attribute %>
<% end %>
</tbody>
</table>
<%= form_with(url: common_attributes_path, html: { class: 'mb-3' }, method: :get, data: { controller: "autosubmit", autosubmit_target: "form" }) do |form| %>
<div class="mt-2">
<%= check_box_tag :show_archived, 1, @show_archived, data: { action: "input->autosubmit#search" } %>
<%= form.label :show_archived, "Show Archived Common Attributes" %>
</div>
<% end %>

<%= turbo_frame_tag 'common_attributes_list' do %>
<%= render 'common_attributes_list' %>
<% end %>
</div>
4 changes: 4 additions & 0 deletions app/views/common_attributes/unarchive.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= turbo_stream.update 'common_attributes_list' do %>
<%= render 'common_attributes_list' %>
<% end %>
<%= render_flash_stream %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
post '/common_attribute_states/update_common_attribute_states/:id', to: 'common_attribute_states#update_common_attribute_states', as: :update_common_attribute_states

resources :common_attributes, except: [:show]
post 'unarchive_common_attribute/:id', to: 'common_attributes#unarchive', as: :unarchive_common_attribute
resources :rovers

resources :zones do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddArchivedToCommonAttributes < ActiveRecord::Migration[7.1]
def change
add_column :common_attributes, :archived, :boolean, default: false
end
end

0 comments on commit 6354d3b

Please sign in to comment.