Skip to content

Commit

Permalink
Merge pull request #444 from Vizzuality/refactoring/ascor-models
Browse files Browse the repository at this point in the history
refactoring: ASCOR models
  • Loading branch information
martintomas authored Sep 21, 2023
2 parents 1974573 + c7951dc commit 4c42b14
Show file tree
Hide file tree
Showing 41 changed files with 689 additions and 623 deletions.
3 changes: 3 additions & 0 deletions app/admin/ascor/assessment_indicators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
row :code
row :indicator_type
row :text
row :units_or_response_type
row :created_at
row :updated_at
end
Expand All @@ -33,6 +34,7 @@
f.input :indicator_type, as: :select, collection: ASCOR::AssessmentIndicator::INDICATOR_TYPES
f.input :code
f.input :text
f.input :units_or_response_type
end

f.actions
Expand All @@ -51,5 +53,6 @@
column :type, &:indicator_type
column :code
column :text
column :units_or_response_type
end
end
58 changes: 26 additions & 32 deletions app/admin/ascor/assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@

menu label: 'Assessments', parent: 'ASCOR', priority: 5

permit_params :country_id, :assessment_date, :publication_date, :research_notes, :further_information,
results_attributes: [:id, :assessment_id, :indicator_id, :answer, :source_name, :source_date,
:source_link, :_destroy]
permit_params :country_id, :assessment_date, :publication_date, :notes,
results_attributes: [:id, :assessment_id, :indicator_id, :answer, :source, :_destroy]

filter :country
filter :assessment_date, as: :select

data_export_sidebar 'ASCORAssessments', display_name: 'ASCOR Assessments'

index do
column :country
selectable_column
id_column
column :country, sortable: 'ascor_countries.name'
column :assessment_date
column :publication_date
column :research_notes
column :notes

actions
end
Expand All @@ -28,8 +29,7 @@
row :country
row :assessment_date
row :publication_date
row :research_notes
row :further_information
row :notes
row :created_at
row :updated_at
end
Expand All @@ -38,9 +38,8 @@
table_for resource.results.includes(:indicator).order(:id) do
column(:indicator)
column(:answer)
column(:source_name)
column(:source_date)
column(:source_link)
column(:source)
column(:year)
end
end

Expand All @@ -54,17 +53,15 @@
f.input :country, as: :select, collection: ASCOR::Country.all.order(:name)
f.input :assessment_date, as: :datepicker
f.input :publication_date, as: :datepicker
f.input :research_notes
f.input :further_information
f.input :notes
end

f.has_many :results, allow_destroy: true, heading: false do |ff|
ff.inputs 'Assessment Results' do
ff.input :indicator, as: :select, collection: ASCOR::AssessmentIndicator.all.order(:code)
ff.input :answer
ff.input :source_name
ff.input :source_date
ff.input :source_link
ff.input :source
ff.input :year
end
end

Expand All @@ -78,31 +75,28 @@
end
column :assessment_date
column :publication_date
ASCOR::AssessmentIndicator.order(:id).all.each do |indicator|
if indicator.indicator_type.in? %w[area indicator]
column "#{indicator.indicator_type.capitalize} #{indicator.code}", humanize_name: false do |resource|
controller.assessment_results[[resource.id, indicator.id]]&.first&.answer
end
ASCOR::AssessmentIndicator.where.not(indicator_type: 'pillar')
.where.not(code: %w[EP.1.a.i EP.1.a.ii]).order(:id).all.each do |indicator|
column "#{indicator.indicator_type} #{indicator.code}", humanize_name: false do |resource|
controller.assessment_results[[resource.id, indicator.id]]&.first&.answer
end
next unless indicator.indicator_type.in? %w[indicator custom_indicator]

column "Source name #{indicator.code}", humanize_name: false do |resource|
controller.assessment_results[[resource.id, indicator.indicator_type, indicator.code]]&.first&.source_name
end
column "Source date #{indicator.code}", humanize_name: false do |resource|
controller.assessment_results[[resource.id, indicator.indicator_type, indicator.code]]&.first&.source_date
end
ASCOR::AssessmentIndicator.where(indicator_type: %w[indicator metric]).order(:id).all.each do |indicator|
column "source #{indicator.indicator_type} #{indicator.code}", humanize_name: false do |resource|
controller.assessment_results[[resource.id, indicator.id]]&.first&.source
end
column "Source link #{indicator.code}", humanize_name: false do |resource|
controller.assessment_results[[resource.id, indicator.indicator_type, indicator.code]]&.first&.source_link
end
ASCOR::AssessmentIndicator.where(indicator_type: 'metric').order(:id).all.each do |indicator|
column "year #{indicator.indicator_type} #{indicator.code}", humanize_name: false do |resource|
controller.assessment_results[[resource.id, indicator.id]]&.first&.year
end
end
column :research_notes
column :further_information
column :notes
end

controller do
def assessment_results
@assessment_results ||= ASCOR::AssessmentResult.group_by { |r| [r.assessment_id, r.indicator_id] }
@assessment_results ||= ASCOR::AssessmentResult.all.group_by { |r| [r.assessment_id, r.indicator_id] }
end
end
end
12 changes: 4 additions & 8 deletions app/admin/ascor/benchmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@

menu label: 'Benchmarks', parent: 'ASCOR', priority: 2

permit_params :country_id, :publication_date, :emissions_metric, :emissions_boundary, :land_use, :units, :benchmark_type,
:emissions
permit_params :country_id, :publication_date, :emissions_metric, :emissions_boundary, :units, :benchmark_type, :emissions

filter :country, as: :select, collection: -> { ASCOR::Country.all.order(:name) }
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 }
filter :benchmark_type, as: :select, collection: -> { ASCOR::BenchmarkType::VALUES }

data_export_sidebar 'ASCORBenchmarks', display_name: 'ASCOR Benchmarks'

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

Expand All @@ -33,7 +32,6 @@
row :publication_date
row :emissions_metric
row :emissions_boundary
row :land_use
row :units
row :benchmark_type
row :created_at
Expand All @@ -55,7 +53,6 @@
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 :benchmark_type, as: :select, collection: ASCOR::BenchmarkType::VALUES
f.input :emissions, as: :hidden, input_html: {value: f.object.emissions.to_json, id: 'input_emissions'}
Expand All @@ -79,7 +76,6 @@
column(:publication_date) { |b| b.publication_date.to_s(:year_month) }
column :emissions_metric
column :emissions_boundary
column :land_use
column :units
column :benchmark_type

Expand Down
14 changes: 10 additions & 4 deletions app/admin/ascor/countries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

menu label: 'Countries', parent: 'ASCOR', priority: 1

permit_params :name, :iso, :region, :wb_lending_group, :fiscal_monitor_category
permit_params :name, :iso, :region, :wb_lending_group, :fiscal_monitor_category, :type_of_party

filter :iso_contains, label: 'ISO'
filter :name_contains, label: 'Name'
Expand All @@ -13,6 +13,8 @@
data_export_sidebar 'ASCORCountries', display_name: 'ASCOR Countries'

index do
selectable_column
id_column
column :name
column 'Country ISO code', :iso
column :region
Expand All @@ -24,10 +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 '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 All @@ -43,6 +46,8 @@
f.input :wb_lending_group, as: :select, collection: ASCOR::Country::LENDING_GROUPS, label: 'World Bank lending group'
f.input :fiscal_monitor_category, as: :select, collection: ASCOR::Country::MONITOR_CATEGORIES,
label: 'International Monetary Fund fiscal monitor category'
f.input :type_of_party, as: :select, collection: ASCOR::Country::TYPE_OF_PARTY,
label: 'Type of Party to the United Nations Framework Convention on Climate Change'
end

f.actions
Expand All @@ -55,5 +60,6 @@
column :region
column 'World Bank lending group', humanize_name: false, &:wb_lending_group
column 'International Monetary Fund fiscal monitor category', humanize_name: false, &:fiscal_monitor_category
column 'Type of Party to the United Nations Framework Convention on Climate Change', humanize_name: false, &:type_of_party
end
end
63 changes: 38 additions & 25 deletions app/admin/ascor/pathways.rb
Original file line number Diff line number Diff line change
@@ -1,26 +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
column :country
selectable_column
id_column
column :country, sortable: 'ascor_countries.name'
column :assessment_date
column :emissions_metric
column :emissions_boundary
column :land_use
column :units

actions
Expand All @@ -34,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 @@ -60,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 @@ -88,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: 1 addition & 1 deletion app/models/ascor/assessment_indicator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ASCOR::AssessmentIndicator < ApplicationRecord
INDICATOR_TYPES = %w[pillar area indicator custom_indicator].freeze
INDICATOR_TYPES = %w[pillar area indicator metric].freeze
enum indicator_type: array_to_enum_hash(INDICATOR_TYPES)

has_many :results, class_name: 'ASCOR::AssessmentResult', foreign_key: :indicator_id, dependent: :destroy
Expand Down
3 changes: 1 addition & 2 deletions app/models/ascor/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ class ASCOR::Benchmark < ApplicationRecord

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

validates_presence_of :emissions_metric, :emissions_boundary, :land_use, :units, :benchmark_type
validates_presence_of :emissions_metric, :emissions_boundary, :units, :benchmark_type
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
validates :benchmark_type, inclusion: {in: ASCOR::BenchmarkType::VALUES}, allow_nil: true
end
5 changes: 5 additions & 0 deletions app/models/ascor/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class ASCOR::Country < ApplicationRecord
'Emerging market economies',
'Low-income developing countries'
].freeze
TYPE_OF_PARTY = [
'Annex I',
'Non-Annex I'
].freeze

friendly_id :name, use: [:slugged, :history], routes: :default

Expand All @@ -31,6 +35,7 @@ class ASCOR::Country < ApplicationRecord
validates :region, inclusion: {in: REGIONS}, allow_nil: true
validates :wb_lending_group, inclusion: {in: LENDING_GROUPS}, allow_nil: true
validates :fiscal_monitor_category, inclusion: {in: MONITOR_CATEGORIES}, allow_nil: true
validates :type_of_party, inclusion: {in: TYPE_OF_PARTY}, allow_nil: true

def path
Rails.application.routes.url_helpers.tpi_ascor_path slug
Expand Down
Loading

0 comments on commit 4c42b14

Please sign in to comment.