Skip to content

Commit

Permalink
feat: add counts for wdpa and oecm national designations to tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
yuelongh committed Jan 5, 2024
1 parent f560c67 commit 03d87f2
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
8 changes: 7 additions & 1 deletion app/controllers/country_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class CountryController < ApplicationController
after_action :enable_caching
before_action :load_essential_vars
before_action :build_stats, only: :show
before_action :calculate_national_designations_counts, only: :show

include MapHelper
include CountriesHelper
Expand Down Expand Up @@ -69,7 +70,6 @@ def get_number_of_national_desinitions
number_of_national_desinitions = 0
designations_list = @stats_data[:wdpa][:designations][:designations]
designations_list.each do |designation|

total = designation[:total]
number_of_national_desinitions = total if designation[:type] == 'National' && total.is_a?(Integer)
end
Expand All @@ -81,6 +81,12 @@ def has_oecms
@total_oecm.positive?
end

def calculate_national_designations_counts
# ['National'] -> all avaliable juriidctions are in /app/presenters/designations_presenter.rb
@wdpa_national_designations_count = @country_presenter.get_designations_list(['National'], is_oecm: false).count
@oecm_national_designations_count = @country_presenter.get_designations_list(['National'], is_oecm: true).count
end

def build_hash(tab)
hash = {}

Expand Down
19 changes: 18 additions & 1 deletion app/models/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,24 @@ def protected_areas_per_designation(jurisdictions=[], exclude_oecms: false)
GROUP BY designations.name
""")
end

def designations_list_by_wdpa_or_oecm(jurisdictions:[], is_oecm: false)
ActiveRecord::Base.connection.execute("""
SELECT protected_areas.wdpa_id, protected_areas.designation_id,protected_areas.name as protected_areas_name,
protected_areas.designation_id,protected_areas.is_oecm, protected_areas.is_dopa,
countries_protected_areas.country_id,designations.jurisdiction_id,
jurisdictions.name as jurisdiction_name
FROM protected_areas
INNER JOIN countries_protected_areas
ON protected_areas.id = countries_protected_areas.protected_area_id
AND countries_protected_areas.country_id = #{self.id}
INNER JOIN designations
ON protected_areas.designation_id = designations.id
INNER JOIN jurisdictions
ON designations.jurisdiction_id = jurisdictions.id
and jurisdictions.id IN (#{jurisdictions.pluck(:id).join(',') if jurisdictions.any?})
WHERE protected_areas.is_oecm = #{is_oecm}
""")
end
def protected_areas_per_jurisdiction(exclude_oecms: false)
ActiveRecord::Base.connection.execute("""
SELECT jurisdictions.name, COUNT(*)
Expand Down
4 changes: 4 additions & 0 deletions app/presenters/country_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def designations(exclude_oecms: false)
@designations_presenter.designations(exclude_oecms: exclude_oecms)
end

def get_designations_list(jurisdictions, is_oecm: false)
@designations_presenter.designations_list(jurisdictions: jurisdictions, is_oecm: is_oecm)
end

def documents
[
national_report,
Expand Down
11 changes: 9 additions & 2 deletions app/presenters/designations_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DesignationsPresenter
def initialize(geo_entity)
@geo_entity = geo_entity
end

# All avaliable JURISDICTIONS
JURISDICTIONS = ['National', 'Regional', 'International', 'Not Applicable'].freeze
JURISDICTIONS_TITLE = {
'National' => 'National',
Expand All @@ -27,6 +27,13 @@ def designations(exclude_oecms: false)
end
end

def designations_list(jurisdictions: [], is_oecm: false)
jurisdictions = get_jurisdictions(jurisdictions)
return [] unless jurisdictions.any?

geo_entity.designations_list_by_wdpa_or_oecm(jurisdictions: jurisdictions, is_oecm: is_oecm)
end

private

attr_reader :geo_entity
Expand All @@ -45,7 +52,7 @@ def get_jurisdiction(jurisdiction)

def designation_title(jurisdiction)
jurisdiction_title = JURISDICTIONS_TITLE[jurisdiction]
jurisdiction_title ? "#{jurisdiction_title} designations": 'Designation Title Not Found'
jurisdiction_title ? "#{jurisdiction_title} designations" : 'Designation Title Not Found'
end

def all_pas(exclude_oecms)
Expand Down
4 changes: 3 additions & 1 deletion app/views/country/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
total_oecm: @total_oecm,
total_pame: @total_pame,
total_wdpa: @total_wdpa,
number_of_national_desinitions: @number_of_national_desinitions
number_of_national_desinitions: @number_of_national_desinitions,
wdpa_national_designations_count: @wdpa_national_designations_count,
oecm_national_designations_count: @oecm_national_designations_count
} %>
<div class="page__map map--country">
<%= render partial: "partials/maps/header", locals: {
Expand Down
10 changes: 8 additions & 2 deletions app/views/partials/stats/_stats-overview.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
<span class="card__tooltip--designations">
<span><%= t('stats.tooltip.designations') %></span>
<span>=</span>
<span><%= local_assigns[:number_of_national_desinitions] %></span>
<span>
<%# Search For calculate_national_designations_counts to see how it's done in the backend %>
<%= wdpa_national_designations_count %>
</span>
</span>
</div>
</template>
Expand Down Expand Up @@ -111,7 +114,10 @@
<span class="card__tooltip--designations">
<span><%= t('stats.tooltip.designations') %></span>
<span>=</span>
<span><%= local_assigns[:number_of_national_desinitions] %></span>
<span>
<%# Search For calculate_national_designations_counts to see how it's done in the backend %>
<%= oecm_national_designations_count %>
</span>
</span>
</div>
</template>
Expand Down

0 comments on commit 03d87f2

Please sign in to comment.