Skip to content

Commit

Permalink
Merge pull request #289 from Vizzuality/feature/country-profile
Browse files Browse the repository at this point in the history
Feature/country profile
  • Loading branch information
simaob committed Feb 27, 2020
2 parents b9f2a0b + 4bd994a commit c389ca4
Show file tree
Hide file tree
Showing 22 changed files with 373 additions and 53 deletions.
9 changes: 8 additions & 1 deletion app/admin/geographies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

permit_params :name, :iso, :region, :federal, :federal_details,
:legislative_process, :geography_type,
:visibility_status,
:visibility_status, :percent_global_emissions,
:climate_risk_index, :wb_income_group,
:created_by_id, :updated_by_id,
political_group_ids: [],
events_attributes: permit_params_for(:events)
Expand Down Expand Up @@ -41,6 +42,9 @@
row :indc_link
row :legislative_process
row :political_groups
row 'Percentage of Global Emissions', &:percent_global_emissions
row 'Climate Risk Index', &:climate_risk_index
row 'World Bank Income Group', &:wb_income_group
row :updated_at
row :updated_by
row :created_at
Expand Down Expand Up @@ -74,6 +78,9 @@
column :federal
column :federal_details
column :political_groups, &:political_groups_string
column :percent_global_emissions
column :climate_risk_index
column :wb_income_group
column :visibility_status
end

Expand Down
4 changes: 4 additions & 0 deletions app/decorators/geography_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ def indc_link

h.link_to 'INDC Link', indc_url, target: '_blank'
end

def percent_global_emissions
model.percent_global_emissions ? "#{model.percent_global_emissions} %" : '-'
end
end
12 changes: 12 additions & 0 deletions app/models/cp/unit.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# == Schema Information
#
# Table name: cp_units
#
# id :bigint not null, primary key
# sector_id :bigint
# valid_since :date
# unit :text not null
# created_at :datetime not null
# updated_at :datetime not null
#

class CP::Unit < ApplicationRecord
belongs_to :sector, class_name: 'TPISector', foreign_key: 'sector_id'

Expand Down
33 changes: 18 additions & 15 deletions app/models/geography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
#
# Table name: geographies
#
# id :bigint not null, primary key
# geography_type :string not null
# iso :string not null
# name :string not null
# slug :string not null
# region :string not null
# federal :boolean default(FALSE), not null
# federal_details :text
# legislative_process :text
# created_at :datetime not null
# updated_at :datetime not null
# visibility_status :string default("draft")
# created_by_id :bigint
# updated_by_id :bigint
# discarded_at :datetime
# id :bigint not null, primary key
# geography_type :string not null
# iso :string not null
# name :string not null
# slug :string not null
# region :string not null
# federal :boolean default(FALSE), not null
# federal_details :text
# legislative_process :text
# created_at :datetime not null
# updated_at :datetime not null
# visibility_status :string default("draft")
# created_by_id :bigint
# updated_by_id :bigint
# discarded_at :datetime
# percent_global_emissions :string
# climate_risk_index :string
# wb_income_group :string
#

class Geography < ApplicationRecord
Expand Down
2 changes: 2 additions & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# link :string
# content_id :bigint not null
# name :string
# created_at :datetime not null
# updated_at :datetime not null
#

class Image < ApplicationRecord
Expand Down
2 changes: 1 addition & 1 deletion app/models/tpi_sector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# slug :string not null
# created_at :datetime not null
# updated_at :datetime not null
# cp_unit :text
# cluster_id :bigint
#

class TPISector < ApplicationRecord
Expand Down
10 changes: 10 additions & 0 deletions app/models/tpi_sector_cluster.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# == Schema Information
#
# Table name: tpi_sector_clusters
#
# id :bigint not null, primary key
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
#

class TPISectorCluster < ApplicationRecord
has_many :sectors, class_name: 'TPISector', foreign_key: :cluster_id, inverse_of: :cluster

Expand Down
3 changes: 3 additions & 0 deletions app/services/csv_import/geographies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def geography_attributes(row)
federal_details: row[:federal_details],
legislative_process: row[:legislative_process],
geography_type: row[:geography_type].downcase,
percent_global_emissions: row[:percent_global_emissions],
climate_risk_index: row[:climate_risk_index],
wb_income_group: row[:wb_income_group],
visibility_status: row[:visibility_status]
}
end
Expand Down
27 changes: 27 additions & 0 deletions app/services/migration/geographies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'uri'
module Migration
class Geographies
class << self
def migrate_metadata_file(source)
file = File.read(source).force_encoding('UTF-8')
csv = CSV.parse(
file,
headers: true,
skip_blanks: true
).delete_if { |row| row.to_hash.values.all?(&:blank?) }
csv.each do |row|
puts row['iso']
g = ::Geography.where(iso: row['iso']).first
unless g
puts "Can't find geography with this iso: #{row['iso']}"
next
end
g.percent_global_emissions = row['percent_global_emissions'].to_f.round(2).to_s
g.climate_risk_index = row['climate_risk_index']
g.wb_income_group = row['wb_income_group']
g.save
end
end
end
end
end
7 changes: 7 additions & 0 deletions app/services/seed/cclow_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class << self
delegate :call_sources_import, to: :instance
delegate :import_litigation_sides, to: :instance
delegate :call_litigation_sources_import, to: :instance
delegate :import_geographies_metadata, to: :instance
end

def call_sources_import
Expand Down Expand Up @@ -65,6 +66,12 @@ def import_litigation_sides
end
end

def import_geographies_metadata
TimedLogger.log('Import Geographies Metadata') do
Migration::Geographies.migrate_metadata_file(seed_file('geographies_metadata.csv'))
end
end

private

def seed_file(filename)
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/geographies/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<%= f.input :federal_details, wrapper_html: {data: {controller: 'dependent-input', depends_on: 'federal'}} %>
<%= f.input :legislative_process, as: :trix %>
<%= f.input :political_group_ids, label: 'Political Groups', as: :tags, collection: PoliticalGroup.all %>
<%= f.input :percent_global_emissions, label: 'Percentage of Global Emissions' %>
<%= f.input :climate_risk_index %>
<%= f.input :wb_income_group, label: 'World Bank Income Group' %>
<%= f.input :visibility_status, as: :select %>
<% end %>
</div>
Expand Down
6 changes: 3 additions & 3 deletions app/views/cclow/geographies/_overview.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
{caption: 'Litigation cases', count: geography_overview.number_of_litigation_cases, img: 'scale', link: cclow_geography_litigation_cases_path(geography)},
{caption: 'Climate targets', count: geography_overview.number_of_targets, img: 'target', link: cclow_geography_climate_targets_path(geography)}] %>
<% properties = [{caption: 'Region', value: geography.region, question: false},
{caption: 'Rank as emitter', question: false},
{caption: 'Paris Agreement ratification status', question: false},
{caption: 'Income group (World Bank)', question: false},
{caption: '% Global Emissions', value: geography.percent_global_emissions, question: false},
{caption: 'Global Climate Risk Index', value: geography.climate_risk_index, question: false},
{caption: 'Income group (World Bank)', value: geography.wb_income_group, question: false},
{caption: 'Main political groups', value: geography.political_groups.map(&:name).join('; '), question: false},
{caption: 'Federative/Unitary',
value: geography.federal ? "Federative #{geography.federal_details}" : 'Unitary', question: false}] %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddPercentGlobalEmissionsAndClimateRiskIndexAndWbIncomeGroupToGeographies < ActiveRecord::Migration[6.0]
def change
add_column :geographies, :percent_global_emissions, :string
add_column :geographies, :climate_risk_index, :string
add_column :geographies, :wb_income_group, :string
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_02_16_161342) do
ActiveRecord::Schema.define(version: 2020_02_27_100810) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Expand Down Expand Up @@ -234,6 +234,9 @@
t.bigint "created_by_id"
t.bigint "updated_by_id"
t.datetime "discarded_at"
t.string "percent_global_emissions"
t.string "climate_risk_index"
t.string "wb_income_group"
t.index ["created_by_id"], name: "index_geographies_on_created_by_id"
t.index ["discarded_at"], name: "index_geographies_on_discarded_at"
t.index ["iso"], name: "index_geographies_on_iso", unique: true
Expand Down
Loading

0 comments on commit c389ca4

Please sign in to comment.