Skip to content

Commit

Permalink
refactoring: Hide V4.0 MQ assessments if V5.0 BETA data exists with s…
Browse files Browse the repository at this point in the history
…ame assessment date
  • Loading branch information
martintomas committed Nov 7, 2023
1 parent fd7aa24 commit ade53ec
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
7 changes: 7 additions & 0 deletions app/helpers/mq_assessment_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module MQAssessmentHelper
def hide_mq_assessments_with_same_date(assessments)
result = []
assessments.group_by(&:assessment_date).each { |_date, a| result << a.max_by(&:methodology_version) }
result
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 @@ -49,7 +49,7 @@ def previous_assessments
# keep select and sort by, company - mq assessments will be cached once
company
.mq_assessments
.currently_published
.select { |a| a.publication_date <= DateTime.now }
.select { |a| a.assessment_date < assessment_date }
.sort_by(&:assessment_date)
end
Expand Down
8 changes: 7 additions & 1 deletion app/services/api/charts/mq_assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def company_mq_assessments
@company_mq_assessments ||= begin
query = company.mq_assessments.currently_published.order(:assessment_date)
query = query.without_beta_methodologies unless @enable_beta_mq_assessments
query
hide_mq_assessments_with_same_date query
end
end

Expand All @@ -57,6 +57,12 @@ def max_level

beta_assessment.beta_levels.last.to_i
end

def hide_mq_assessments_with_same_date(assessments)
result = []
assessments.group_by(&:assessment_date).each { |_date, a| result << a.max_by(&:methodology_version) }
result
end
end
end
end
4 changes: 2 additions & 2 deletions app/views/tpi/companies/_mq_assessment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

<div class="column">
<div class="mq-assessment-box">
<div class="is-hidden-touch">Nr of assessments: <%= assessment.previous_assessments.size + 1 %></div>
<div class="is-hidden-touch">Nr of assessments: <%= hide_mq_assessments_with_same_date(assessment.previous_assessments).size + 1 %></div>
<div class="is-hidden-desktop">
Number of assessments: <span class="assessment-count"><%= assessment.previous_assessments.size + 1 %></span>
Number of assessments: <span class="assessment-count"><%= hide_mq_assessments_with_same_date(assessment.previous_assessments).size + 1 %></span>
</div>

<%= react_component('charts/mq-level', {
Expand Down
4 changes: 2 additions & 2 deletions app/views/tpi/companies/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<div class="summary-box summary-box--assessment">
<h4>Management Quality</h4>
<p>
<small>Number of assessments: <%= @company_presenter.mq_assessments.size %></small>
<small>Number of assessments: <%= hide_mq_assessments_with_same_date(@company_presenter.mq_assessments).size %></small>
</p>
<% if @company_presenter.mq_assessments.any? %>
<%= render 'mq_level', level: @company.mq_level, status: @company.mq_status %>
Expand Down Expand Up @@ -170,7 +170,7 @@
name: 'mq_assessment_id',
remote: true,
url: mq_assessment_tpi_company_path(@company),
data: @company_presenter.mq_assessments.map {|v| {label: v['assessment_date']&.strftime('%d %B %Y'), value: v['id']}},
data: hide_mq_assessments_with_same_date(@company_presenter.mq_assessments).map {|v| {label: v['assessment_date']&.strftime('%d %B %Y'), value: v['id']}},
selected: params[:mq_assessment_id]
}) %>
</div>
Expand Down
6 changes: 4 additions & 2 deletions spec/services/api/charts/mq_assessment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
create(:mq_assessment, company: company, assessment_date: '2018-08-08', publication_date: '2018-08-08', level: '2')
create(:mq_assessment, company: company, assessment_date: '2019-02-02', publication_date: '2019-02-02', level: '4')
create(:mq_assessment, company: company, assessment_date: '2020-03-03', publication_date: '2020-03-03', level: '3')
create(:mq_assessment, company: company, assessment_date: '2021-03-03', publication_date: '2021-03-03', level: '4')
# beta scores
create(:mq_assessment,
company: company,
Expand All @@ -43,7 +44,8 @@
['01/01/2018', 3],
['08/08/2018', 2],
['02/02/2019', 4],
['03/03/2020', 3]
['03/03/2020', 3],
['03/03/2021', 4]
],
name: 'Level'
},
Expand All @@ -64,7 +66,7 @@
context 'when beta assessments are enabled' do
subject do
described_class.new(
company.mq_assessments.where(assessment_date: '2021-03-03').first,
company.mq_assessments.where(assessment_date: '2021-03-03', methodology_version: beta_methodology).first,
enable_beta_mq_assessments: true
)
end
Expand Down

0 comments on commit ade53ec

Please sign in to comment.