From a6abc44cf4030ca10df099eddafa32032551d6b0 Mon Sep 17 00:00:00 2001 From: David Kane Date: Tue, 24 Oct 2017 16:41:31 +0100 Subject: [PATCH 1/2] Fix fund admin --- app/admin/fund.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/admin/fund.rb b/app/admin/fund.rb index 91b49c26..40af30a4 100644 --- a/app/admin/fund.rb +++ b/app/admin/fund.rb @@ -9,7 +9,7 @@ :country_distribution, :sources, :national, :org_type_distribution, :income_distribution, :slug, :beneficiary_distribution, :grant_examples, - :geographic_scale_limited, :geo_area, + :geographic_scale_limited, :geo_area_id, :min_amount_awarded_limited, :min_amount_awarded, :max_amount_awarded_limited, :max_amount_awarded, :min_duration_awarded_limited, :min_duration_awarded, @@ -181,7 +181,7 @@ def find_resource end inputs 'Geography' do - f.input :geo_area, collection: GeoArea.pluck(:name, :id) + f.input :geo_area, input_html: { class: 'chosen-select' } f.input :geographic_scale_limited f.input :national end From cf89923725849f09d5a1bf50156e9f40dd329f10 Mon Sep 17 00:00:00 2001 From: David Kane Date: Tue, 24 Oct 2017 16:41:42 +0100 Subject: [PATCH 2/2] Add districts check to geo_area --- app/models/geo_area.rb | 12 ++++++++++++ spec/models/geo_areas_spec.rb | 10 ++++++++++ spec/services/check/eligibility/location_spec.rb | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/models/geo_area.rb b/app/models/geo_area.rb index b0c5693f..af007085 100644 --- a/app/models/geo_area.rb +++ b/app/models/geo_area.rb @@ -7,7 +7,19 @@ class GeoArea < ApplicationRecord validates :name, presence: true, uniqueness: true validates :countries, presence: true + validate :validate_districts + def short_name self[:short_name].present? ? self[:short_name] : self[:name] end + + private + + def validate_districts + return if districts.empty? + districts.each do |district| + errors.add(:districts, "Country '#{district.country.name}' for #{district.name} not selected") if + country_ids.exclude?(district.country_id) + end + end end diff --git a/spec/models/geo_areas_spec.rb b/spec/models/geo_areas_spec.rb index f20b4e95..a5bf02d6 100644 --- a/spec/models/geo_areas_spec.rb +++ b/spec/models/geo_areas_spec.rb @@ -18,5 +18,15 @@ @area.update(name: nil) expect(@area).not_to be_valid end + + it 'districts must be from countries' do + @area.update(districts: @db[:uk_districts], countries: [@db[:kenya]]) + expect(@area).not_to be_valid + end + + it 'districts must be from countries valid' do + @area.update(districts: @db[:uk_districts], countries: [@db[:uk]]) + expect(@area).to be_valid + end end end diff --git a/spec/services/check/eligibility/location_spec.rb b/spec/services/check/eligibility/location_spec.rb index f3bcb33a..8026fc8c 100644 --- a/spec/services/check/eligibility/location_spec.rb +++ b/spec/services/check/eligibility/location_spec.rb @@ -54,7 +54,7 @@ end it '#call countries_ineligible?' do - @local.geo_area.update!(countries: [@db[:kenya]]) + @local.geo_area.update!(countries: [@db[:kenya]], districts: [@db[:kenya_districts].first]) expect(subject.call(@proposal, @local)).to eq 'eligible' => false end