Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TPI Production Ready Seeds #131

Merged
merged 14 commits into from
Nov 24, 2019
Merged
1 change: 1 addition & 0 deletions app/admin/cp_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
year_columns = collection.flat_map(&:emissions_all_years).uniq.sort

column :id
column(:company_id) { |a| a.company.id }
column(:company) { |a| a.company.name }
column :assessment_date
column :publication_date, &:publication_date_csv
Expand Down
1 change: 1 addition & 0 deletions app/admin/instrument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
permit_params :name, :instrument_type_id

config.batch_actions = false
config.sort_order = 'name_asc'

decorate_with InstrumentDecorator

Expand Down
1 change: 1 addition & 0 deletions app/admin/laws_sectors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ActiveAdmin.register LawsSector do
config.batch_actions = false
config.sort_order = 'name_asc'

menu parent: 'Laws', priority: 5

Expand Down
1 change: 1 addition & 0 deletions app/admin/mq_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

csv do
column :id
column(:company_id) { |a| a.company.id }
column(:company) { |a| a.company.name }
column :assessment_date
column :publication_date, &:publication_date_csv
Expand Down
2 changes: 1 addition & 1 deletion app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Company < ApplicationRecord

validates :ca100, inclusion: {in: [true, false]}
validates_presence_of :name, :slug, :isin, :market_cap_group
validates_uniqueness_of :slug, :isin, :name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I missed that thing before, but that wasn't right after having multiple ISINs in one string ;]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I missed that too 😅

validates_uniqueness_of :slug, :name

def to_s
name
Expand Down
7 changes: 6 additions & 1 deletion app/services/csv_import/companies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def resource_klass
end

def prepare_company(row)
return prepare_overridden_resource(row) if override_id

find_record_by(:id, row) ||
find_record_by(:isin, row) ||
find_record_by(:name, row) ||
Expand All @@ -40,10 +42,13 @@ def company_attributes(row)
name: row[:name],
isin: row[:isin],
sector: find_or_create_tpi_sector(row[:sector]),
market_cap_group: row[:market_cap_group],
market_cap_group: row[:market_cap_group].downcase,
geography: geographies[row[:geography_iso]],
headquarters_geography: geographies[row[:headquarters_geography_iso]],
ca100: row[:ca100],
sedol: row[:sedol],
latest_information: row[:latest_information],
historical_comments: row[:historical_comments],
visibility_status: row[:visibility_status]
}
end
Expand Down
6 changes: 3 additions & 3 deletions app/services/csv_import/cp_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def prepare_assessment(row)
end

def find_company!(row)
return unless row[:company].present?
return unless row[:company_id].present?

Company.where('lower(name) = ?', row[:company].downcase).first!
Company.find(row[:company_id])
end

def assessment_attributes(row)
Expand All @@ -47,7 +47,7 @@ def assessment_attributes(row)
end

def assessment_date(row)
CSVImport::DateUtils.safe_parse(row[:assessment_date], ['%Y-%m-%d'])
CSVImport::DateUtils.safe_parse(row[:assessment_date], ['%Y-%m-%d']) if row[:assessment_date]
end

def publication_date(row)
Expand Down
4 changes: 2 additions & 2 deletions app/services/csv_import/mq_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def prepare_assessment(row)
end

def find_company!(row)
return unless row[:company].present?
return unless row[:company_id].present?

Company.where('lower(name) = ?', row[:company].downcase).first!
Company.find(row[:company_id])
end

def assessment_attributes(row)
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20191121170141_remove_isin_index_from_companies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveIsinIndexFromCompanies < ActiveRecord::Migration[6.0]
def change
remove_index :companies, :isin
end
end
3 changes: 1 addition & 2 deletions 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: 2019_11_20_162650) do
ActiveRecord::Schema.define(version: 2019_11_21_170141) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -83,7 +83,6 @@
t.index ["discarded_at"], name: "index_companies_on_discarded_at"
t.index ["geography_id"], name: "index_companies_on_geography_id"
t.index ["headquarters_geography_id"], name: "index_companies_on_headquarters_geography_id"
t.index ["isin"], name: "index_companies_on_isin", unique: true
t.index ["market_cap_group"], name: "index_companies_on_market_cap_group"
t.index ["sector_id"], name: "index_companies_on_sector_id"
t.index ["slug"], name: "index_companies_on_slug", unique: true
Expand Down
65 changes: 63 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,60 @@
sector.update!(cp_unit: sector_cp_unit) if sector.new_record? && sector_cp_unit.present?
end

# instruments: Instrument Type & Instrument
# envs: ALL
# rubocop:disable Style/WordArray
[
['Capacity-building', ['Knowledge generation',
'Sharing and dissemination',
'Research and development',
'Education and training']],
['Regulation', ['Standards and obligations',
'Building codes',
'Zoning and spatial planning',
'Disclosure obligations']],
['Incentives', ['Taxes', 'Subsidies']],
['Governance and planning', ['Creating bodies/institutions',
'Designing processes',
'Developing plans and strategies',
'Assigning responsibilities to other levels of government',
'Monitoring and evaluation']],
['Direct investment', ['Public goods - early warning systems',
'Public goods - other',
'Social safety nets',
'Provision of climate finance']]
].each do |inst_type, instruments|
type = InstrumentType.find_or_create_by!(name: inst_type)
instruments.each do |inst|
Instrument.find_or_create_by!(name: inst, instrument_type: type)
end
end
# rubocop:enable Style/WordArray

# Laws sectors: Names
# envs: ALL
[
'Agriculture',
'Residential and Commercial',
'Coastal zones',
'Economy-wide',
'Energy',
'Health',
'Industry',
'LULUCF',
'Social development',
'Tourism',
'Transportation',
'Urban',
'Waste',
'Water',
'Rural',
'Environment',
'Other'
].each do |sector|
LawsSector.find_or_create_by!(name: sector)
end

if Rails.env.development? || ENV['SEED_DATA']
# import geographies
TimedLogger.log('Import geographies') do
Expand All @@ -37,8 +91,12 @@

# import companies
TimedLogger.log('Import companies') do
file = File.open(Rails.root.join('db', 'seeds', 'companies.csv'), 'r')
CSVImport::Companies.new(file).call
file = File.open(Rails.root.join('db', 'seeds', 'tpi-companies.csv'), 'r')
importer = CSVImport::Companies.new(file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just taking a note here (not a blocker for this PR) to refactor this just a little CSVImport::Companies.new(file, override_id: true)

importer.override_id = true
importer.call

ActiveRecord::Base.connection.execute("select setval('companies_id_seq',max(id)) from companies;")
end

# import CP Benchmarks
Expand All @@ -60,6 +118,9 @@

file = File.open(Rails.root.join('db', 'seeds', 'mq-assessments-M2.csv'), 'r')
CSVImport::MQAssessments.new(file).call

file = File.open(Rails.root.join('db', 'seeds', 'mq-assessments-M3.csv'), 'r')
CSVImport::MQAssessments.new(file).call
end

# import Legislations
Expand Down
Loading