Skip to content

Commit

Permalink
populated homepage cards with number of land and marine PAs, and crea…
Browse files Browse the repository at this point in the history
…ted a presenter
  • Loading branch information
stanleypliu committed Aug 4, 2020
1 parent 03c9786 commit 1c47b36
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 54 deletions.
57 changes: 3 additions & 54 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class HomeController < ApplicationController
include MapHelper

def index
@presenter = HomePresenter.new

@pa_coverage_percentage = 9999 #TODO Total PA coverage in %

@config_search_areas = {
Expand All @@ -13,60 +15,7 @@ def index
@pas_button = home_yml[:pas][:button]
@pas_levels = levels

@site_facts = [
{
percentage: 00, #total percentage coverage of terrestrial pas
theme: I18n.t('home.facts')[0][:theme],
title: I18n.t('home.facts')[0][:title],
totals: [
{
number: 00, #total terrestrial pas
text: I18n.t('home.total_pas')
}
]
},
{
percentage: 00, #total percentage coverage of marine pas
theme: I18n.t('home.facts')[1][:theme],
title: I18n.t('home.facts')[1][:title],
totals: [
{
number: 00, #total marine pas
text: I18n.t('home.total_pas')
}
]
},
{
percentage: 00, #total percentage coverage of terrestrial pas and OECMs
theme: I18n.t('home.facts')[2][:theme],
title: I18n.t('home.facts')[2][:title],
totals: [
{
number: 00, #total terrestrial pas
text: I18n.t('home.total_pas')
},
{
number: 00, #total terrestrial oecms
text: I18n.t('home.total_oecms')
}
]
},
{
percentage: 00, #total percentage coverage of marine pas and OECMs
theme: I18n.t('home.facts')[3][:theme],
title: I18n.t('home.facts')[3][:title],
totals: [
{
number: 00, #total marine pas
text: I18n.t('home.total_pas')
},
{
number: 00, #total marine oecms
text: I18n.t('home.total_oecms')
}
]
}
]
@site_facts = @presenter.fact_card_stats

comfy_themes = Comfy::Cms::Page.find_by_slug("thematical-areas")
@themes_title = comfy_themes.label
Expand Down
87 changes: 87 additions & 0 deletions app/presenters/home_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class HomePresenter
include ActionView::Helpers::NumberHelper

def initialize
end

def terrestrial_pas
@terrestrial_pas ||= number_with_delimiter(Stats::Global.terrestrial_pa_count)
end

def marine_pas
@marine_pas ||= number_with_delimiter(Stats::Global.marine_pa_count)
end

def terrestrial_oecms
@terrestrial_oecms = number_with_delimiter(Stats::Global.terrestrial_oecm_count)
end

def marine_oecms
@marine_oecms = number_with_delimiter(Stats::Global.marine_oecm_count)
end

def terrestrial_cover
@terrestrial_cover = CountryStatistic.global_percentage_pa_land_cover
end

def marine_cover
@marine_cover = CountryStatistic.global_percentage_pa_marine_cover
end

def fact_card_stats
[
{
percentage: terrestrial_cover,
theme: I18n.t('home.facts')[0][:theme],
title: I18n.t('home.facts')[0][:title],
totals: [
{
number: terrestrial_pas,
text: I18n.t('home.total_pas')
}
]
},
{
percentage: marine_cover,
theme: I18n.t('home.facts')[1][:theme],
title: I18n.t('home.facts')[1][:title],
totals: [
{
number: marine_pas,
text: I18n.t('home.total_pas')
}
]
},
{
percentage: 00, #total percentage coverage of terrestrial pas and OECMs
theme: I18n.t('home.facts')[2][:theme],
title: I18n.t('home.facts')[2][:title],
totals: [
{
number: terrestrial_pas,
text: I18n.t('home.total_pas')
},
{
number: terrestrial_oecms,
text: I18n.t('home.total_oecms')
}
]
},
{
percentage: 00, #total percentage coverage of marine pas and OECMs
theme: I18n.t('home.facts')[3][:theme],
title: I18n.t('home.facts')[3][:title],
totals: [
{
number: marine_pas,
text: I18n.t('home.total_pas')
},
{
number: marine_oecms,
text: I18n.t('home.total_oecms')
}
]
}
]
end
end
28 changes: 28 additions & 0 deletions lib/modules/stats/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ def self.pa_count
ProtectedArea.count
end

def self.terrestrial_pa_count
ProtectedArea.where(marine: false).count
end

def self.marine_pa_count
ProtectedArea.where(marine: true).count
end

def self.terrestrial_oecm_count
ProtectedArea.where(marine: false, is_oecm: true).count
end

def self.marine_oecm_count
ProtectedArea.where(marine: true, is_oecm: true).count
end



def self.percentage_pa_cover
RegionalStatistic.joins(:region)
.where('regions.iso' => 'GLOBAL')
Expand Down Expand Up @@ -48,4 +66,14 @@ def self.calculate_stats_for(klass, field_name)
def self.global_area
CountryStatistic.global_land_area + CountryStatistic.global_marine_area
end

def self.percentage_land_cover
CountryStatistic.global_percentage_pa_land_cover
end

def self.percentage_marine_cover
CountryStatistic.global_percentage_pa_marine_cover
end


end

0 comments on commit 1c47b36

Please sign in to comment.