PageRenderTime 54ms CodeModel.GetById 27ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/widgets/progressbar_helper.rb

http://github.com/paolodona/rails-widgets
Ruby | 39 lines | 28 code | 2 blank | 9 comment | 6 complexity | dd6594725e68088b93673aead3a761ae MD5 | raw file
Possible License(s): MIT
 1module Widgets
 2  module ProgressbarHelper  
 3    include CssTemplate
 4    
 5    # show a progressbar
 6    #
 7    # eg: <%= progressbar 35, :generate_css => true %>
 8    # or  <%= progressbar [35,78,15] %>
 9    #
10    # options
11    # ===
12    #   :generate_css defaults to false 
13    #   :adjust       defaults to false
14    def progressbar values, options={}
15      raise ArgumentError, "Missing value(s) parameter in progressbar call" unless values
16      raise ArgumentError, "The value parameter has to be a Numeric o Array" unless values.kind_of?(Array) or values.kind_of?(Numeric)
17      if values.kind_of? Numeric # single value
18        total = 100 
19        values = [values]
20      else # Array of values
21        total = values.sum
22      end 
23
24      html = ""
25      html << render_css('progressbar') if options[:generate_css] == true
26      html << '<div class="progressbar">'          
27      values.dup.each_with_index do |value, index|
28        if total == 0
29          percentage = 0
30        else
31          percentage = options[:adjust] ? (value * 100 / total) : value
32        end 
33        css_class = "progressbar_color_#{index.modulo(10)}"
34        html << "<div style='width: #{percentage}%;' class='#{css_class}'></div>"
35      end 
36      html << "</div>" 
37    end
38  end
39end