Skip to content

Instantly share code, notes, and snippets.

@hmlON
Created April 13, 2017 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmlON/d77d086a3b263c9d677934c53eb53516 to your computer and use it in GitHub Desktop.
Save hmlON/d77d086a3b263c9d677934c53eb53516 to your computer and use it in GitHub Desktop.
Ruby helper for html progress bar tag with bootstrap
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