Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indicator fields to partner export #4843

Merged
2 changes: 2 additions & 0 deletions app/models/distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Distribution < ApplicationRecord
enum state: { scheduled: 5, complete: 10 }
enum delivery_method: { pick_up: 0, delivery: 1, shipped: 2 }
scope :active, -> { joins(:line_items).joins(:items).where(items: { active: true }) }
scope :with_diapers, -> { joins(line_items: :item).merge(Item.disposable.or(Item.cloth_diapers)) }
scope :with_period_supplies, -> { joins(line_items: :item).merge(Item.period_supplies) }
# add item_id scope to allow filtering distributions by item
scope :by_item_id, ->(item_id) { includes(:items).where(items: { id: item_id }) }
# partner scope to allow filtering by partner
Expand Down
16 changes: 14 additions & 2 deletions app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ def self.csv_export_headers
"Contact Name",
"Contact Phone",
"Contact Email",
"Notes"
"Notes",
"Providing Diapers",
"Providing Period Supplies"
]
end

Expand All @@ -202,10 +204,20 @@ def csv_export_attributes
contact_person[:name],
contact_person[:phone],
contact_person[:email],
notes
notes,
providing_diapers,
providing_period_supplies
]
end

def providing_diapers
distributions.with_diapers.any? ? "Y" : "N"
end

def providing_period_supplies
distributions.with_period_supplies.any? ? "Y" : "N"
end

def contact_person
return @contact_person if @contact_person

Expand Down
48 changes: 47 additions & 1 deletion spec/models/partner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@
let(:agency_type) { Partner::AGENCY_TYPES["OTHER"] }
let(:other_agency_type) { "Another Agency Name" }
let(:notes) { "Some notes" }
let(:providing_diapers) { "N" }
let(:providing_period_supplies) { "N" }

before do
partner.profile.update({
Expand Down Expand Up @@ -330,9 +332,53 @@
contact_name,
contact_phone,
contact_email,
notes
notes,
providing_diapers,
providing_period_supplies
])
end

context "when partner has a distribution" do
let(:providing_diapers) { "Y" }
let(:providing_period_supplies) { "Y" }
let(:distribution) { create(:distribution, partner: partner) }

shared_examples "providing_diapers check" do |scope|
before do
case scope
when :disposable
item = create(:item, base_item: create(:base_item, category: "Diapers - Childrens"))
when :cloth_diapers
item = create(:item, base_item: create(:base_item, category: "Diapers - Cloth (Kids)"))
end

create(:line_item, item: item, itemizable: distribution)
end

it "should have Y as providing_diapers" do
expect(partner.csv_export_attributes[12]).to eq(providing_diapers)
end
end

context "with a disposable item" do
include_examples "providing_diapers check", :disposable
end

context "with a cloth diaper item" do
include_examples "providing_diapers check", :cloth_diapers
end

context "with a period supplies item" do
before do
item = create(:item, base_item: create(:base_item, category: "Menstrual Supplies/Items"))
create(:line_item, item: item, itemizable: distribution)
end

it "should have Y as providing_period_supplies" do
expect(partner.csv_export_attributes[13]).to eq(providing_period_supplies)
end
end
end
end

describe '#quantity_year_to_date' do
Expand Down
Loading