Skip to content

Commit e3acbe0

Browse files
5248 combined purchased from (#5267)
* Refactor diaper report service to combine purchased_from and vendors_purchased_from into a single method * Refactor combined_purchased_from method to use compact_blank and update specs for merged output
1 parent 222064d commit e3acbe0

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

app/services/reports/diaper_report_service.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ def report
3232
"% disposable diapers purchased" => "#{percent_disposable_diapers_purchased.round}%",
3333
"% cloth diapers purchased" => "#{percent_cloth_diapers_purchased.round}%",
3434
"Money spent purchasing diapers" => number_to_currency(money_spent_on_diapers),
35-
"Purchased from" => purchased_from,
36-
"Vendors diapers purchased through" => vendors_purchased_from
35+
"Purchased from" => combined_purchased_from
3736
}}
3837
end
3938

39+
# Helper method to combine purchased_from and vendors_purchased_from
40+
def combined_purchased_from
41+
[purchased_from, vendors_purchased_from].compact_blank.join(", ")
42+
end
43+
4044
# @return [Integer]
4145
def distributed_loose_disposable_diapers
4246
@distributed_loose_disposable_diapers ||= organization
@@ -53,10 +57,10 @@ def distributed_disposable_diapers_from_kits
5357

5458
sql_query = <<-SQL
5559
SELECT SUM(line_items.quantity * kit_line_items.quantity)
56-
FROM distributions
57-
INNER JOIN line_items ON line_items.itemizable_type = 'Distribution' AND line_items.itemizable_id = distributions.id
58-
INNER JOIN items ON items.id = line_items.item_id
59-
INNER JOIN kits ON kits.id = items.kit_id
60+
FROM distributions
61+
INNER JOIN line_items ON line_items.itemizable_type = 'Distribution' AND line_items.itemizable_id = distributions.id
62+
INNER JOIN items ON items.id = line_items.item_id
63+
INNER JOIN kits ON kits.id = items.kit_id
6064
INNER JOIN line_items AS kit_line_items ON kits.id = kit_line_items.itemizable_id
6165
INNER JOIN items AS kit_items ON kit_items.id = kit_line_items.item_id
6266
WHERE distributions.organization_id = ?

spec/services/reports/diaper_report_service_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@
171171
"% disposable diapers purchased" => "22%",
172172
"% cloth diapers purchased" => "11%",
173173
"Money spent purchasing diapers" => "$60.00",
174-
"Purchased from" => "Google, Walmart",
175-
"Vendors diapers purchased through" => "Vendor 1, Vendor 2"},
174+
"Purchased from" => "Google, Walmart, Vendor 1, Vendor 2"},
176175
name: "Diapers"
177176
})
178177
end

0 commit comments

Comments
 (0)