Skip to content

Commit

Permalink
Merge pull request #496 from Vizzuality/refactoring/removing-beta-naming
Browse files Browse the repository at this point in the history
refactoring: Minor tweaks related to MQ changes
  • Loading branch information
martintomas authored Oct 24, 2024
2 parents 76d71e3 + b0c3ca9 commit 1953a21
Show file tree
Hide file tree
Showing 28 changed files with 210 additions and 186 deletions.
5 changes: 5 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
GCS_CREDENTIALS_FILE=
GCS_BUCKET=
MQ_BETA_ENABLED=true

# SMTP API
SMTP_API_KEY=
SMTP_API_SECRET=
SMTP_DEFAULT_FROM=
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ gem 'react-rails'
gem 'appsignal'
gem 'roo'
gem 'caxlsx'
gem 'mailjet'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ GEM
net-imap
net-pop
net-smtp
mailjet (1.8.0)
activesupport (>= 5.0.0)
faraday (~> 2.1)
rack (>= 1.4.0)
yajl-ruby
marcel (1.0.4)
matrix (0.4.2)
memoist (0.16.2)
Expand Down Expand Up @@ -547,6 +552,7 @@ GEM
rails (>= 3.2.16)
xpath (3.2.0)
nokogiri (~> 1.8)
yajl-ruby (1.4.3)
zeitwerk (2.6.18)

PLATFORMS
Expand Down Expand Up @@ -589,6 +595,7 @@ DEPENDENCIES
image_processing
language_list
listen (>= 3.0.5, < 3.2)
mailjet
pg (>= 0.18, < 2.0)
pg_search (= 2.3.2)
pry
Expand Down
5 changes: 4 additions & 1 deletion app/assets/stylesheets/tpi/_mq_beta_scores.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
&__download-button {
flex-grow: 4;
text-align: right;
display: flex;
gap: 5px;
justify-content: end;

.button {
background-color: $blue;
Expand All @@ -73,4 +76,4 @@
margin-bottom: 10px;
}
}
}
}
4 changes: 0 additions & 4 deletions app/assets/stylesheets/tpi/pages/company.scss
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@

border-left: 2px solid $hawkes-blue;

&.level5 {
background-color: rgba($tpi-level5-background-color, 0.15);
}

&__header {
font-size: 16px;
font-weight: bold;
Expand Down
10 changes: 3 additions & 7 deletions app/controllers/concerns/tpi/user_download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ def send_tpi_user_file(mq_assessments:, cp_assessments:, filename:)
.includes(sector: [:cp_units])

mq_assessments_files = mq_assessments_by_methodology.map do |methodology, assessments|
is_beta_methodology = MQ::Assessment::BETA_METHODOLOGIES.include? methodology
name = is_beta_methodology ? "#{methodology}_BETA_#{timestamp}" : "#{methodology}_#{timestamp}"
{
"MQ_Assessments_Methodology_#{name}.csv" => CSVExport::User::MQAssessments.new(assessments).call
"MQ_Assessments_Methodology_#{methodology}_#{timestamp}.csv" => CSVExport::User::MQAssessments.new(assessments).call
}
end.reduce(&:merge)

Expand All @@ -28,17 +26,15 @@ def send_tpi_user_file(mq_assessments:, cp_assessments:, filename:)
cp_assessments_csv = CSVExport::User::CompanyCPAssessments.new(cp_assessments).call
cp_assessments_regional_csv = CSVExport::User::CompanyCPAssessmentsRegional.new(cp_assessments).call
sector_benchmarks_csv = CSVExport::User::CPBenchmarks.new(cp_benchmarks).call
user_guide = File.binread(Rails.root.join('public', 'tpi', 'export_support', 'User guide TPI files.xlsx'))

files = (mq_assessments_files || {}).merge(
'Company_Latest_Assessments.csv' => latest_cp_assessments_csv,
"CP_Assessments_#{timestamp}.csv" => cp_assessments_csv,
"CP_Assessments_Regional_#{timestamp}.csv" => cp_assessments_regional_csv,
"Sector_Benchmarks_#{timestamp}.csv" => sector_benchmarks_csv,
'User guide TPI files.xlsx' => user_guide
"Sector_Benchmarks_#{timestamp}.csv" => sector_benchmarks_csv
)
if ENV['MQ_BETA_ENABLED'].to_s == 'true'
files = files.merge 'Company_Latest_Assessments_BETA_5.0.csv' => latest_cp_assessments_beta_csv
files = files.merge 'Company_Latest_Assessments_5.0.csv' => latest_cp_assessments_beta_csv
end
render zip: files.compact, filename: "#{filename} - #{timestamp}"
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/tpi/companies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module TPI
class CompaniesController < TPIController
include UserDownload

before_action :enable_beta_mq_assessments
before_action :fetch_company
before_action :redirect_if_numeric_or_historic_slug, only: [:show]
before_action :fetch_cp_assessment, only: [:show, :cp_assessment, :emissions_chart_data]
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/tpi/sectors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module TPI
class SectorsController < TPIController
include UserDownload

before_action :enable_beta_mq_assessments
before_action :fetch_companies, only: [:show, :index]
before_action :fetch_sectors, only: [:show, :index, :user_download_all]
before_action :fetch_sector, only: [:show, :user_download]
Expand Down Expand Up @@ -77,6 +78,11 @@ def cp_performance_chart_data
render json: data.chart_json
end

def send_download_file_info_email
DataDownloadMailer.send_download_file_info_email(permitted_email_params).deliver_now
head :ok
end

def user_download_all
send_user_download_file(
Company.published.select(:id).where(sector_id: @sectors.pluck(:id)),
Expand All @@ -91,6 +97,13 @@ def user_download
)
end

def user_download_methodology
user_guide = File.binread(Rails.root.join('public', 'tpi', 'export_support', 'User guide TPI files.xlsx'))
render zip: {
'User guide TPI files.xlsx' => user_guide
}, filename: "TPI Methodology - #{Time.now.strftime('%d%m%Y')}"
end

private

def any_cp_assessment?
Expand Down Expand Up @@ -155,5 +168,9 @@ def companies_scope(params)
Company.published.active
end
end

def permitted_email_params
params.permit(:email, :job_title, :forename, :surname, :location, :organisation, purposes: [])
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/tpi/tpi_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class TPIController < ApplicationController

protected

def enable_beta_mq_assessments
session[:enable_beta_mq_assessments] = true unless session.key?(:enable_beta_mq_assessments)
end

def fixed_navbar(admin_panel_section_title, admin_panel_link)
@admin_panel_section_title = admin_panel_section_title
@link = admin_panel_link
Expand Down
5 changes: 1 addition & 4 deletions app/javascript/components/tpi/charts/mq-level/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ export function getOptions({ chartData }) {
fontSize: '12px'
},
useHTML: true,
stagger: 0,
formatter() {
return this.value === 5 ? '5 [BETA]' : this.value;
}
stagger: 0
},
title: {
text: 'Level',
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: '[email protected]'
default from: ENV.fetch('SMTP_DEFAULT_FROM', '[email protected]')
layout 'mailer'
end
6 changes: 6 additions & 0 deletions app/mailers/data_download_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class DataDownloadMailer < ApplicationMailer
def send_download_file_info_email(data)
@data = data
mail(to: '[email protected]', subject: 'TPI data has been downloaded')
end
end
2 changes: 1 addition & 1 deletion app/models/mq/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Assessment < ApplicationRecord

LEVELS = %w[0 1 2 3 4 4STAR 5 5STAR].freeze
BETA_METHODOLOGIES = { # taken into account only when beta is enabled
5 => {levels: %w[5], highlight_questions: %w[18]}
5 => {levels: %w[5], highlight_questions: %w[]}
}.freeze
BETA_LEVELS = BETA_METHODOLOGIES.map { |_k, v| v[:levels] }.flatten.freeze

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h2>Following user downloaded data from TPI website</h2>

<p><strong>Email:</strong> <%= @data[:email] %></p>
<p><strong>Job Title:</strong> <%= @data[:job_title] %></p>
<p><strong>Forename:</strong> <%= @data[:forename] %></p>
<p><strong>Surname:</strong> <%= @data[:surname] %></p>
<p><strong>Location:</strong> <%= @data[:location] %></p>
<p><strong>Organisation:</strong> <%= @data[:organisation] %></p>

<h3>Purposes</h3>
<ul>
<% Array.wrap(@data[:purposes]).each do |purpose| %>
<li><%= purpose %></li>
<% end %>
</ul>
2 changes: 1 addition & 1 deletion app/views/tpi/companies/_mq_assessment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<% assessment.questions_by_level.each do |level, questions| %>
<div class="mq-assessment-level level<%= level %>">
<p class="mq-assessment-level__header">
Level <%= level.in?(assessment.beta_levels) ? "#{level} [BETA]" : level %>: <%= t("tpi.company.level_description.#{level}") %>
Level <%= "#{level}: #{t("tpi.company.level_description.#{level}")}" %>
</p>

<div class="mq-assessment-answers">
Expand Down
34 changes: 6 additions & 28 deletions app/views/tpi/companies/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<% content_for :page_title, "#{@company.name} - Transition Pathway Initiative" %>
<% content_for :page_description, "Assess how prepared #{@company.name} is for the transition to a low-carbon economy – its Management Quality and Carbon Performance." %>

<%= react_component('MqBetaModal', {
enabled: session[:enable_beta_mq_assessments],
page: "companies"
}) %>

<% regional_view = @company.cp_alignment_region.present? && params[:view] == 'regional' %>

<div class="company-page" data-controller="companies">
Expand All @@ -16,39 +11,21 @@
<div class="container is-hidden-touch">
<div class="mq-beta-scores">
<%= render 'tpi/shared/mq_beta_scores', has_data: @company.beta_mq_assessments? %>
<div>
<a href="#management-quality" class="link with-icon is-pulled-left">
<img src="<%= asset_path 'icons/arrow-down.svg'%>" alt="arrow down" />
Go to Management Quality
</a>
<% if @company_presenter.cp_assessments.any? %>
<a href="#carbon-performance" class="link with-icon is-pulled-left">
<img src="<%= asset_path 'icons/arrow-down.svg'%>" alt="arrow down" />
Go to Carbon Performance
</a>
<% end %>
</div>
<div class="mq-beta-scores__download-button">
<%= link_to user_download_methodology_tpi_sectors_path, class: 'button is-primary is-pulled-right with-icon with-border' do %>
<img src="<%= asset_path 'icons/download.svg'%>" alt="download icon" />
Download Methodology
<% end %>
<%= link_to user_download_all_tpi_sectors_path, class: 'button is-primary is-pulled-right with-icon with-border' do %>
<img src="<%= asset_path 'icons/download.svg'%>" alt="download icon" />
Download data
Download CP & MQ Data
<% end %>
</div>
</div>
</div>
<div class="container is-hidden-desktop">
<div class="mq-beta-scores is-hidden-desktop">
<%= render 'tpi/shared/mq_beta_scores', has_data: @company.beta_mq_assessments? %>
<div>
<a href="#management-quality" class="link with-icon is-pulled-left">
Management Quality
</a>
<% if @company_presenter.cp_assessments.any? %>
<a href="#carbon-performance" class="link with-icon is-pulled-left">
Carbon Performance
</a>
<% end %>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -242,6 +219,7 @@
</div>
</section>
<% end %>

<% if @company.latest_information.present? %>
<%= react_component("LatestInformation", { name: @company.name, latestInformation: @company.latest_information }) %>
<% end %>
Expand Down
63 changes: 5 additions & 58 deletions app/views/tpi/sectors/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<% content_for :page_title, "Tool - Transition Pathway Initiative" %>
<% content_for :page_description, "The TPI tool is a corporate climate action benchmark which assesses how prepared companies are for the transition to a low-carbon economy." %>

<%= react_component('MqBetaModal', {
enabled: session[:enable_beta_mq_assessments],
page: "company"
}) %>

<div class="sectors-index-page">
<div class="dropdown-selector-sector-page-wrapper">
<%= react_component("DropdownSelector", {
Expand All @@ -19,30 +14,14 @@
<div class="container">
<div class="mq-beta-scores">
<%= render 'tpi/shared/mq_beta_scores', has_data: true %>
<div>
<a href="#management-quality" class="link with-icon">
<img src="<%= asset_path 'icons/arrow-down.svg'%>" alt="arrow down" class="is-hidden-touch" />
Management Quality
</a>
<a href="#carbon-performance" class="link with-icon">
<img src="<%= asset_path 'icons/arrow-down.svg'%>" alt="arrow down" class="is-hidden-touch" />
Carbon Performance
</a>
<% if @methodology_publication.present? %>
<a href="#methodology" class="link with-icon">
<img src="<%= asset_path 'icons/arrow-down.svg'%>" alt="arrow down" class="is-hidden-touch" />
Methodology
</a>
<% end %>
<a href="#publications" class="link with-icon">
<img src="<%= asset_path 'icons/arrow-down.svg'%>" alt="arrow down" class="is-hidden-touch" />
Publications
</a>
</div>
<div class="mq-beta-scores__download-button">
<%= link_to user_download_methodology_tpi_sectors_path, class: 'button is-primary is-pulled-right with-icon with-border' do %>
<img src="<%= asset_path 'icons/download.svg'%>" alt="download icon" />
Download Methodology
<% end %>
<%= link_to user_download_all_tpi_sectors_path, class: 'button is-primary with-icon with-border' do %>
<img src="<%= asset_path 'icons/download.svg'%>" alt="download icon" />
Download data
Download CP & MQ Data
<% end %>
</div>
</div>
Expand Down Expand Up @@ -80,36 +59,4 @@
sectors: TPI::CPPerformanceSectorDecorator.decorate_collection(@sectors).map(&:to_h)
}) %>
</section>

<% if @methodology_publication.present? %>
<section id="methodology" class="container">
<div class="columns">
<div class="column pages__content">
<h4>Methodology</h4>

<div>
<%= @methodology_description&.text&.html_safe %>
</div>
</div>
<div class="column">
<%= render 'tpi/publications/list', publications_and_articles: [@methodology_publication] %>
</div>
</div>
</section>
<% end %>

<hr/>

<section id="publications" class="container tpi-sector__promoted-publications">
<div class="promoted-publications__title">
<div class="promoted-publications__title-text">Publications and news</div>

<%= link_to 'View all publications and news', tpi_publications_path, class: 'button is-secondary is-hidden-touch' %>
</div>

<%= render "tpi/publications/promoted", publications_and_articles: @publications_and_articles, count: @publications_and_articles.count %>
<div class="view-all-btn__container">
<a href="/publications" class="button is-secondary is-hidden-desktop">View all news</a>
</div>
</section>
</div>
Loading

0 comments on commit 1953a21

Please sign in to comment.