generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2263 from UKGovernmentBEIS/feature/2932-add-iati-…
…scope-to-xml (2932) Add IATI Scope to xml
- Loading branch information
Showing
8 changed files
with
166 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Iati::ActivityScopeService | ||
# see vendor/data/beis_organisation_references/codelists/BEIS/benefitting_regions.yml | ||
REGION_LEVEL_CODE = 1 | ||
|
||
SCOPES = { | ||
national: 4, | ||
regional: 2, | ||
multi_national: 3 | ||
}.freeze | ||
|
||
def initialize(benefitting_country_codes) | ||
@benefitting_country_codes = benefitting_country_codes | ||
end | ||
|
||
def call | ||
return false unless @benefitting_country_codes.present? | ||
|
||
return SCOPES[:national] if benefitting_countries.count == 1 && benefitting_regions.count == 1 | ||
return SCOPES[:regional] if benefitting_countries.count > 1 && benefitting_regions.count == 1 | ||
return SCOPES[:multi_national] if benefitting_countries.count > 1 && benefitting_regions.count > 1 | ||
end | ||
|
||
private def benefitting_countries | ||
return [] if @benefitting_country_codes.nil? | ||
|
||
@_benefitting_countries ||= @benefitting_country_codes.map do |country_code| | ||
BenefittingCountry.find_by_code(country_code) | ||
end | ||
end | ||
|
||
private def benefitting_regions | ||
level = BenefittingRegion::Level.find_by_code(REGION_LEVEL_CODE) | ||
|
||
@_benefitting_regions ||= benefitting_countries.map do |benefitting_country| | ||
benefitting_country.regions.select { |region| region.level == level } | ||
end | ||
|
||
@_benefitting_regions.uniq.flatten | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Iati::ActivityScopeService do | ||
describe "#call" do | ||
context "when there is a single benefitting country in one region" do | ||
it "returns the IATI code for national: 4" do | ||
country_codes = ["DZ"] | ||
|
||
expect(described_class.new(country_codes).call).to eql 4 | ||
end | ||
end | ||
|
||
context "when there are multiple benefitting countries in one region" do | ||
it "returns the IATI code for regional: 2" do | ||
country_codes = ["DZ", "MA", "EG"] | ||
|
||
expect(described_class.new(country_codes).call).to eql 2 | ||
end | ||
end | ||
|
||
context "when there are multiple benefitting countries across multiple regions" do | ||
it "returns the IATI code for multi-national: 3" do | ||
country_codes = ["DZ", "SV", "MY"] | ||
|
||
expect(described_class.new(country_codes).call).to eql 3 | ||
end | ||
end | ||
|
||
context "when the activity has no benefitting countries" do | ||
it "returns false" do | ||
country_codes = [] | ||
|
||
expect(described_class.new(country_codes).call).to eql false | ||
end | ||
end | ||
|
||
context "when the activity benefitting countries is nil" do | ||
it "returns false" do | ||
country_codes = nil | ||
|
||
expect(described_class.new(country_codes).call).to eql false | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters