Created
April 13, 2017 20:54
-
-
Save hmlON/d77d086a3b263c9d677934c53eb53516 to your computer and use it in GitHub Desktop.
Ruby helper for html progress bar tag with bootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment