diff --git a/app/helpers/dashboard_helper.rb b/app/helpers/dashboard_helper.rb index 7d6039f..4eed1c5 100644 --- a/app/helpers/dashboard_helper.rb +++ b/app/helpers/dashboard_helper.rb @@ -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) diff --git a/app/helpers/progress_bar.rb b/app/helpers/progress_bar.rb new file mode 100644 index 0000000..dc02ed4 --- /dev/null +++ b/app/helpers/progress_bar.rb @@ -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