Skip to content

Commit 596f5a6

Browse files
committed
Perf: Refactor ProductDrive#index with service
1 parent 456588a commit 596f5a6

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

app/controllers/product_drives_controller.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ def index
66
setup_date_range_picker
77
@product_drives = current_organization
88
.product_drives
9-
.includes(donations: {line_items: :item})
109
.class_filter(filter_params)
1110
.within_date_range(@selected_date_range)
1211
.order(start_date: :desc)
@@ -15,6 +14,11 @@ def index
1514
@item_categories = current_organization.item_categories
1615
@selected_name_filter = filter_params[:by_name]
1716
@selected_item_category = filter_params[:by_item_category_id]
17+
@summary = ProductDriveSummaryService.new(
18+
product_drives: @product_drives,
19+
within_date_range: @selected_date_range,
20+
item_category_id: @selected_item_category
21+
).call
1822

1923
respond_to do |format|
2024
format.html

app/models/product_drive.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ProductDrive < ApplicationRecord
1919

2020
scope :by_name, ->(name_filter) { where(name: name_filter) }
2121
scope :by_item_category_id, ->(item_category_id) {
22-
joins(donations: {line_items: :item})
22+
includes(donations: {line_items: :item})
2323
.where(item: { item_category_id: item_category_id })
2424
}
2525

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class ProductDriveSummaryService
2+
def initialize(product_drives:, within_date_range:, item_category_id:)
3+
@product_drives = product_drives
4+
@within_date_range = within_date_range
5+
@item_category_id = item_category_id
6+
end
7+
8+
def call
9+
calculate_summary
10+
self
11+
end
12+
13+
def fetch_quantity(product_drive_id)
14+
@summary.dig(product_drive_id, :quantity)
15+
end
16+
17+
def fetch_unique_item_count(product_drive_id)
18+
@summary.dig(product_drive_id, :unique_item_count)
19+
end
20+
21+
def fetch_value(product_drive_id)
22+
@summary.dig(product_drive_id, :value)
23+
end
24+
25+
private
26+
27+
# Returns hash of total quantity, unique item count and value per product drive
28+
# Example return: { 1 => { quantity: 15, unique_item_count: 2, value: 100 }, ...}
29+
#
30+
# @return [Hash<Hash<Symbol, Integer>>]
31+
def calculate_summary
32+
@summary ||= begin
33+
query = ProductDrive
34+
.left_joins(donations: {line_items: [:item]})
35+
.where(id: @product_drives.ids)
36+
.within_date_range(@within_date_range)
37+
.group("product_drives.id")
38+
.distinct
39+
40+
query = query.where(items: {item_category_id: @item_category_id}) if @item_category_id.present?
41+
42+
query.pluck(Arel.sql(
43+
"product_drives.id AS id,
44+
COALESCE(SUM(line_items.quantity), 0) AS quantity,
45+
COUNT(DISTINCT line_items.item_id) AS unique_item_count,
46+
COALESCE(SUM(COALESCE(items.value_in_cents, 0) * line_items.quantity), 0) AS value"
47+
)).to_h do |(id, quantity, unique_item_count, value)|
48+
[id, {quantity:, unique_item_count:, value:}]
49+
end
50+
end
51+
end
52+
end

app/views/product_drives/index.html.erb

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@
104104
<td><%= product_drive.start_date.strftime("%m-%d-%Y") %></td>
105105
<td><%= product_drive.end_date&.strftime("%m-%d-%Y") %></td>
106106
<td><%= is_virtual(product_drive: product_drive) %></td>
107-
<td class="text-right"><%= product_drive.donation_quantity_by_date(selected_range, @selected_item_category) %></td>
108-
<td class="text-right"><%= product_drive.distinct_items_count_by_date(selected_range, @selected_item_category) %></td>
109-
<td class="text-right"><strong><%= dollar_value(product_drive.in_kind_value) %></strong></td>
107+
<td class="text-right"><%= @summary.fetch_quantity(product_drive.id) %></td>
108+
<td class="text-right"><%= @summary.fetch_unique_item_count(product_drive.id) %></td>
109+
<td class="text-right"><strong><%= dollar_value(@summary.fetch_value(product_drive.id)) %></strong></td>
110110
<td class="text-right"><%= view_button_to product_drive_path(product_drive.id) %></td>
111111
</tr>
112112
<% end %>

0 commit comments

Comments
 (0)