diff --git a/.travis.yml b/.travis.yml index f033096..489a04c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +dist: trusty language: ruby before_install: - sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e0d814..bcc2b4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +### 0.2.0 + +**Features** + +* Add new endpoint for ACP countries (BIOPAMA) protected areas + +**Bug fixes** + +* Make sure Travis build passes by specifying 'trusty' distribution + ### 0.1.1 **Add new fields to Protected Areas JSON** diff --git a/api/v3/protected_areas.rb b/api/v3/protected_areas.rb index 7272a2e..0f615eb 100644 --- a/api/v3/protected_areas.rb +++ b/api/v3/protected_areas.rb @@ -51,6 +51,20 @@ class API::V3::ProtectedAreas < Grape::API @protected_areas = paginate(collection) end + # == annotations + ################ + desc "Get ACP countries protected areas." + params { optional :with_geometry, default: false, type: Boolean } + # == body + ######### + get :biopama, rabl: "v3/views/protected_areas" do + collection = ProtectedArea.biopama.with_pame_evaluations + collection = collection.without_geometry unless params[:with_geometry] + + @with_geometry = params[:with_geometry] + @protected_areas = collection + end + # == annotations ################ desc "Get a protected area via its wdpa_id." diff --git a/db b/db index 5ffe513..8665cdd 160000 --- a/db +++ b/db @@ -1 +1 @@ -Subproject commit 5ffe5139a09298e415515166c4bc98ce0a0ee648 +Subproject commit 8665cdde184024a825fd74e3d0b3f17fd22a2bbf diff --git a/models/protected_area.rb b/models/protected_area.rb index ce0fb3b..aef12f1 100644 --- a/models/protected_area.rb +++ b/models/protected_area.rb @@ -30,6 +30,9 @@ class ProtectedArea < ActiveRecord::Base delegate :jurisdiction, to: :designation, allow_nil: true + scope :biopama, -> { joins(:countries).where("countries.is_biopama IS TRUE").distinct } + scope :with_pame_evaluations, -> { joins(:pame_evaluations).where("pame_evaluations.id IS NOT NULL").distinct } + SEARCHES = { country: -> (scope, value) { scope.joins(:countries).where("countries.iso_3 = ?", value.upcase) }, marine: -> (scope, value) { scope.where(marine: value) }, diff --git a/test/api/v3/protected_areas_test.rb b/test/api/v3/protected_areas_test.rb index 431ae2c..0af2e17 100644 --- a/test/api/v3/protected_areas_test.rb +++ b/test/api/v3/protected_areas_test.rb @@ -119,4 +119,26 @@ def test_get_protected_areas_search_wants_at_least_one_param refute last_response.ok? assert last_response.status == 400 end + + def test_get_protected_areas_biopama_returns_only_acp_countries_areas + create(:protected_area, :with_pame_evaluation, name: "Mandalia Plains") + create(:protected_area, :biopama_country, :with_pame_evaluation, name: "Darjeeling") + create(:protected_area, :biopama_country, :with_pame_evaluation, name: "Not Marine") + + get_with_rabl "/v3/protected_areas/biopama" + + assert last_response.ok? + assert_equal(2, @json_response["protected_areas"].size) + end + + def test_get_protected_areas_biopama_returns_only_acp_countries_areas_with_pame_evaluations + create(:protected_area, :biopama_country, name: "Mandalia Plains") + create(:protected_area, :biopama_country, :with_pame_evaluation, name: "Darjeeling") + create(:protected_area, :biopama_country, :with_pame_evaluation, name: "Not Marine") + + get_with_rabl "/v3/protected_areas/biopama" + + assert last_response.ok? + assert_equal(2, @json_response["protected_areas"].size) + end end diff --git a/test/factories/pame_evaluations.rb b/test/factories/pame_evaluations.rb new file mode 100644 index 0000000..eff92c8 --- /dev/null +++ b/test/factories/pame_evaluations.rb @@ -0,0 +1,4 @@ +FactoryGirl.define do + factory :pame_evaluation do + end +end diff --git a/test/factories/protected_areas.rb b/test/factories/protected_areas.rb index 76e906d..6b08119 100644 --- a/test/factories/protected_areas.rb +++ b/test/factories/protected_areas.rb @@ -8,5 +8,17 @@ association :iucn_category, factory: :iucn_category, name: 'My IUCN category' association :legal_status, factory: :legal_status, name: 'My legal status' association :governance, factory: :governance, name: 'My governance' + + trait :biopama_country do + after(:create) do |protected_area| + create(:country, protected_areas: [protected_area], is_biopama: true) + end + end + + trait :with_pame_evaluation do + after(:create) do |protected_area| + create(:pame_evaluation, protected_area: protected_area) + end + end end end