From 810a702333da5ed4eb2f6fa7712bcd7b384dfa24 Mon Sep 17 00:00:00 2001 From: Alex Basson Date: Thu, 16 Sep 2021 13:30:31 -0400 Subject: [PATCH] Refactor construct_graph_row_presenter to return negative values for x-offset and extract percentage method --- .../construct_graph_row_presenter.rb | 35 +++++++++++-------- app/views/dashboard/index.html.erb | 2 +- .../construct_graph_row_presenter_spec.rb | 6 ++-- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/presenters/construct_graph_row_presenter.rb b/app/presenters/construct_graph_row_presenter.rb index 2208d30c..c463617d 100644 --- a/app/presenters/construct_graph_row_presenter.rb +++ b/app/presenters/construct_graph_row_presenter.rb @@ -25,19 +25,7 @@ class ConstructGraphRowPresenter end def bar_width - percentage = (@score - zone.low_benchmark) / (zone.high_benchmark - zone.low_benchmark) - case zone.type - when :ideal - (percentage * ideal_zone_params.width + approval_zone_params.width).round - when :approval - (percentage * approval_zone_params.width).round - when :growth - (percentage * growth_zone_params.width).round - when :watch - (percentage * watch_zone_params.width + growth_zone_params.width).round - else - (percentage * warning_zone_params.width + watch_zone_params.width + growth_zone_params.width).round - end + unrounded_bar_width.round end def x_offset @@ -45,12 +33,31 @@ class ConstructGraphRowPresenter when :ideal, :approval 0 else - bar_width + -1 * bar_width end end private + def unrounded_bar_width + case zone.type + when :ideal + percentage * ideal_zone_params.width + approval_zone_params.width + when :approval + percentage * approval_zone_params.width + when :growth + percentage * growth_zone_params.width + when :watch + percentage * watch_zone_params.width + growth_zone_params.width + else + percentage * warning_zone_params.width + watch_zone_params.width + growth_zone_params.width + end + end + + def percentage + (@score - zone.low_benchmark) / (zone.high_benchmark - zone.low_benchmark) + end + def zone @construct.zone_for_score(@score) end diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index f4bfa0ab..d1e5b86b 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -47,7 +47,7 @@ > <%= @construct_graph_row_presenters.each_with_index do |presenter, index| %> - y=<%= index * construct_row_height + (construct_row_height - construct_row_bar_height) / 2 %> width="<%= presenter.bar_width %>" height=<%= construct_row_bar_height %> fill=<%= presenter.bar_color %> /> + y=<%= index * construct_row_height + (construct_row_height - construct_row_bar_height) / 2 %> width="<%= presenter.bar_width %>" height=<%= construct_row_bar_height %> fill=<%= presenter.bar_color %> /> <% end %> diff --git a/spec/presenters/construct_graph_row_presenter_spec.rb b/spec/presenters/construct_graph_row_presenter_spec.rb index afa88864..d402be41 100644 --- a/spec/presenters/construct_graph_row_presenter_spec.rb +++ b/spec/presenters/construct_graph_row_presenter_spec.rb @@ -77,7 +77,7 @@ RSpec.describe "construct graph row presenter" do end it('returns an x-offset equal to the bar width') do - expect(presenter.x_offset).to eq 29 + expect(presenter.x_offset).to eq -29 end end @@ -95,7 +95,7 @@ RSpec.describe "construct graph row presenter" do end it('returns an x-offset equal to the bar width') do - expect(presenter.x_offset).to be > 0 + expect(presenter.x_offset).to eq -216 end end @@ -113,7 +113,7 @@ RSpec.describe "construct graph row presenter" do end it('returns an x-offset equal to the bar width') do - expect(presenter.x_offset).to eq 424 + expect(presenter.x_offset).to eq -424 end end end