Skip to content

Commit

Permalink
Move progress bar to a class and fix red bar wrong width
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlON committed Apr 13, 2017
1 parent 85725f3 commit 5c53c83
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
11 changes: 2 additions & 9 deletions app/helpers/dashboard_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
module DashboardHelper
# :reek:FeatureEnvy
def progress_tag(current:, min: 0, max: 100, text: '')
width_persentage = current.zero? ? 0 : (current - min) * 100 / (max - min)
content_tag(:div,
content_tag(:div, text,
class: current.negative? ? 'progress-bar bg-danger' : 'progress-bar',
'aria-valuemax' => max, 'aria-valuemin' => min, 'aria-valuenow' => current,
'role' => 'progressbar', 'style' => "width: #{width_persentage}%"),
class: 'progress')
def progress_tag(current:, **options)
ProgressBar.new(current: current, **options).html_tag
end

def activity_time_tag(time)
Expand Down
34 changes: 34 additions & 0 deletions app/helpers/progress_bar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class ProgressBar
include ActionView::Helpers::TagHelper

def initialize(current:, min: 0, max: 100, text: '')
@current = current
@min = min
@max = max
@text = text
end

def html_tag
content_tag(:div,
content_tag(:div, text,
class: "progress-bar #{'bg-danger' if current.negative?}",
'aria-valuemax' => max, 'aria-valuemin' => min, 'aria-valuenow' => current,
'role' => 'progressbar',
'style' => "width: #{width_persentage}%"),
class: 'progress')
end

private

attr_accessor :current, :min, :max, :text

def width_persentage
if current.zero?
0
elsif current.positive?
current * 100 / max
else
current * 100 / (max + min)
end
end
end

0 comments on commit 5c53c83

Please sign in to comment.