-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9109e68
commit 5faf849
Showing
17 changed files
with
814 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
ActiveAdmin.register ASCOR::Pathway do | ||
config.sort_order = 'country_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 | ||
|
||
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' | ||
|
||
index do | ||
column :country | ||
column :assessment_date | ||
column :emissions_metric | ||
column :emissions_boundary | ||
column :land_use | ||
column :units | ||
|
||
actions | ||
end | ||
|
||
show do | ||
attributes_table do | ||
row :id | ||
row :country | ||
row :assessment_date | ||
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 :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 :assessment_date, as: :datepicker | ||
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 :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::Pathway.select(:emissions).flat_map(&:emissions_all_years).uniq.sort | ||
|
||
column :id | ||
column(:country) { |b| b.country.name } | ||
column(:assessment_date) { |b| b.assessment_date.strftime '%m/%d/%y' } | ||
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 | ||
year_columns.map do |year| | ||
column year do |benchmark| | ||
benchmark.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class ASCOR::EmissionsMetric | ||
VALUES = [ | ||
'Absolute', | ||
'Per capital emissions', | ||
'Per capita emissions', | ||
'PPP-adjusted GDP emissions' | ||
].freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class ASCOR::Pathway < ApplicationRecord | ||
include HasEmissions | ||
|
||
belongs_to :country, class_name: 'ASCOR::Country', foreign_key: :country_id | ||
|
||
validates_presence_of :emissions_metric, :emissions_boundary, :land_use, :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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module CSVImport | ||
class ASCORPathways < BaseImporter | ||
include Helpers | ||
|
||
def import | ||
import_each_csv_row(csv) do |row| | ||
pathway = prepare_pathway(row) | ||
|
||
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') | ||
|
||
was_new_record = pathway.new_record? | ||
any_changes = pathway.changed? | ||
|
||
pathway.save! | ||
|
||
update_import_results(was_new_record, any_changes) | ||
end | ||
end | ||
|
||
private | ||
|
||
def resource_klass | ||
ASCOR::Pathway | ||
end | ||
|
||
def required_headers | ||
[:id] | ||
end | ||
|
||
def prepare_pathway(row) | ||
find_record_by(:id, row) || | ||
ASCOR::Pathway.find_or_initialize_by( | ||
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 | ||
|
||
def countries | ||
@countries ||= ASCOR::Country.all.group_by(&:name) | ||
end | ||
|
||
def assessment_date(row) | ||
CSVImport::DateUtils.safe_parse!(row[:assessment_date], ['%Y-%m-%d', '%m/%d/%y']) if row[:assessment_date] | ||
end | ||
|
||
def publication_date(row) | ||
CSVImport::DateUtils.safe_parse!(row[:publication_date], ['%Y-%m']) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class CreateASCORPathways < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :ascor_pathways do |t| | ||
t.references :country, null: false, foreign_key: { to_table: :ascor_countries } | ||
t.string :emissions_metric | ||
t.string :emissions_boundary | ||
t.string :land_use | ||
t.string :units | ||
t.date :assessment_date | ||
t.date :publication_date, null: true | ||
t.integer :last_reported_year, null: true | ||
t.string :trend_1_year, null: true | ||
t.string :trend_3_year, null: true | ||
t.string :trend_5_year, null: true | ||
t.jsonb :emissions, default: {} | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Oops, something went wrong.