Skip to content

Commit c86f218

Browse files
committed
Refactor after review
1 parent 245a805 commit c86f218

File tree

4 files changed

+59
-48
lines changed

4 files changed

+59
-48
lines changed

app/controllers/distributions_by_county_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ def report
66
setup_date_range_picker
77
start_date = helpers.selected_range.first.iso8601
88
end_date = helpers.selected_range.last.iso8601
9-
@breakdown = DistributionSummaryByCountyQuery.new(
9+
@breakdown = DistributionSummaryByCountyQuery.call(
1010
organization_id: current_organization.id,
1111
start_date: start_date,
1212
end_date: end_date
13-
).call
13+
)
1414
end
1515
end
Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
class DistributionSummaryByCountyQuery
2-
DISTRIBUTION_BY_COUNTY_SQL = <<~SQL.squish.freeze
2+
CountySummary = Data.define(:name, :quantity, :value)
3+
4+
# No need to send comments in the query
5+
SQL_MULTILINE_COMMENTS = /\/\*.*?\*\//
6+
7+
DISTRIBUTION_BY_COUNTY_SQL = <<~SQL.squish.gsub(SQL_MULTILINE_COMMENTS, "").freeze
38
/* Calculate total item quantity and value per distribution */
49
WITH distribution_totals AS
510
(
611
SELECT DISTINCT d.id,
712
d.partner_id,
813
COALESCE(SUM(li.quantity) OVER (PARTITION BY d.id), 0) AS quantity,
9-
COALESCE(SUM(COALESCE(i.value_in_cents, 0) * li.quantity) OVER (PARTITION BY d.id), 0) AS amount
14+
COALESCE(SUM(COALESCE(i.value_in_cents, 0) * li.quantity) OVER (PARTITION BY d.id), 0) AS value
1015
FROM distributions d
1116
JOIN line_items li ON li.itemizable_id = d.id AND li.itemizable_type = 'Distribution'
1217
JOIN items i ON i.id = li.item_id
@@ -21,7 +26,7 @@ class DistributionSummaryByCountyQuery
2126
(
2227
SELECT dt.id,
2328
dt.quantity,
24-
dt.amount,
29+
dt.value,
2530
COALESCE(psa.client_share::float / 100, 1) AS percentage,
2631
COALESCE(c.name, 'Unspecified') county_name,
2732
COALESCE(c.region, 'ZZZ') county_region
@@ -34,7 +39,7 @@ class DistributionSummaryByCountyQuery
3439
even if all distributions have an associated county */
3540
SELECT 0 AS id,
3641
0 AS quantity,
37-
0 AS amount,
42+
0 AS value,
3843
1 AS percentage,
3944
'Unspecified' AS county_name,
4045
'ZZZ' AS county_region
@@ -43,36 +48,42 @@ class DistributionSummaryByCountyQuery
4348
so we cast to an integer for rounding purposes */
4449
SELECT tbc.county_name AS name,
4550
SUM((tbc.quantity * percentage)::int) AS quantity,
46-
SUM((tbc.amount * percentage)::int) AS amount
51+
SUM((tbc.value * percentage)::int) AS value
4752
FROM totals_by_county tbc
4853
GROUP BY county_name, county_region
4954
ORDER BY county_region ASC;
5055
SQL
5156

52-
def initialize(organization_id:, start_date: nil, end_date: nil)
53-
@organization_id = organization_id
54-
@start_date = start_date || "1000-01-01"
55-
@end_date = end_date || "3000-01-01"
56-
end
57+
class << self
58+
def call(organization_id:, start_date:, end_date:)
59+
params = {
60+
organization_id: organization_id,
61+
start_date: start_date || "1000-01-01",
62+
end_date: end_date || "3000-01-01"
63+
}
5764

58-
def call
59-
execute(to_sql(DISTRIBUTION_BY_COUNTY_SQL)).to_a
60-
end
65+
execute(to_sql(DISTRIBUTION_BY_COUNTY_SQL, **params)).to_a.map(&to_county_summary)
66+
end
6167

62-
private
68+
private
6369

64-
def execute(sql)
65-
ActiveRecord::Base.connection.execute(sql)
66-
end
70+
def execute(sql)
71+
ActiveRecord::Base.connection.execute(sql)
72+
end
6773

68-
def to_sql(query)
69-
ActiveRecord::Base.sanitize_sql_array(
70-
[
71-
query,
72-
organization_id: @organization_id,
73-
start_date: @start_date,
74-
end_date: @end_date
75-
]
76-
)
74+
def to_sql(query, organization_id:, start_date:, end_date:)
75+
ActiveRecord::Base.sanitize_sql_array(
76+
[
77+
query,
78+
organization_id: organization_id,
79+
start_date: start_date,
80+
end_date: end_date
81+
]
82+
)
83+
end
84+
85+
def to_county_summary
86+
->(params) { CountySummary.new(**params) }
87+
end
7788
end
7889
end

app/views/distributions_by_county/report.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
<tbody>
5151
<% @breakdown.each do |bd| %>
5252
<tr>
53-
<td><%= bd["name"] %></td>
54-
<td class="numeric"><%= number_with_delimiter(bd["quantity"]) %></td>
55-
<td class="numeric"><%= dollar_presentation(bd["amount"]) %></td>
53+
<td><%= bd.name %></td>
54+
<td class="numeric"><%= number_with_delimiter(bd.quantity) %></td>
55+
<td class="numeric"><%= dollar_presentation(bd.value) %></td>
5656
</tr>
5757
<% end %>
5858

spec/queries/distribution_summary_by_county_query_spec.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,47 @@
2828
describe "call" do
2929
it "will have 100% unspecified shows if no served_areas" do
3030
create(:distribution, :with_items, item: item_1, organization: organization)
31-
breakdown = DistributionSummaryByCountyQuery.new(**params).call
31+
breakdown = DistributionSummaryByCountyQuery.call(**params)
3232
expect(breakdown.size).to eq(1)
33-
expect(breakdown[0]["quantity"]).to eq(100)
34-
expect(breakdown[0]["amount"]).to be_within(0.01).of(105000.0)
33+
expect(breakdown[0].quantity).to eq(100)
34+
expect(breakdown[0].value).to be_within(0.01).of(105000.0)
3535
end
3636

3737
it "divides the item numbers and values according to the partner profile" do
3838
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_1)
39-
breakdown = DistributionSummaryByCountyQuery.new(**params).call
39+
breakdown = DistributionSummaryByCountyQuery.call(**params)
4040
expect(breakdown.size).to eq(5)
41-
expect(breakdown[4]["quantity"]).to eq(0)
42-
expect(breakdown[4]["amount"]).to be_within(0.01).of(0)
41+
expect(breakdown[4].quantity).to eq(0)
42+
expect(breakdown[4].value).to be_within(0.01).of(0)
4343
3.times do |i|
44-
expect(breakdown[i]["quantity"]).to eq(25)
45-
expect(breakdown[i]["amount"]).to be_within(0.01).of(26250.0)
44+
expect(breakdown[i].quantity).to eq(25)
45+
expect(breakdown[i].value).to be_within(0.01).of(26250.0)
4646
end
4747
end
4848

4949
it "handles multiple partners with overlapping service areas properly" do
5050
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_1, issued_at: now)
5151
create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_2, issued_at: now)
52-
breakdown = DistributionSummaryByCountyQuery.new(**params).call
52+
breakdown = DistributionSummaryByCountyQuery.call(**params)
5353
num_with_45 = 0
5454
num_with_20 = 0
5555
num_with_0 = 0
5656
# The result will have at least 1 45 and at least 1 20, and 1 0. Anything else will be either 45 or 25 or 20
5757
breakdown.each do |sa|
58-
if sa["quantity"] == 45
59-
expect(sa["amount"]).to be_within(0.01).of(47250.0)
58+
if sa.quantity == 45
59+
expect(sa.value).to be_within(0.01).of(47250.0)
6060
num_with_45 += 1
6161
end
6262

63-
if sa["quantity"] == 25
64-
expect(sa["amount"]).to be_within(0.01).of(26250.0)
63+
if sa.quantity == 25
64+
expect(sa.value).to be_within(0.01).of(26250.0)
6565
end
66-
if sa["quantity"] == 20
67-
expect(sa["amount"]).to be_within(0.01).of(21000.0)
66+
if sa.quantity == 20
67+
expect(sa.value).to be_within(0.01).of(21000.0)
6868
num_with_20 += 1
6969
end
70-
if sa["quantity"] == 0
71-
expect(sa["amount"]).to be_within(0.01).of(0)
70+
if sa.quantity == 0
71+
expect(sa.value).to be_within(0.01).of(0)
7272
end
7373
end
7474
expect(num_with_45).to be > 0

0 commit comments

Comments
 (0)