Skip to content

Commit

Permalink
feat: Adding visibility status to ASCOR countries
Browse files Browse the repository at this point in the history
  • Loading branch information
martintomas committed Feb 6, 2024
1 parent 4248075 commit 55588b5
Show file tree
Hide file tree
Showing 24 changed files with 87 additions and 44 deletions.
5 changes: 4 additions & 1 deletion 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, :type_of_party
permit_params :name, :iso, :region, :wb_lending_group, :fiscal_monitor_category, :type_of_party, :visibility_status

filter :iso_contains, label: 'ISO'
filter :name_contains, label: 'Name'
Expand All @@ -18,6 +18,7 @@
column :name
column 'Country ISO code', :iso
column :region
tag_column :visibility_status

actions
end
Expand All @@ -31,6 +32,7 @@
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
row :visibility_status
end

active_admin_comments
Expand All @@ -48,6 +50,7 @@
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'
f.input :visibility_status, as: :select, collection: ASCOR::Country::VISIBILITY
end

f.actions
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/tpi/ascor_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ def user_download
private

def fetch_ascor_countries
@countries = ASCOR::Country.all.order(:name)
@countries = ASCOR::Country.published.all.order(:name)
@countries_json = [
{name: 'All countries', path: tpi_ascor_index_path},
*@countries.as_json(only: [:name], methods: [:path])
]
end

def fetch_ascor_country
@country = ASCOR::Country.friendly.find(params[:id])
@country = ASCOR::Country.published.friendly.find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to tpi_ascor_index_path
end

def fetch_assessment_date
Expand Down
1 change: 1 addition & 0 deletions app/models/ascor/assessment_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ class ASCOR::AssessmentResult < ApplicationRecord

scope :of_type, ->(type) { includes(:indicator).where(ascor_assessment_indicators: {indicator_type: type}) }
scope :by_date, ->(date) { includes(:assessment).where(ascor_assessments: {assessment_date: date}) }
scope :published, -> { includes(assessment: :country).where(ascor_countries: {visibility_status: 'published'}) }
end
2 changes: 2 additions & 0 deletions app/models/ascor/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
# created_at :datetime not null
# updated_at :datetime not null
# type_of_party :string
# visibility_status :string default("draft")
#
class ASCOR::Country < ApplicationRecord
include VisibilityStatus
extend FriendlyId

REGIONS = [
Expand Down
2 changes: 1 addition & 1 deletion app/services/api/ascor/bubble_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def call
::ASCOR::AssessmentResult
.by_date(@assessment_date)
.of_type(:area)
.includes(assessment: :country)
.published
.order(:indicator_id)
.map do |result|
{
Expand Down
4 changes: 2 additions & 2 deletions app/services/api/ascor/emissions_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def collect_metadata

def countries
@countries ||= if country_ids.blank?
::ASCOR::Country.where(iso: ::ASCOR::Country::DEFAULT_COUNTRIES)
::ASCOR::Country.published.where(iso: ::ASCOR::Country::DEFAULT_COUNTRIES)
else
::ASCOR::Country.where(id: country_ids)
::ASCOR::Country.published.where(id: country_ids)
end
end

Expand Down
3 changes: 2 additions & 1 deletion app/services/csv_export/ascor/assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def year_values_for(assessment)
end

def assessments
@assessments ||= ::ASCOR::Assessment.joins(:country).includes(:country).order(:assessment_date, 'ascor_countries.name')
@assessments ||= ::ASCOR::Assessment.joins(:country).includes(:country)
.where(ascor_countries: {visibility_status: 'published'}).order(:assessment_date, 'ascor_countries.name')
end

def assessment_results
Expand Down
3 changes: 2 additions & 1 deletion app/services/csv_export/ascor/benchmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def year_columns
end

def benchmarks
@benchmarks ||= ::ASCOR::Benchmark.joins(:country).includes(:country).order('ascor_countries.name')
@benchmarks ||= ::ASCOR::Benchmark.joins(:country).includes(:country)
.where(ascor_countries: {visibility_status: 'published'}).order('ascor_countries.name')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/csv_export/ascor/countries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def call
private

def countries
@countries ||= ::ASCOR::Country.order(:name)
@countries ||= ::ASCOR::Country.published.order(:name)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions app/services/csv_export/ascor/pathways.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def year_columns

def pathways
@pathways ||= ::ASCOR::Pathway.joins(:country).includes(:country)
.where(ascor_countries: {visibility_status: 'published'})
.order(:assessment_date, 'ascor_countries.name')
end
end
Expand Down
1 change: 1 addition & 0 deletions app/services/csv_import/ascor_countries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def import
if row.header?(:type_of_party_to_the_united_nations_framework_convention_on_climate_change)
country.type_of_party = row[:type_of_party_to_the_united_nations_framework_convention_on_climate_change]
end
country.visibility_status = row[:visibility_status]&.downcase if row.header?(:visibility_status)

was_new_record = country.new_record?
any_changes = country.changed?
Expand Down
4 changes: 2 additions & 2 deletions app/views/tpi/ascor/_emissions_chart.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
default_emissions_metric_filter: 'Absolute',
emissions_boundary_filter: ASCOR::EmissionsBoundary::VALUES,
default_emissions_boundary_filter: 'Production - excluding LULUCF',
countries: ASCOR::Country.all.sort_by(&:name).map { |c| { id: c.id, iso: c.iso, name: c.name } },
default_countries: ASCOR::Country.where(iso: ASCOR::Country::DEFAULT_COUNTRIES).map(&:id),
countries: ASCOR::Country.published.all.sort_by(&:name).map { |c| { id: c.id, iso: c.iso, name: c.name } },
default_countries: ASCOR::Country.published.where(iso: ASCOR::Country::DEFAULT_COUNTRIES).map(&:id),
emissions_data_url: emissions_chart_data_tpi_ascor_index_path
}) %>
<%= react_component('InfoModal', {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddVisibilityStatusToASCORCountries < ActiveRecord::Migration[6.1]
def change
add_column :ascor_countries, :visibility_status, :string, index: true, default: 'draft'
ASCOR::Country.update_all visibility_status: 'published'
end
end
52 changes: 26 additions & 26 deletions db/seeds/tpi/ascor_countries.csv
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
Id,Name,Country ISO code,Region,World Bank lending group,International Monetary Fund fiscal monitor category,Type of Party to the United Nations Framework Convention on Climate Change
1,United States,USA,North America,High-income,Advanced economies,Annex I
2,Japan,JPN,Asia,High-income,Advanced economies,Annex I
3,France,FRA,Europe,High-income,Advanced economies,Annex I
4,Italy,ITA,Europe,High-income,Advanced economies,Annex I
5,Egypt,EGY,Africa,Lower-middle-income,Emerging market economies,Non-Annex I
6,Uruguay,URY,Latin America and Caribbean,High-income,Emerging market economies,Non-Annex I
7,Saudi Arabia,SAU,Asia,High-income,Emerging market economies,Non-Annex I
8,Australia,AUS,Oceania,High-income,Advanced economies,Annex I
9,Mexico,MEX,Latin America and Caribbean,Upper-middle-income,Emerging market economies,Non-Annex I
10,China,CHN,Asia,Upper-middle-income,Emerging market economies,Non-Annex I
11,Poland,POL,Europe,High-income,Emerging market economies,Annex I
12,Indonesia,IDN,Asia,Upper-middle-income,Emerging market economies,Non-Annex I
13,Kazakhstan,KAZ,Asia,Upper-middle-income,Emerging market economies,Non-Annex I
14,Canada,CAN,North America,High-income,Advanced economies,Annex I
15,Thailand,THA,Asia,Upper-middle-income,Emerging market economies,Non-Annex I
16,Chile,CHL,Latin America and Caribbean,High-income,Emerging market economies,Non-Annex I
17,South Africa,ZAF,Africa,Upper-middle-income,Emerging market economies,Non-Annex I
18,Brazil,BRA,Latin America and Caribbean,Upper-middle-income,Emerging market economies,Non-Annex I
19,Kenya,KEN,Africa,Lower-middle-income,Low-income developing countries,Non-Annex I
20,Morocco,MAR,Africa,Lower-middle-income,Emerging market economies,Non-Annex I
21,Germany,DEU,Europe,High-income,Advanced economies,Annex I
22,United Kingdom,GBR,Europe,High-income,Advanced economies,Annex I
23,India,IND,Asia,Lower-middle-income,Emerging market economies,Non-Annex I
24,Barbados,BRB,Latin America and Caribbean,High-income,Emerging market economies,Non-Annex I
25,Bangladesh,BGD,Asia,Lower-middle-income,Low-income developing countries,Non-Annex I
Id,Name,Country ISO code,Region,World Bank lending group,International Monetary Fund fiscal monitor category,Type of Party to the United Nations Framework Convention on Climate Change,Visibility status
1,United States,USA,North America,High-income,Advanced economies,Annex I,published
2,Japan,JPN,Asia,High-income,Advanced economies,Annex I,published
3,France,FRA,Europe,High-income,Advanced economies,Annex I,published
4,Italy,ITA,Europe,High-income,Advanced economies,Annex I,published
5,Egypt,EGY,Africa,Lower-middle-income,Emerging market economies,Non-Annex I,published
6,Uruguay,URY,Latin America and Caribbean,High-income,Emerging market economies,Non-Annex I,published
7,Saudi Arabia,SAU,Asia,High-income,Emerging market economies,Non-Annex I,published
8,Australia,AUS,Oceania,High-income,Advanced economies,Annex I,published
9,Mexico,MEX,Latin America and Caribbean,Upper-middle-income,Emerging market economies,Non-Annex I,published
10,China,CHN,Asia,Upper-middle-income,Emerging market economies,Non-Annex I,published
11,Poland,POL,Europe,High-income,Emerging market economies,Annex I,published
12,Indonesia,IDN,Asia,Upper-middle-income,Emerging market economies,Non-Annex I,published
13,Kazakhstan,KAZ,Asia,Upper-middle-income,Emerging market economies,Non-Annex I,published
14,Canada,CAN,North America,High-income,Advanced economies,Annex I,published
15,Thailand,THA,Asia,Upper-middle-income,Emerging market economies,Non-Annex I,published
16,Chile,CHL,Latin America and Caribbean,High-income,Emerging market economies,Non-Annex I,published
17,South Africa,ZAF,Africa,Upper-middle-income,Emerging market economies,Non-Annex I,published
18,Brazil,BRA,Latin America and Caribbean,Upper-middle-income,Emerging market economies,Non-Annex I,published
19,Kenya,KEN,Africa,Lower-middle-income,Low-income developing countries,Non-Annex I,published
20,Morocco,MAR,Africa,Lower-middle-income,Emerging market economies,Non-Annex I,published
21,Germany,DEU,Europe,High-income,Advanced economies,Annex I,published
22,United Kingdom,GBR,Europe,High-income,Advanced economies,Annex I,published
23,India,IND,Asia,Lower-middle-income,Emerging market economies,Non-Annex I,published
24,Barbados,BRB,Latin America and Caribbean,High-income,Emerging market economies,Non-Annex I,published
25,Bangladesh,BGD,Asia,Lower-middle-income,Low-income developing countries,Non-Annex I,published
6 changes: 4 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ CREATE TABLE public.ascor_countries (
fiscal_monitor_category character varying,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL,
type_of_party character varying
type_of_party character varying,
visibility_status character varying DEFAULT 'draft'::character varying
);


Expand Down Expand Up @@ -4174,6 +4175,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20231108125346'),
('20231207082211'),
('20240119084250'),
('20240202090401');
('20240202090401'),
('20240206094238');


Binary file modified db/test-dump.psql
Binary file not shown.
4 changes: 2 additions & 2 deletions spec/commands/csv_data_upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1460,14 +1460,14 @@
expect_data_upload_results(
ASCOR::Country,
ascor_countries_csv,
{new_records: 1, not_changed_records: 1, rows: 2, updated_records: 0},
{new_records: 2, not_changed_records: 1, rows: 3, updated_records: 0},
custom_uploader: 'ASCORCountries'
)
# subsequent import should not create or update any record
expect_data_upload_results(
ASCOR::Country,
ascor_countries_csv,
{new_records: 0, not_changed_records: 2, rows: 2, updated_records: 0},
{new_records: 0, not_changed_records: 3, rows: 3, updated_records: 0},
custom_uploader: 'ASCORCountries'
)

Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/tpi/ascor_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
let_it_be(:ascor_benchmark) { create :ascor_benchmark, country: ascor_country }
let_it_be(:ascor_pathway) { create :ascor_pathway, country: ascor_country }

let_it_be(:ascor_country_draft) do
create :ascor_country, name: 'Poland', iso: 'PLN', visibility_status: 'draft'
end
let_it_be(:ascor_assessment_draft) { create :ascor_assessment, country: ascor_country_draft }
let_it_be(:ascor_assessment_result_draft) do
create :ascor_assessment_result, assessment: ascor_assessment_draft, indicator: ascor_assessment_indicator
end
let_it_be(:ascor_benchmark_draft) { create :ascor_benchmark, country: ascor_country_draft }
let_it_be(:ascor_pathway_draft) { create :ascor_pathway, country: ascor_country_draft }

describe 'GET index' do
subject { get :index }

Expand All @@ -20,6 +30,12 @@
subject { get :show, params: {id: ascor_country.slug} }

it { is_expected.to be_successful }

context 'when country is draft' do
subject { get :show, params: {id: ascor_country_draft.slug} }

it { is_expected.to redirect_to(tpi_ascor_index_path) }
end
end

describe 'GET user download' do
Expand Down
2 changes: 2 additions & 0 deletions spec/factories/ascor_countries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# created_at :datetime not null
# updated_at :datetime not null
# type_of_party :string
# visibility_status :string default("draft")
#
FactoryBot.define do
factory :ascor_country, class: 'ASCOR::Country' do
Expand All @@ -21,5 +22,6 @@
wb_lending_group { 'High-income' }
fiscal_monitor_category { 'Advanced economies' }
type_of_party { 'Annex I' }
visibility_status { 'published' }
end
end
1 change: 1 addition & 0 deletions spec/services/api/ascor/benchmarks_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

before_all do
@country = create :ascor_country
_draft_country = create(:ascor_country, id: 3, name: 'Draft Country', iso: 'DFT')

create :ascor_pathway,
country: @country,
Expand Down
1 change: 1 addition & 0 deletions spec/services/api/ascor/bubble_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
before_all do
usa = create(:ascor_country, id: 1, name: 'USA', iso: 'USA')
japan = create(:ascor_country, id: 2, name: 'Japan', iso: 'JPN')
_draft_country = create(:ascor_country, id: 3, name: 'Draft Country', iso: 'DFT')

_indicator_pillar_1 = create(:ascor_assessment_indicator, id: 1, code: 'EP', indicator_type: :pillar,
text: 'Emissions Performance')
Expand Down
1 change: 1 addition & 0 deletions spec/services/api/ascor/emissions_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
before_all do
@usa = create(:ascor_country, id: 1, name: 'USA', iso: 'USA')
@czechia = create(:ascor_country, id: 2, name: 'Czechia', iso: 'CZE')
_draft_country = create(:ascor_country, id: 3, name: 'Draft Country', iso: 'DFT')

create :ascor_pathway,
country: @usa,
Expand Down
1 change: 1 addition & 0 deletions spec/services/api/ascor/recent_emissions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

before_all do
@country = create(:ascor_country, id: 1, name: 'USA', iso: 'USA')
_draft_country = create(:ascor_country, id: 3, name: 'Draft Country', iso: 'DFT')
@assessment_date = Date.new(2019, 1, 1)

create :ascor_pathway,
Expand Down
7 changes: 4 additions & 3 deletions spec/support/fixtures/files/ascor_countries.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Id,Name,Country ISO code,Region,World Bank lending group,International Monetary Fund fiscal monitor category,Type of Party to the United Nations Framework Convention on Climate Change
,United States,USA,North America,High-income,Advanced economies,Annex I
,Japan,JPN,Asia,High-income,Advanced economies,Annex I
Id,Name,Country ISO code,Region,World Bank lending group,International Monetary Fund fiscal monitor category,Type of Party to the United Nations Framework Convention on Climate Change,Visibility stauts
,United States,USA,North America,High-income,Advanced economies,Annex I,published
,Japan,JPN,Asia,High-income,Advanced economies,Annex I,published
,Poland,PLN,Asia,High-income,Advanced economies,Annex I,draft

0 comments on commit 55588b5

Please sign in to comment.