Refactor construct_graph_row_presenter to return negative values for x-offset and extract percentage method

pull/1/head
Alex Basson 4 years ago
parent 1782ef3482
commit 810a702333

@ -25,19 +25,7 @@ class ConstructGraphRowPresenter
end end
def bar_width def bar_width
percentage = (@score - zone.low_benchmark) / (zone.high_benchmark - zone.low_benchmark) unrounded_bar_width.round
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
end end
def x_offset def x_offset
@ -45,12 +33,31 @@ class ConstructGraphRowPresenter
when :ideal, :approval when :ideal, :approval
0 0
else else
bar_width -1 * bar_width
end end
end end
private 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 def zone
@construct.zone_for_score(@score) @construct.zone_for_score(@score)
end end

@ -47,7 +47,7 @@
<svg id="construct-row-bars" x="25%" y=<%= heading_gutter %>> <svg id="construct-row-bars" x="25%" y=<%= heading_gutter %>>
<%= @construct_graph_row_presenters.each_with_index do |presenter, index| %> <%= @construct_graph_row_presenters.each_with_index do |presenter, index| %>
<rect class="construct-row-bar" x=<%= graph_center - presenter.x_offset %> 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 %> /> <rect class="construct-row-bar" x=<%= graph_center + presenter.x_offset %> 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 %> <% end %>
</svg> </svg>
</g> </g>

@ -77,7 +77,7 @@ RSpec.describe "construct graph row presenter" do
end end
it('returns an x-offset equal to the bar width') do 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
end end
@ -95,7 +95,7 @@ RSpec.describe "construct graph row presenter" do
end end
it('returns an x-offset equal to the bar width') do 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
end end
@ -113,7 +113,7 @@ RSpec.describe "construct graph row presenter" do
end end
it('returns an x-offset equal to the bar width') do 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 end
end end

Loading…
Cancel
Save