Skip to content

Commit

Permalink
Merge pull request #443 from Vizzuality/feat/ascor-models
Browse files Browse the repository at this point in the history
ASCOR models + importers and Administration
  • Loading branch information
martintomas authored Sep 19, 2023
2 parents 115c38c + 686fcac commit 1974573
Show file tree
Hide file tree
Showing 75 changed files with 3,110 additions and 13 deletions.
55 changes: 55 additions & 0 deletions app/admin/ascor/assessment_indicators.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
ActiveAdmin.register ASCOR::AssessmentIndicator do
config.sort_order = 'id_asc'

menu label: 'Assessment Indicators', parent: 'ASCOR', priority: 4

actions :all, except: [:new, :create]

filter :code
filter :text
filter :indicator_type, as: :check_boxes, collection: ASCOR::AssessmentIndicator::INDICATOR_TYPES

data_export_sidebar 'ASCORAssessmentIndicators', display_name: 'ASCOR AssessmentIndicators'

permit_params :code, :indicator_type, :text

show do
attributes_table do
row :id
row :code
row :indicator_type
row :text
row :created_at
row :updated_at
end

active_admin_comments
end

form html: {'data-controller' => 'check-modified'} do |f|
f.semantic_errors(*f.object.errors.keys)

f.inputs do
f.input :indicator_type, as: :select, collection: ASCOR::AssessmentIndicator::INDICATOR_TYPES
f.input :code
f.input :text
end

f.actions
end

index do
id_column
column :indicator_type
column :code
column :text
actions
end

csv do
column :id
column :type, &:indicator_type
column :code
column :text
end
end
108 changes: 108 additions & 0 deletions app/admin/ascor/assessments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
ActiveAdmin.register ASCOR::Assessment do
config.sort_order = 'assessment_date_desc'
includes :country

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]

filter :country
filter :assessment_date, as: :select

data_export_sidebar 'ASCORAssessments', display_name: 'ASCOR Assessments'

index do
column :country
column :assessment_date
column :publication_date
column :research_notes

actions
end

show do
attributes_table do
row :id
row :country
row :assessment_date
row :publication_date
row :research_notes
row :further_information
row :created_at
row :updated_at
end

panel 'Assessment Results' do
table_for resource.results.includes(:indicator).order(:id) do
column(:indicator)
column(:answer)
column(:source_name)
column(:source_date)
column(:source_link)
end
end

active_admin_comments
end

form do |f|
f.semantic_errors(*f.object.errors.keys)

f.inputs do
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
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
end
end

f.actions
end

csv do
column :id
column :country do |resource|
resource.country.name
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
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
column "Source link #{indicator.code}", humanize_name: false do |resource|
controller.assessment_results[[resource.id, indicator.indicator_type, indicator.code]]&.first&.source_link
end
end
column :research_notes
column :further_information
end

controller do
def assessment_results
@assessment_results ||= ASCOR::AssessmentResult.group_by { |r| [r.assessment_id, r.indicator_id] }
end
end
end
92 changes: 92 additions & 0 deletions app/admin/ascor/benchmarks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
ActiveAdmin.register ASCOR::Benchmark do
config.sort_order = 'country_id_asc'
includes :country

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

permit_params :country_id, :publication_date, :emissions_metric, :emissions_boundary, :land_use, :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
column :emissions_metric
column :emissions_boundary
column :land_use
column :units
column :benchmark_type

actions
end

show do
attributes_table do
row :id
row :country
row :publication_date
row :emissions_metric
row :emissions_boundary
row :land_use
row :units
row :benchmark_type
row :created_at
row :updated_at
end

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

active_admin_comments
end

form html: {'data-controller' => 'check-modified with-emission-table-form'} do |f|
f.semantic_errors(*f.object.errors.keys)

f.inputs do
f.input :country, as: :select, collection: ASCOR::Country.all.order(:name)
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'}
end

div class: 'panel' do
h3 'Benchmark emission values'
div class: 'panel-contents padding-20' do
render 'admin/cp/emissions_table_edit', f: f
end
end

f.actions
end

csv do
year_columns = ASCOR::Benchmark.select(:emissions).flat_map(&:emissions_all_years).uniq.sort

column :id
column(:country) { |b| b.country.name }
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

year_columns.map do |year|
column year do |benchmark|
benchmark.emissions[year]
end
end
end
end
59 changes: 59 additions & 0 deletions app/admin/ascor/countries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
ActiveAdmin.register ASCOR::Country do
config.batch_actions = false
config.sort_order = 'name_asc'

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

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

filter :iso_contains, label: 'ISO'
filter :name_contains, label: 'Name'
filter :region, as: :check_boxes, collection: proc { Geography::REGIONS }

data_export_sidebar 'ASCORCountries', display_name: 'ASCOR Countries'

index do
column :name
column 'Country ISO code', :iso
column :region

actions
end

show do
attributes_table do
row :id
row :name
row :iso, label: 'Country ISO code'
row :region
row :wb_lending_group, label: 'World Bank lending group'
row :fiscal_monitor_category, label: 'International Monetary Fund fiscal monitor category'
end

active_admin_comments
end

form do |f|
semantic_errors(*f.object.errors.attribute_names)

f.inputs do
f.input :name
f.input :iso, label: 'Country ISO code'
f.input :region, as: :select, collection: ASCOR::Country::REGIONS
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'
end

f.actions
end

csv do
column :id
column :name
column 'Country ISO code', humanize_name: false, &:iso
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
end
end
Loading

0 comments on commit 1974573

Please sign in to comment.