diff --git a/app/admin/ascor/countries.rb b/app/admin/ascor/countries.rb index cf329134c..16f7a4340 100644 --- a/app/admin/ascor/countries.rb +++ b/app/admin/ascor/countries.rb @@ -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' @@ -18,6 +18,7 @@ column :name column 'Country ISO code', :iso column :region + tag_column :visibility_status actions end @@ -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 @@ -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 diff --git a/app/controllers/tpi/ascor_controller.rb b/app/controllers/tpi/ascor_controller.rb index 91a9e4ca2..de86d7294 100644 --- a/app/controllers/tpi/ascor_controller.rb +++ b/app/controllers/tpi/ascor_controller.rb @@ -56,7 +56,7 @@ 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]) @@ -64,7 +64,9 @@ def fetch_ascor_countries 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 diff --git a/app/models/ascor/assessment_result.rb b/app/models/ascor/assessment_result.rb index 3275d559f..3c386490e 100644 --- a/app/models/ascor/assessment_result.rb +++ b/app/models/ascor/assessment_result.rb @@ -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 diff --git a/app/models/ascor/country.rb b/app/models/ascor/country.rb index a4261566e..d0d169f92 100644 --- a/app/models/ascor/country.rb +++ b/app/models/ascor/country.rb @@ -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 = [ diff --git a/app/services/api/ascor/bubble_chart.rb b/app/services/api/ascor/bubble_chart.rb index 1ac13ff9b..be6f96f6a 100644 --- a/app/services/api/ascor/bubble_chart.rb +++ b/app/services/api/ascor/bubble_chart.rb @@ -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| { diff --git a/app/services/api/ascor/emissions_chart.rb b/app/services/api/ascor/emissions_chart.rb index f502ab86e..dc33d3753 100644 --- a/app/services/api/ascor/emissions_chart.rb +++ b/app/services/api/ascor/emissions_chart.rb @@ -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 diff --git a/app/services/csv_export/ascor/assessments.rb b/app/services/csv_export/ascor/assessments.rb index 460ddabd6..7af113133 100644 --- a/app/services/csv_export/ascor/assessments.rb +++ b/app/services/csv_export/ascor/assessments.rb @@ -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 diff --git a/app/services/csv_export/ascor/benchmarks.rb b/app/services/csv_export/ascor/benchmarks.rb index 2ce163a71..1c3e440fc 100644 --- a/app/services/csv_export/ascor/benchmarks.rb +++ b/app/services/csv_export/ascor/benchmarks.rb @@ -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 diff --git a/app/services/csv_export/ascor/countries.rb b/app/services/csv_export/ascor/countries.rb index 8c5b6ae61..5790c55b9 100644 --- a/app/services/csv_export/ascor/countries.rb +++ b/app/services/csv_export/ascor/countries.rb @@ -32,7 +32,7 @@ def call private def countries - @countries ||= ::ASCOR::Country.order(:name) + @countries ||= ::ASCOR::Country.published.order(:name) end end end diff --git a/app/services/csv_export/ascor/pathways.rb b/app/services/csv_export/ascor/pathways.rb index d54a4e253..895243d68 100644 --- a/app/services/csv_export/ascor/pathways.rb +++ b/app/services/csv_export/ascor/pathways.rb @@ -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 diff --git a/app/services/csv_import/ascor_countries.rb b/app/services/csv_import/ascor_countries.rb index 24cf8f6fd..b94def069 100644 --- a/app/services/csv_import/ascor_countries.rb +++ b/app/services/csv_import/ascor_countries.rb @@ -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? diff --git a/app/views/tpi/ascor/_emissions_chart.html.erb b/app/views/tpi/ascor/_emissions_chart.html.erb index e4e69777a..4413e35eb 100644 --- a/app/views/tpi/ascor/_emissions_chart.html.erb +++ b/app/views/tpi/ascor/_emissions_chart.html.erb @@ -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', { diff --git a/db/migrate/20240206094238_add_visibility_status_to_ascor_countries.rb b/db/migrate/20240206094238_add_visibility_status_to_ascor_countries.rb new file mode 100644 index 000000000..5f5c82b4c --- /dev/null +++ b/db/migrate/20240206094238_add_visibility_status_to_ascor_countries.rb @@ -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 diff --git a/db/seeds/tpi/ascor_countries.csv b/db/seeds/tpi/ascor_countries.csv index 4b5fcb2ac..15d9f3a90 100644 --- a/db/seeds/tpi/ascor_countries.csv +++ b/db/seeds/tpi/ascor_countries.csv @@ -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 diff --git a/db/structure.sql b/db/structure.sql index 609f4fb4b..4402487d8 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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 ); @@ -4174,6 +4175,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20231108125346'), ('20231207082211'), ('20240119084250'), -('20240202090401'); +('20240202090401'), +('20240206094238'); diff --git a/db/test-dump.psql b/db/test-dump.psql index 9a76c8dbf..ba7329d74 100644 Binary files a/db/test-dump.psql and b/db/test-dump.psql differ diff --git a/spec/commands/csv_data_upload_spec.rb b/spec/commands/csv_data_upload_spec.rb index 8e9f0c8c9..425e1731f 100644 --- a/spec/commands/csv_data_upload_spec.rb +++ b/spec/commands/csv_data_upload_spec.rb @@ -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' ) diff --git a/spec/controllers/tpi/ascor_controller_spec.rb b/spec/controllers/tpi/ascor_controller_spec.rb index 6b64d0c9b..c3ba4eb27 100644 --- a/spec/controllers/tpi/ascor_controller_spec.rb +++ b/spec/controllers/tpi/ascor_controller_spec.rb @@ -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 } @@ -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 diff --git a/spec/factories/ascor_countries.rb b/spec/factories/ascor_countries.rb index 23784e8ea..2bed1179a 100644 --- a/spec/factories/ascor_countries.rb +++ b/spec/factories/ascor_countries.rb @@ -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 @@ -21,5 +22,6 @@ wb_lending_group { 'High-income' } fiscal_monitor_category { 'Advanced economies' } type_of_party { 'Annex I' } + visibility_status { 'published' } end end diff --git a/spec/services/api/ascor/benchmarks_chart_spec.rb b/spec/services/api/ascor/benchmarks_chart_spec.rb index 5b59f6b51..570085da4 100644 --- a/spec/services/api/ascor/benchmarks_chart_spec.rb +++ b/spec/services/api/ascor/benchmarks_chart_spec.rb @@ -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, diff --git a/spec/services/api/ascor/bubble_chart_spec.rb b/spec/services/api/ascor/bubble_chart_spec.rb index ef437f3ec..a7daf30d0 100644 --- a/spec/services/api/ascor/bubble_chart_spec.rb +++ b/spec/services/api/ascor/bubble_chart_spec.rb @@ -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') diff --git a/spec/services/api/ascor/emissions_chart_spec.rb b/spec/services/api/ascor/emissions_chart_spec.rb index 252cd01cd..d4893dbf2 100644 --- a/spec/services/api/ascor/emissions_chart_spec.rb +++ b/spec/services/api/ascor/emissions_chart_spec.rb @@ -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, diff --git a/spec/services/api/ascor/recent_emissions_spec.rb b/spec/services/api/ascor/recent_emissions_spec.rb index 544cca89d..ce7c90a6b 100644 --- a/spec/services/api/ascor/recent_emissions_spec.rb +++ b/spec/services/api/ascor/recent_emissions_spec.rb @@ -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, diff --git a/spec/support/fixtures/files/ascor_countries.csv b/spec/support/fixtures/files/ascor_countries.csv index 0deb4c88c..6385c9919 100644 --- a/spec/support/fixtures/files/ascor_countries.csv +++ b/spec/support/fixtures/files/ascor_countries.csv @@ -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 \ No newline at end of file +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 \ No newline at end of file