Skip to content

Commit 95154fa

Browse files
committed
Only use distributions issued in the last 12 months
1 parent 3bd0af2 commit 95154fa

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

app/models/distribution.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class Distribution < ApplicationRecord
7373
where("issued_at > :start_date AND issued_at <= :end_date",
7474
start_date: Time.zone.today.beginning_of_week.beginning_of_day, end_date: Time.zone.today.end_of_week.end_of_day)
7575
end
76+
scope :in_last_12_months, -> do
77+
where("issued_at > :start_date AND issued_at <= :end_date",
78+
start_date: 12.months.ago.beginning_of_day, end_date: Time.zone.today.end_of_day)
79+
end
7680

7781
delegate :name, to: :partner, prefix: true
7882

app/models/partner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@ def csv_export_attributes
211211
end
212212

213213
def providing_diapers
214-
distributions.with_diapers.any? ? "Y" : "N"
214+
distributions.in_last_12_months.with_diapers.any? ? "Y" : "N"
215215
end
216216

217217
def providing_period_supplies
218-
distributions.with_period_supplies.any? ? "Y" : "N"
218+
distributions.in_last_12_months.with_period_supplies.any? ? "Y" : "N"
219219
end
220220

221221
def contact_person

spec/models/partner_spec.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@
338338
])
339339
end
340340

341-
context "when partner has a distribution" do
341+
context "when partner has a distribution in the last 12 months" do
342342
let(:providing_diapers) { "Y" }
343343
let(:providing_period_supplies) { "Y" }
344344
let(:distribution) { create(:distribution, partner: partner) }
@@ -379,6 +379,27 @@
379379
end
380380
end
381381
end
382+
383+
context "when partner only has distribution older than a 12 months" do
384+
let(:distribution) { create(:distribution, issued_at: 24.months.ago.beginning_of_day, partner: partner) }
385+
let(:disposable_diapers_item) { create(:item, base_item: create(:base_item, category: "Diapers - Childrens")) }
386+
let(:cloth_diapers_item) { create(:item, base_item: create(:base_item, category: "Diapers - Cloth (Kids)")) }
387+
let(:period_supplies_item) { create(:item, base_item: create(:base_item, category: "Menstrual Supplies/Items")) }
388+
389+
before do
390+
create(:line_item, item: disposable_diapers_item, itemizable: distribution)
391+
create(:line_item, item: cloth_diapers_item, itemizable: distribution)
392+
create(:line_item, item: period_supplies_item, itemizable: distribution)
393+
end
394+
395+
it "should have N as providing_diapers" do
396+
expect(partner.csv_export_attributes[12]).to eq(providing_diapers)
397+
end
398+
399+
it "should have N as providing_period_supplies" do
400+
expect(partner.csv_export_attributes[13]).to eq(providing_period_supplies)
401+
end
402+
end
382403
end
383404

384405
describe '#quantity_year_to_date' do

0 commit comments

Comments
 (0)