Skip to content

Commit

Permalink
refactoring: Splitting MQ download option
Browse files Browse the repository at this point in the history
  • Loading branch information
martintomas committed Oct 23, 2024
1 parent 0c8870f commit 877968f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 114 deletions.
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: 1 addition & 3 deletions app/controllers/concerns/tpi/user_download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ 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_5.0.csv' => latest_cp_assessments_beta_csv
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/tpi/sectors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,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
29 changes: 6 additions & 23 deletions app/views/tpi/companies/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,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 @@ -237,6 +219,7 @@
</div>
</section>
<% end %>

<% if @company.latest_information.present? %>
<%= react_component("LatestInformation", { name: @company.name, latestInformation: @company.latest_information }) %>
<% end %>
Expand Down
58 changes: 5 additions & 53 deletions app/views/tpi/sectors/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,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 @@ -75,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>
37 changes: 5 additions & 32 deletions app/views/tpi/sectors/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@
<div class="container is-hidden-touch">
<div class="mq-beta-scores">
<%= render 'tpi/shared/mq_beta_scores', has_data: true %>
<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 any_cp_assessment? %>
<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_tpi_sector_path(@sector), 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>
Expand Down Expand Up @@ -85,23 +77,4 @@
}) %>
</section>
<% end %>

<% if @publications_and_articles.present? %>
<hr>

<section class="section tpi-sector__promoted-publications">
<div class="container">
<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>
</div>
</section>
<% end %>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
get :levels_chart_data
get :cp_performance_chart_data
get :user_download_all
get :user_download_methodology
end
member do
get :levels_chart_data
Expand Down
31 changes: 29 additions & 2 deletions spec/controllers/tpi/sectors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@
expect(entries_names).to include('Company_Latest_Assessments_5.0.csv')
expect(entries_names).to include("MQ_Assessments_Methodology_1_#{timestamp}.csv")
expect(entries_names).to include("MQ_Assessments_Methodology_5_#{timestamp}.csv")
expect(entries_names).to include('User guide TPI files.xlsx')

expect(entries_csv_json["Sector_Benchmarks_#{timestamp}.csv"])
.to match_snapshot('tpi_single_sector_user_download_zip_sector_benchmarks_csv')
Expand Down Expand Up @@ -210,7 +209,6 @@
expect(entries_names).to include('Company_Latest_Assessments_5.0.csv')
expect(entries_names).to include("MQ_Assessments_Methodology_1_#{timestamp}.csv")
expect(entries_names).to include("MQ_Assessments_Methodology_5_#{timestamp}.csv")
expect(entries_names).to include('User guide TPI files.xlsx')

expect(entries_csv_json["Sector_Benchmarks_#{timestamp}.csv"])
.to match_snapshot('tpi_all_sectors_user_download_zip_sector_benchmarks_csv')
Expand All @@ -227,4 +225,33 @@
end
end
end

describe 'GET user download methodology' do
subject { get :user_download_methodology }

it { is_expected.to be_successful }

it 'returns zip file' do
subject
expect(response.content_type).to eq('application/zip')
end

describe 'zip file' do
it 'has proper content' do
subject

entries_names = []
entries_csv_json = {}
zip_io = StringIO.new(response.body)
Zip::File.open_buffer(zip_io) do |zipfile|
zipfile.each do |entry|
entries_names << entry.name
entries_csv_json[entry.name] = parse_csv_to_json(entry.get_input_stream.read) if entry.name.ends_with?('.csv')
end
end

expect(entries_names).to include('User guide TPI files.xlsx')
end
end
end
end

0 comments on commit 877968f

Please sign in to comment.