Skip to content

Commit

Permalink
Merge pull request #4499 from rubyforgood/item-index-caching
Browse files Browse the repository at this point in the history
Speed up item page
  • Loading branch information
cielf authored Jul 5, 2024
2 parents e32137b + 201d643 commit 846dd95
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ItemsController < ApplicationController
def index
@items = current_organization
.items
.includes(:base_item, :kit, :line_items, :request_units)
.includes(:base_item, :kit, :line_items, :request_units, :item_category)
.alphabetized
.class_filter(filter_params)
.group('items.id')
Expand Down
22 changes: 13 additions & 9 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,23 @@ def has_inventory?(inventory = nil)
end
end

def is_in_kit?
organization.kits
.active
.joins(:line_items)
.where(line_items: { item_id: id}).any?
def is_in_kit?(kits = nil)
if kits
kits.any? { |k| k.line_items.map(&:item_id).include?(id) }
else
organization.kits
.active
.joins(:line_items)
.where(line_items: { item_id: id}).any?
end
end

def can_delete?(inventory = nil)
can_deactivate_or_delete?(inventory) && line_items.none? && !barcode_count&.positive?
def can_delete?(inventory = nil, kits = nil)
can_deactivate_or_delete?(inventory, kits) && line_items.none? && !barcode_count&.positive?
end

# @return [Boolean]
def can_deactivate_or_delete?(inventory = nil)
def can_deactivate_or_delete?(inventory = nil, kits = nil)
if inventory.nil? && Event.read_events?(organization)
inventory = View::Inventory.new(organization_id)
end
Expand All @@ -156,7 +160,7 @@ def can_deactivate_or_delete?(inventory = nil)
# If an active kit includes this item, then changing kit allocations would change inventory
# for an inactive item - which we said above we don't want to allow.

!has_inventory?(inventory) && !is_in_kit?
!has_inventory?(inventory) && !is_in_kit?(kits)
end

def validate_destroy
Expand Down
2 changes: 1 addition & 1 deletion app/views/items/_item_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</tr>
</thead>
<tbody>
<%= render partial: "items/item_row", collection: items, as: :item_row, locals: { inventory: inventory } %>
<%= render partial: "items/item_row", collection: items, as: :item_row, locals: { inventory: inventory, kits: kits } %>
</tbody>
</table>
</div><!-- /.box-body.table-responsive -->
4 changes: 2 additions & 2 deletions app/views/items/_item_row.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<%= view_button_to item_path(item_row) %>
<%= edit_button_to edit_item_path(item_row) %>
<% if item_row.active? %>
<% if item_row.can_delete?(inventory) %>
<% if item_row.can_delete?(inventory, kits) %>
<%= delete_button_to item_path(item_row),
text: 'Delete',
confirm: confirm_delete_msg(item_row.name) %>
<% else %>
<% can_deactivate = item_row.can_deactivate_or_delete?(inventory) %>
<% can_deactivate = item_row.can_deactivate_or_delete?(inventory, kits) %>
<%= delete_button_to deactivate_item_path(item_row),
text: 'Deactivate',
enabled: can_deactivate,
Expand Down
2 changes: 1 addition & 1 deletion app/views/items/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
</div>
<div class="card-body">
<div class="tab-content" id="custom-tabs-three-tabContent">
<%= render partial: 'item_list', locals: { items: @items, inventory: @inventory } %>
<%= render partial: 'item_list', locals: { items: @items, inventory: @inventory, kits: @kits.active } %>
<%= render partial: 'item_categories', locals: { item_categories: @item_categories } %>
<%= render partial: 'items_quantity_and_location' %>
<%= render partial: 'items_inventory' %>
Expand Down

0 comments on commit 846dd95

Please sign in to comment.