Module: Megatron::ProgressHelper

Defined in:
app/helpers/megatron/progress_helper.rb

Instance Method Summary collapse

Instance Method Details

#capacity_bar(size, total = 100, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/megatron/progress_helper.rb', line 38

def capacity_bar(size, total=100, options={})
  # make total an optional argument
  if total.is_a?(Hash)
    options = total
    total = 100
  end

  percentage = ((size.to_f / total.to_f) * 100).round

  if percentage > 90
    options[:color] = 'red'
  elsif percentage > 80
    options[:color] = 'orange'
  end

  unless options.delete(:label) == false
    options[:label] = "#{percentage}% - #{number_to_human_size(size).sub(' ','')} / #{number_to_human_size(total).sub(' ','')}"
  end

  progress_bar(percentage, options)
end

#progress_bar(percentage, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/megatron/progress_helper.rb', line 3

def progress_bar(percentage, options={})
  
  options = {
    percentage: percentage,
    label_position: 'before'
  }.merge(options)

  if options[:label] == true
    options[:label] = "#{percentage}%"
  end

  # Just in case something exceends 100%
  percentage = 100 if percentage > 100

  width = if options[:width]
    "width: #{options.delete(:width)};"
  else
    ''
  end

  color = options.delete(:color) || 'blue'

  (:span, class: 'progress-bar-wrapper') { 
    if options[:label] && options[:label_position].to_s == 'before'
      concat (:span, class: "progress-bar-label"){ options[:label] }
    end
    concat (:span, class: 'progress-bar', data: options, style: width) { 
      (:span, class: "progress-bar-fill #{color}-bg", style: "width: #{percentage}%"){}
    }
    if options[:label] && options[:label_position].to_s == 'after'
      concat (:span, class: "progress-bar-label"){ options[:label] }
    end
  }
end