diff --git a/app/models/partner.rb b/app/models/partner.rb index 2e7b5ab77b..52029a444a 100644 --- a/app/models/partner.rb +++ b/app/models/partner.rb @@ -185,7 +185,8 @@ def self.csv_export_headers "Contact Name", "Contact Phone", "Contact Email", - "Notes" + "Notes", + "Counties Served" ] end @@ -202,7 +203,8 @@ def csv_export_attributes contact_person[:name], contact_person[:phone], contact_person[:email], - notes + notes, + profile.county_list_by_region ] end diff --git a/app/models/partners/profile.rb b/app/models/partners/profile.rb index 3fd9fcb397..271ec146b5 100644 --- a/app/models/partners/profile.rb +++ b/app/models/partners/profile.rb @@ -91,6 +91,7 @@ class Profile < Base has_many :served_areas, foreign_key: "partner_profile_id", class_name: "Partners::ServedArea", dependent: :destroy, inverse_of: :partner_profile + has_many :counties, through: :served_areas accepts_nested_attributes_for :served_areas, allow_destroy: true has_many_attached :documents @@ -125,6 +126,11 @@ def split_pick_up_emails pick_up_email.split(/,|\s+/).compact_blank end + def county_list_by_region + # provides a county list in case insensitive alpha order, by region, then county name + counties.order(%w(lower(region) lower(name))).pluck(:name).join("; ") + end + private def check_social_media diff --git a/spec/models/partner_spec.rb b/spec/models/partner_spec.rb index c632759004..5364f872d3 100644 --- a/spec/models/partner_spec.rb +++ b/spec/models/partner_spec.rb @@ -317,7 +317,16 @@ partner.update(notes: notes) end - it "should has the info in the columns order" do + it "should have the expected info in the columns order" do + county_1 = create(:county, name: "High County, Maine", region: "Maine") + county_2 = create(:county, name: "laRue County, Louisiana", region: "Louisiana") + county_3 = create(:county, name: "Ste. Anne County, Louisiana", region: "Louisiana") + create(:partners_served_area, partner_profile: partner.profile, county: county_1, client_share: 50) + create(:partners_served_area, partner_profile: partner.profile, county: county_2, client_share: 40) + create(:partners_served_area, partner_profile: partner.profile, county: county_3, client_share: 10) + partner.profile.reload # not sure if this is needed + # county ordering is a bit esoteric -- it is human alphabetical by county within region (region is state) + correctly_ordered_counties = "laRue County, Louisiana; Ste. Anne County, Louisiana; High County, Maine" expect(partner.csv_export_attributes).to eq([ partner.name, partner.email, @@ -330,7 +339,8 @@ contact_name, contact_phone, contact_email, - notes + notes, + correctly_ordered_counties ]) end end diff --git a/spec/models/partners/profile_spec.rb b/spec/models/partners/profile_spec.rb index 80a6cb0c05..f82cb7e976 100644 --- a/spec/models/partners/profile_spec.rb +++ b/spec/models/partners/profile_spec.rb @@ -307,6 +307,25 @@ end end + describe "county_list" do + it "provides a county list in human-alpha by county within region order" do + county_1 = create(:county, name: "High County, Maine", region: "Maine") + county_2 = create(:county, name: "laRue County, Louisiana", region: "Louisiana") + county_3 = create(:county, name: "Ste. Anne County, Louisiana", region: "Louisiana") + county_4 = create(:county, name: "Other County, Louisiana", region: "Louisiana") + profile = create(:partner_profile) + profile_2 = create(:partner_profile) + create(:partners_served_area, partner_profile: profile, county: county_1, client_share: 50) + create(:partners_served_area, partner_profile: profile, county: county_2, client_share: 40) + create(:partners_served_area, partner_profile: profile, county: county_3, client_share: 10) + create(:partners_served_area, partner_profile: profile_2, county: county_4) + profile.reload + profile_2.reload + ## This is human-alpha by county within region (i.e. state) + expect(profile.county_list_by_region).to eq("laRue County, Louisiana; Ste. Anne County, Louisiana; High County, Maine") + end + end + describe "versioning" do it { is_expected.to be_versioned } end