Skip to content

Commit

Permalink
refactoring: ASCOR pathways model
Browse files Browse the repository at this point in the history
  • Loading branch information
martintomas committed Sep 21, 2023
1 parent ccc8461 commit c7951dc
Show file tree
Hide file tree
Showing 19 changed files with 346 additions and 391 deletions.
2 changes: 1 addition & 1 deletion app/admin/ascor/assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
index do
selectable_column
id_column
column :country
column :country, sortable: 'ascor_countries.name'
column :assessment_date
column :publication_date
column :notes
Expand Down
2 changes: 1 addition & 1 deletion app/admin/ascor/benchmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
index do
selectable_column
id_column
column :country
column :country, sortable: 'ascor_countries.name'
column :emissions_metric
column :emissions_boundary
column :units
Expand Down
8 changes: 4 additions & 4 deletions app/admin/ascor/countries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
attributes_table do
row :id
row :name
row :iso, label: 'Country ISO code'
row 'Country ISO code', &:iso
row :region
row :wb_lending_group, label: 'World Bank lending group'
row :fiscal_monitor_category, label: 'International Monetary Fund fiscal monitor category'
row :type_of_party, label: 'Type of Party to the United Nations Framework Convention on Climate Change'
row 'World Bank lending group', &:wb_lending_group
row 'International Monetary Fund fiscal monitor category', &:fiscal_monitor_category
row 'Type of Party to the United Nations Framework Convention on Climate Change', &:type_of_party
end

active_admin_comments
Expand Down
61 changes: 36 additions & 25 deletions app/admin/ascor/pathways.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
ActiveAdmin.register ASCOR::Pathway do
config.sort_order = 'country_id_asc'
config.sort_order = 'id_asc'
includes :country

menu label: 'Pathways', parent: 'ASCOR', priority: 3

permit_params :country_id, :publication_date, :assessment_date, :emissions_metric, :emissions_boundary, :land_use, :units,
:emissions, :last_reported_year, :trend_1_year, :trend_3_year, :trend_5_year
permit_params :country_id, :publication_date, :assessment_date, :emissions_metric, :emissions_boundary, :units,
:emissions, :last_historical_year, :trend_1_year, :trend_3_year, :trend_5_year, :trend_source, :trend_year,
:recent_emission_level, :recent_emission_source, :recent_emission_year

filter :country, as: :select, collection: -> { ASCOR::Country.all.order(:name) }
filter :assessment_date, as: :select, collection: -> { ASCOR::Pathway.pluck(:assessment_date).uniq }
filter :emissions_metric, as: :select, collection: -> { ASCOR::EmissionsMetric::VALUES }
filter :emissions_boundary, as: :select, collection: -> { ASCOR::EmissionsBoundary::VALUES }
filter :land_use, as: :select, collection: -> { ASCOR::LandUse::VALUES }

data_export_sidebar 'ASCORPathways', display_name: 'ASCOR Pathways'

index do
selectable_column
id_column
column :country
column :country, sortable: 'ascor_countries.name'
column :assessment_date
column :emissions_metric
column :emissions_boundary
column :land_use
column :units

actions
Expand All @@ -36,17 +35,21 @@
row :publication_date
row :emissions_metric
row :emissions_boundary
row :land_use
row :units
row :last_reported_year
row :trend_1_year
row :trend_3_year
row :trend_5_year
row :last_historical_year
row 'metric EP1.a.i', &:recent_emission_level
row 'source EP1.a.i', &:recent_emission_source
row 'year EP1.a.i', &:recent_emission_year
row 'metric EP1.a.ii 1-year', &:trend_1_year
row 'metric EP1.a.ii 3-year', &:trend_3_year
row 'metric EP1.a.ii 5-year', &:trend_5_year
row 'source metric EP1.a.ii', &:trend_source
row 'year metric EP1.a.ii', &:trend_year
row :created_at
row :updated_at
end

panel 'Benchmark emission values' do
panel 'Pathway emission values' do
render 'admin/cp/emissions_table', emissions: resource.emissions
end

Expand All @@ -62,17 +65,21 @@
f.input :publication_date, as: :datepicker
f.input :emissions_metric, as: :select, collection: ASCOR::EmissionsMetric::VALUES
f.input :emissions_boundary, as: :select, collection: ASCOR::EmissionsBoundary::VALUES
f.input :land_use, as: :select, collection: ASCOR::LandUse::VALUES
f.input :units
f.input :last_reported_year
f.input :trend_1_year
f.input :trend_3_year
f.input :trend_5_year
f.input :last_historical_year
f.input :recent_emission_level, label: 'metric EP1.a.i'
f.input :recent_emission_source, label: 'source EP1.a.i'
f.input :recent_emission_year, label: 'year EP1.a.i'
f.input :trend_1_year, label: 'metric EP1.a.ii 1-year'
f.input :trend_3_year, label: 'metric EP1.a.ii 3-year'
f.input :trend_5_year, label: 'metric EP1.a.ii 5-year'
f.input :trend_source, label: 'source metric EP1.a.ii'
f.input :trend_year, label: 'year metric EP1.a.ii'
f.input :emissions, as: :hidden, input_html: {value: f.object.emissions.to_json, id: 'input_emissions'}
end

div class: 'panel' do
h3 'Benchmark emission values'
h3 'Pathway emission values'
div class: 'panel-contents padding-20' do
render 'admin/cp/emissions_table_edit', f: f
end
Expand All @@ -90,16 +97,20 @@
column(:publication_date) { |b| b.publication_date.to_s(:year_month) }
column :emissions_metric
column :emissions_boundary
column :land_use
column :units
column :last_reported_year
column :last_historical_year
column 'metric EP1.a.i', humanize_name: false, &:recent_emission_level
column 'source EP1.a.i', humanize_name: false, &:recent_emission_source
column 'year EP1.a.i', humanize_name: false, &:recent_emission_year
column 'metric EP1.a.ii 1-year', humanize_name: false, &:trend_1_year
column 'metric EP1.a.ii 3-year', humanize_name: false, &:trend_3_year
column 'metric EP1.a.ii 5-year', humanize_name: false, &:trend_5_year
column 'source metric EP1.a.ii', humanize_name: false, &:trend_source
column 'year metric EP1.a.ii', humanize_name: false, &:trend_year
year_columns.map do |year|
column year do |benchmark|
benchmark.emissions[year]
column year do |resource|
resource.emissions[year]
end
end
column '1-year trend', &:trend_1_year
column '3-year trend', &:trend_3_year
column '5-year trend', &:trend_5_year
end
end
2 changes: 0 additions & 2 deletions app/models/ascor/emissions_boundary.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class ASCOR::EmissionsBoundary
VALUES = [
'Production',
'Consumption',
'Production - excluding LULUCF',
'Production - only LULUCF',
'Consumption - excluding LULUCF',
Expand Down
2 changes: 0 additions & 2 deletions app/models/ascor/emissions_metric.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
class ASCOR::EmissionsMetric
VALUES = [
'Absolute',
'Per capita emissions',
'PPP-adjusted GDP emissions',
'Intensity per capita',
'Intensity per GDP-PPP'
].freeze
Expand Down
6 changes: 0 additions & 6 deletions app/models/ascor/land_use.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/models/ascor/pathway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ class ASCOR::Pathway < ApplicationRecord

belongs_to :country, class_name: 'ASCOR::Country', foreign_key: :country_id

validates_presence_of :emissions_metric, :emissions_boundary, :land_use, :units, :assessment_date
validates_presence_of :emissions_metric, :emissions_boundary, :units, :assessment_date
validates :emissions_metric, inclusion: {in: ASCOR::EmissionsMetric::VALUES}, allow_nil: true
validates :emissions_boundary, inclusion: {in: ASCOR::EmissionsBoundary::VALUES}, allow_nil: true
validates :land_use, inclusion: {in: ASCOR::LandUse::VALUES}, allow_nil: true
end
17 changes: 10 additions & 7 deletions app/services/csv_import/ascor_pathways.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ def import
pathway.country = countries[row[:country]].first if row.header?(:country)
pathway.emissions_metric = row[:emissions_metric] if row.header?(:emissions_metric)
pathway.emissions_boundary = row[:emissions_boundary] if row.header?(:emissions_boundary)
pathway.land_use = row[:land_use] if row.header?(:land_use)
pathway.units = row[:units] if row.header?(:units)
pathway.assessment_date = assessment_date(row) if row.header?(:assessment_date)
pathway.publication_date = publication_date(row) if row.header?(:publication_date)
pathway.last_reported_year = row[:last_reported_year] if row.header?(:last_reported_year)
pathway.emissions = parse_emissions(row) if emission_headers?(row)
pathway.trend_1_year = row[:'1year_trend'] if row.header?(:'1year_trend')
pathway.trend_3_year = row[:'3year_trend'] if row.header?(:'3year_trend')
pathway.trend_5_year = row[:'5year_trend'] if row.header?(:'5year_trend')
pathway.last_historical_year = row[:last_historical_year] if row.header?(:last_historical_year)
pathway.trend_1_year = row[:metric_ep1aii_1year] if row.header?(:metric_ep1aii_1year)
pathway.trend_3_year = row[:metric_ep1aii_3year] if row.header?(:metric_ep1aii_3year)
pathway.trend_5_year = row[:metric_ep1aii_5year] if row.header?(:metric_ep1aii_5year)
pathway.trend_source = row[:source_metric_ep1aii] if row.header?(:source_metric_ep1aii)
pathway.trend_year = row[:year_metric_ep1aii] if row.header?(:year_metric_ep1aii)
pathway.recent_emission_level = string_to_float(row[:metric_ep1ai]) if row.header?(:metric_ep1ai)
pathway.recent_emission_source = row[:source_metric_ep1ai] if row.header?(:source_metric_ep1ai)
pathway.recent_emission_year = row[:year_metric_ep1ai] if row.header?(:year_metric_ep1ai)
pathway.emissions = parse_emissions(row, thousands_separator: ',') if emission_headers?(row)

was_new_record = pathway.new_record?
any_changes = pathway.changed?
Expand All @@ -44,7 +48,6 @@ def prepare_pathway(row)
country: countries[row[:country]].first,
emissions_metric: row[:emissions_metric],
emissions_boundary: row[:emissions_boundary],
land_use: row[:land_use],
assessment_date: assessment_date(row)
)
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/csv_import/helpers/emissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Emissions

def parse_emissions(row, thousands_separator: '')
row.headers.grep(EMISSION_YEAR_PATTERN).reduce({}) do |acc, year|
next acc unless row[year].present?
next acc if row[year].blank? || row[year] == 'NA'

acc.merge(year.to_s.to_i => string_to_float(row[year], thousands_separator: thousands_separator))
end
Expand Down
10 changes: 9 additions & 1 deletion db/migrate/20230920083300_refactor_ascor_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ def change
add_column :ascor_countries, :type_of_party, :string, null: true
### ASCOR Benchmarks ###
remove_column :ascor_benchmarks, :land_use
### ASCOR Pathways ###
remove_column :ascor_pathways, :land_use
add_column :ascor_pathways, :trend_source, :string, null: true
add_column :ascor_pathways, :trend_year, :integer, null: true
add_column :ascor_pathways, :recent_emission_level, :float, null: true
add_column :ascor_pathways, :recent_emission_source, :string, null: true
add_column :ascor_pathways, :recent_emission_year, :integer, null: true
rename_column :ascor_pathways, :last_reported_year, :last_historical_year
### ASCOR Assessment Indicators ###
add_column :ascor_assessment_indicators, :units_or_response_type, :string, null: true
### ASCOR Assessments ###
Expand All @@ -14,6 +22,6 @@ def change
remove_column :ascor_assessment_results, :source_date
remove_column :ascor_assessment_results, :source_link
add_column :ascor_assessment_results, :source, :string, null: true
add_column :ascor_assessment_results, :year, :string, null: true
add_column :ascor_assessment_results, :year, :integer, null: true
end
end
Loading

0 comments on commit c7951dc

Please sign in to comment.