Skip to content

Commit

Permalink
Make progress information visible in the info card (#377)
Browse files Browse the repository at this point in the history
* Make progress information visible in the info card
  • Loading branch information
adrianna-chang-shopify authored Mar 23, 2021
1 parent afadabb commit 601b70a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
5 changes: 3 additions & 2 deletions app/helpers/maintenance_tasks/tasks_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ def progress(run)

progress = Progress.new(run)

tag.progress(
progress_bar = tag.progress(
value: progress.value,
max: progress.max,
title: progress.title,
class: ["progress"] + STATUS_COLOURS.fetch(run.status)
)
progress_text = tag.p(tag.i(progress.text))
tag.div(progress_bar + progress_text, class: "block")
end

# Renders a span with a Run's status, with the corresponding tag class
Expand Down
24 changes: 13 additions & 11 deletions app/models/maintenance_tasks/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module MaintenanceTasks
# This class generates progress information about a Run.
class Progress
include ActiveSupport::NumberHelper
include ActionView::Helpers::TextHelper

# Sets the Progress initial state with a Run.
#
Expand Down Expand Up @@ -41,22 +42,23 @@ def max
estimatable? ? @run.tick_total : @run.tick_count
end

# The title for the progress information. This is a text that describes the
# progress of the Run so far. It includes the percentage that is done out of
# the maximum, if an estimate is possible.
# The text containing progress information. This describes the progress of
# the Run so far. It includes the percentage done out of the maximum, if an
# estimate is possible.
#
# @return [String] the title for the Run progress.
def title
# @return [String] the text for the Run progress.
def text
count = @run.tick_count
total = @run.tick_total
if !total?
"Processed #{@run.tick_count} #{'item'.pluralize(@run.tick_count)}."
"Processed #{pluralize(count, 'item')}."
elsif over_total?
"Processed #{@run.tick_count} #{'item'.pluralize(@run.tick_count)} " \
"(expected #{@run.tick_total})."
"Processed #{pluralize(count, 'item')} (expected #{total})."
else
percentage = 100.0 * @run.tick_count / @run.tick_total
percentage = 100.0 * count / total

"Processed #{@run.tick_count} out of #{@run.tick_total} "\
"(#{number_to_percentage(percentage, precision: 0)})"
"Processed #{count} out of #{pluralize(total, 'item')} "\
"(#{number_to_percentage(percentage, precision: 0)})."
end
end

Expand Down
2 changes: 0 additions & 2 deletions app/views/maintenance_tasks/runs/info/_running.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<p>
<% if run.estimated_completion_time %>
<%= estimated_time_to_completion(run).capitalize %> remaining.
<% else %>
Processed <%= pluralize run.tick_count, 'item' %> so far.
<% end %>
</p>
19 changes: 11 additions & 8 deletions test/helpers/maintenance_tasks/tasks_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,32 @@ class TasksHelperTest < ActionView::TestCase
assert_equal expected_trace, format_backtrace(backtrace)
end

test "#progress renders a <progress> when Run has started" do
test "#progress returns a <div> with a <progress> and progress text when Run has started" do
@run.started_at = Time.now

Progress.expects(:new).with(@run).returns(
mock(value: 42, max: 84, title: "Almost there!")
mock(value: 42, max: 84, text: "Almost there!")
)

expected = '<progress value="42" max="84" title="Almost there!" '\
'class="progress is-primary is-light"></progress>'
expected = '<div class="block"><progress value="42" max="84" '\
'class="progress is-primary is-light"></progress>'\
"<p><i>Almost there!</i></p></div>"
assert_equal expected, progress(@run)
end

test "#progress is nil if the Run has not started" do
assert_nil progress(@run)
end

test "#progress returns a <progress> with no value when the Progress value is nil" do
test "#progress returns a a <div> with a <progress> with no value when the Progress value is nil" do
@run.started_at = Time.now
Progress.expects(:new).with(@run).returns(
mock(value: nil, max: 84, title: "Almost there!")
mock(value: nil, max: 84, text: "Almost there!")
)
expected = '<progress max="84" title="Almost there!" '\
'class="progress is-primary is-light"></progress>'

expected = '<div class="block"><progress max="84" '\
'class="progress is-primary is-light"></progress>'\
"<p><i>Almost there!</i></p></div>"
assert_equal expected, progress(@run)
end

Expand Down
16 changes: 8 additions & 8 deletions test/models/maintenance_tasks/progress_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,24 @@ class ProgressTest < ActiveSupport::TestCase
assert_equal 8, @progress.max
end

test "#title returns a description with tick count, tick total, and percentage" do
assert_equal "Processed 4 out of 7 (57%)", @progress.title
test "#text returns a description with tick count, tick total, and percentage" do
assert_equal "Processed 4 out of 7 items (57%).", @progress.text
end

test "#title returns a description with tick count when tick total is not present" do
test "#text returns a description with tick count when tick total is not present" do
@run.tick_total = nil
assert_equal "Processed 4 items.", @progress.title
assert_equal "Processed 4 items.", @progress.text
end

test "#title returns a description with tick count and tick total when tick count is greater than its tick total" do
test "#text returns a description with tick count and tick total when tick count is greater than its tick total" do
@run.tick_count = 8
assert_equal "Processed 8 items (expected 7).", @progress.title
assert_equal "Processed 8 items (expected 7).", @progress.text
end

test "#title pluralizes the description according to the tick count" do
test "#text pluralizes the description according to the tick count" do
@run.tick_count = 1
@run.tick_total = nil
assert_equal "Processed 1 item.", @progress.title
assert_equal "Processed 1 item.", @progress.text
end
end
end

0 comments on commit 601b70a

Please sign in to comment.