diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index be8bc38c4..9acd88121 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -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 = { @@ -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 diff --git a/app/presenters/home_presenter.rb b/app/presenters/home_presenter.rb new file mode 100644 index 000000000..bdfae40d7 --- /dev/null +++ b/app/presenters/home_presenter.rb @@ -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 \ No newline at end of file diff --git a/lib/modules/stats/global.rb b/lib/modules/stats/global.rb index da2c1c7f7..2c1827cfc 100644 --- a/lib/modules/stats/global.rb +++ b/lib/modules/stats/global.rb @@ -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') @@ -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