Skip to content

Commit 63a56fb

Browse files
authored
Merge pull request #788 from citizensadvice/overall-scores-2-decimal-places
chore/Update decimal places display for scores
2 parents 1615130 + 274807f commit 63a56fb

5 files changed

Lines changed: 46 additions & 18 deletions

File tree

app/components/csr_table/score_component.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
module CsrTable
44
class ScoreComponent < ViewComponent::Base
5-
def initialize(score:, show_decimal_score: false)
5+
def initialize(score:, show_decimal_score: false, decimal_places: 1, highlight_stars: false)
66
super()
77
@score = score
88
@show_decimal_score = show_decimal_score
9+
@decimal_places = decimal_places
10+
@highlight_stars = highlight_stars
911
end
1012

1113
def render?
@@ -23,8 +25,7 @@ def show_decimal_score?
2325
end
2426

2527
def highlight_stars?
26-
# We only highlight the stars when a decimal score is displayed
27-
show_decimal_score?
28+
@highlight_stars
2829
end
2930

3031
def scored?
@@ -36,13 +37,11 @@ def score_text
3637
end
3738

3839
def score_out_of_five
39-
return "#{score_number} out of 5" unless show_decimal_score?
40-
4140
"#{score_number} out of 5"
4241
end
4342

4443
def score_number
45-
show_decimal_score? ? @score.round(1) : @score.round
44+
show_decimal_score? ? format("%.#{@decimal_places}f", @score) : @score.round
4645
end
4746
end
4847
end

app/components/csr_table/score_summary_component.html.haml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
%li.score-summary__item
88
%p.score-summary__text
99
Overall rating
10-
= render CsrTable::ScoreComponent.new(score: supplier.overall_rating, show_decimal_score: true)
10+
= render CsrTable::ScoreComponent.new(score: supplier.overall_rating, show_decimal_score: true, decimal_places: 2, highlight_stars: true)
1111
%li.score-summary__item
1212
%p.score-summary__text
1313
Fewer complaints received
14-
= render CsrTable::ScoreComponent.new(score: supplier.complaints_rating)
14+
= render CsrTable::ScoreComponent.new(score: supplier.complaints_rating, show_decimal_score: true)
1515
%li.score-summary__item
1616
%p.score-summary__text
1717
Contact waiting time
18-
= render CsrTable::ScoreComponent.new(score: supplier.contact_rating)
18+
= render CsrTable::ScoreComponent.new(score: supplier.contact_rating, show_decimal_score: true)
1919
%li.score-summary__item
2020
%p.score-summary__text
2121
Billing and metering
22-
= render CsrTable::ScoreComponent.new(score: supplier.bill_accuracy_and_metering_rating)
22+
= render CsrTable::ScoreComponent.new(score: supplier.bill_accuracy_and_metering_rating, show_decimal_score: true)
2323
%li.score-summary__item
2424
%p.score-summary__text
2525
Customer commitments

app/components/csr_table/supplier_table_row_component.html.haml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
= render_overall_score
1111
%td.supplier-table__complaints
1212
.cads-table__content
13-
= render CsrTable::ScoreComponent.new(score: supplier.complaints_rating)
13+
= render CsrTable::ScoreComponent.new(score: supplier.complaints_rating, show_decimal_score: true)
1414
%td.supplier-table__response
1515
.cads-table__content
16-
= render CsrTable::ScoreComponent.new(score: supplier.contact_rating)
16+
= render CsrTable::ScoreComponent.new(score: supplier.contact_rating, show_decimal_score: true)
1717
%td.supplier-table__billing
1818
.cads-table__content
19-
= render CsrTable::ScoreComponent.new(score: supplier.bill_accuracy_and_metering_rating)
19+
= render CsrTable::ScoreComponent.new(score: supplier.bill_accuracy_and_metering_rating, show_decimal_score: true)
2020
%td.supplier-table__guarantee
2121
.cads-table__content
2222
= render CsrTable::ScoreComponent.new(score: supplier.guarantee_rating)

app/components/csr_table/supplier_table_row_component.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ def row_classes
2828

2929
def render_overall_score
3030
if highlight?
31-
render CsrTable::ScoreComponent.new(score: supplier.overall_rating, show_decimal_score: true)
31+
render CsrTable::ScoreComponent.new(score: supplier.overall_rating, show_decimal_score: true, decimal_places: 2,
32+
highlight_stars: true)
3233
else
3334
render CsrTable::ScoreComponent.new(score: supplier.overall_rating,
34-
show_decimal_score: true).with_content(more_details_link)
35+
show_decimal_score: true, decimal_places: 2,
36+
highlight_stars: true).with_content(more_details_link)
3537
end
3638
end
3739

spec/components/csr_table/score_component_spec.rb

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77

88
let(:score) { 3 }
99
let(:show_decimal_score) { nil }
10+
let(:decimal_places) { 1 }
11+
let(:highlight_stars) { false }
1012

1113
before do
12-
render_inline described_class.new(score: score.presence, show_decimal_score: show_decimal_score.presence)
14+
render_inline described_class.new(score: score.presence,
15+
show_decimal_score: show_decimal_score.presence,
16+
decimal_places: decimal_places.presence,
17+
highlight_stars: highlight_stars)
1318
end
1419

1520
it { is_expected.to have_text "3 out of 5" }
@@ -39,12 +44,34 @@
3944
it { is_expected.to have_no_css ".stars" }
4045
end
4146

47+
context "when the score highlights the stars" do
48+
let(:highlight_stars) { true }
49+
50+
it { is_expected.to have_css ".stars" }
51+
it { is_expected.to have_css ".stars--highlight" }
52+
end
53+
4254
context "when the score is shown as a decimal" do
4355
let(:score) { 1.2 }
4456
let(:show_decimal_score) { true }
4557

46-
it { is_expected.to have_text "1.2 out of 5" }
47-
it { is_expected.to have_css ".stars--highlight" }
58+
context "when the decimal_places is set to 1" do
59+
it { is_expected.to have_text "1.2 out of 5" }
60+
end
61+
62+
context "when decimal_places is set to 2" do
63+
let(:decimal_places) { 2 }
64+
65+
context "when there is a single decimal digit" do
66+
it { is_expected.to have_text "1.20 out of 5" }
67+
end
68+
69+
context "when there are two decimal digits" do
70+
let(:score) { 1.23 }
71+
72+
it { is_expected.to have_text "1.23 out of 5" }
73+
end
74+
end
4875
end
4976

5077
context "when content is provided" do

0 commit comments

Comments
 (0)