Skip to content

Commit 19f10fe

Browse files
committed
CP Changes V2: second iter
1 parent 74941a9 commit 19f10fe

9 files changed

Lines changed: 67 additions & 20 deletions

File tree

app/javascript/components/tpi/charts/cp-performance/Chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Legend from './Legend';
2020
function filterBySubsector(companyData, selectedSubsector, sectorName) {
2121
if (!selectedSubsector || !hasSubsectorToggle(sectorName)) return companyData;
2222

23-
return companyData.filter(d => d.company.subsector === selectedSubsector.value);
23+
return companyData.filter(d => d.company.subsector?.toLowerCase() === selectedSubsector.value.toLowerCase());
2424
}
2525
function filterByShowValue(companyData, showByValue, selectedSubsector, sectorName) {
2626
const bySubsector = filterBySubsector(companyData, selectedSubsector, sectorName);

app/javascript/components/tpi/charts/cp-performance/chart-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const useParsedChartData = (data, companySelector, selectedCompanies, sel
3232
const benchmarks = data.filter(d => {
3333
const typeMatch = d.type === 'area';
3434
if (selectedSubsector) {
35-
return typeMatch && d.subsector === selectedSubsector.value;
35+
return typeMatch && d.subsector?.toLowerCase() === selectedSubsector.value.toLowerCase();
3636
}
3737

3838
return typeMatch;

app/models/company_subsector.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ class CompanySubsector < ApplicationRecord
1616
}, class_name: 'CP::Assessment'
1717
has_one :latest_cp_assessment_regional, -> { currently_published.regional.order(assessment_date: :desc) },
1818
class_name: 'CP::Assessment'
19+
20+
before_validation :normalize_subsector
21+
22+
private
23+
24+
def normalize_subsector
25+
self.subsector = subsector&.strip
26+
end
1927
end

app/models/cp/assessment.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ def cp_regional_alignment_2050_by_company
153153
end
154154

155155
def company_subsector
156-
CompanySubsector.find(company_subsector_id) if company_subsector_id.present?
156+
return @company_subsector if defined?(@company_subsector)
157+
158+
@company_subsector = company_subsector_id.present? ? CompanySubsector.find(company_subsector_id) : nil
157159
end
158160

159161
def subsector_name
@@ -204,6 +206,13 @@ def benchmarks
204206
)
205207
return result if result.present?
206208

209+
if subsector_name.present?
210+
Rails.logger.warn(
211+
"[CP::Assessment#benchmarks] No benchmarks for subsector='#{subsector_name}' " \
212+
"sector='#{sector.name}' date=#{publication_date}, falling back to nil subsector"
213+
)
214+
end
215+
207216
sector.latest_benchmarks_for_date(publication_date, category: cp_assessmentable_type, subsector: nil)
208217
end
209218

app/models/tpi_sector.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ def latest_cp_unit
6363
def latest_released_benchmarks(category:, region: nil, subsector: nil)
6464
region ||= 'Global'
6565
scope = cp_benchmarks.where(category: category.to_s, region: region)
66-
scope = scope.where(subsector: subsector) unless subsector.nil?
66+
scope = if subsector.nil?
67+
scope.where(subsector: nil)
68+
else
69+
scope.where('LOWER(subsector) = LOWER(?)', subsector)
70+
end
6771
last = scope.group_by(&:release_date).max
6872
last ? last.last : []
6973
end
@@ -89,7 +93,12 @@ def latest_benchmarks_for_date(date, category:, region: nil, subsector: nil)
8993
region ||= 'Global'
9094
return latest_released_benchmarks(category: category, region: region, subsector: subsector) unless date
9195

92-
benchmarks = cp_benchmarks.where(category: category.to_s, region: region, subsector: subsector)
96+
benchmarks = cp_benchmarks.where(category: category.to_s, region: region)
97+
benchmarks = if subsector.nil?
98+
benchmarks.where(subsector: nil)
99+
else
100+
benchmarks.where('LOWER(subsector) = LOWER(?)', subsector)
101+
end
93102
sector_benchmarks_dates = benchmarks.pluck(:release_date).uniq.sort
94103

95104
last_release_date_before_given_date =

app/services/csv_export/user/cp_benchmarks.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ def call
3636

3737
private
3838

39+
CHEMICALS_SUBSECTOR_LABELS_DOWNCASED = CP::Benchmark::CHEMICALS_SUBSECTOR_LABELS.map(&:downcase).freeze
40+
3941
def filtered_benchmarks
4042
@cp_benchmarks.reject do |b|
4143
b.sector&.name == 'Chemicals' &&
42-
b.benchmark_label.present? &&
43-
!CP::Benchmark::CHEMICALS_SUBSECTOR_LABELS.include?(b.benchmark_label)
44+
b.subsector.present? &&
45+
!CHEMICALS_SUBSECTOR_LABELS_DOWNCASED.include?(b.subsector.downcase)
4446
end
4547
end
4648
end

app/services/csv_import/company_cp_assessments.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ def subsector?(row)
5656
end
5757

5858
def resolve_subsector_value(row)
59-
return row[:subsector] if row.header?(:subsector) && row[:subsector].present?
60-
return row[:technology_type] if row.header?(:technology_type) && row[:technology_type].present?
61-
62-
nil
59+
value = if row.header?(:subsector) && row[:subsector].present?
60+
row[:subsector]
61+
elsif row.header?(:technology_type) && row[:technology_type].present?
62+
row[:technology_type]
63+
end
64+
value&.strip&.presence
6365
end
6466

6567
def prepare_assessment(row)
@@ -73,10 +75,19 @@ def prepare_assessment(row)
7375
end
7476

7577
def prepare_assessment_subsector(row)
76-
company_id = find_company!(row)&.id
77-
subsector = CompanySubsector.where(company_id: company_id, subsector: resolve_subsector_value(row)).first
78+
company = find_company!(row)
79+
company_id = company&.id
80+
subsector_value = resolve_subsector_value(row)
81+
82+
subsector = CompanySubsector
83+
.where(company_id: company_id)
84+
.where('LOWER(subsector) = LOWER(?)', subsector_value)
85+
.first
86+
87+
if subsector.nil? && company&.sector&.name == 'Chemicals' && subsector_value.present?
88+
subsector = CompanySubsector.find_or_create_by!(company_id: company_id, subsector: subsector_value)
89+
end
7890

79-
# fallthru in case the subsector passed doesn't exist
8091
return prepare_assessment(row) unless subsector
8192

8293
find_record_by(:id, row) ||

app/services/csv_import/cp_benchmarks.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def import
1111
benchmark.scenario = row[:scenario] if row.header?(:scenario)
1212
benchmark.region = parse_cp_benchmark_region(row[:region]) if row.header?(:region)
1313
benchmark.benchmark_label = row[:benchmark_label] if row.header?(:benchmark_label)
14-
benchmark.subsector = row[:technology_type] if row.header?(:technology_type) && !row.header?(:subsector)
14+
benchmark.subsector = row[:technology_type]&.strip if row.header?(:technology_type) && !row.header?(:subsector)
1515
benchmark.emissions = parse_emissions(row) if emission_headers?(row)
1616

1717
was_new_record = benchmark.new_record?
@@ -51,10 +51,12 @@ def prepare_benchmark(row)
5151
end
5252

5353
def resolve_subsector(row)
54-
return row[:subsector] if row.header?(:subsector)
55-
return row[:technology_type] if row.header?(:technology_type)
56-
57-
nil
54+
value = if row.header?(:subsector)
55+
row[:subsector]
56+
elsif row.header?(:technology_type)
57+
row[:technology_type]
58+
end
59+
value&.strip&.presence
5860
end
5961

6062
def parse_date(date)

app/views/tpi/companies/show.html.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,17 @@
310310
</div>
311311

312312
<% if show_subsector_toggle %>
313+
<% steel_pre_split = @company.sector&.name == 'Steel' && @cp_assessment&.assessment_date&.year.to_i < 2025 %>
313314
<div id="subsector-toggle" class="buttons has-addons" style="margin-top: 1rem; margin-bottom: 0.5rem;">
314315
<% @company.company_subsectors.each do |cs| %>
315316
<% has_data = subsector_assessments[cs.subsector].present? %>
316317
<% is_global_default = cs.subsector == 'Global' && !has_subsector_data %>
317-
<% if has_data || is_global_default %>
318+
<% is_pre_split_non_global = steel_pre_split && cs.subsector != 'Global' %>
319+
<% if is_pre_split_non_global %>
320+
<span class="button disabled" style="background-color: #e8e8e8; color: #999; border-color: #ddd; cursor: not-allowed;" title="No data available for this subsector for pre-2025 assessments">
321+
<%= cs.subsector %>
322+
</span>
323+
<% elsif has_data || is_global_default %>
318324
<% assessment_id = has_data ? subsector_assessments[cs.subsector].first.id : @cp_assessment&.id %>
319325
<% is_active = has_subsector_data ? (active_subsector == cs.subsector) : is_global_default %>
320326
<%= link_to cs.subsector, cp_assessment_tpi_company_path(@company, cp_assessment_id: assessment_id),

0 commit comments

Comments
 (0)