Class: ScoreCard
- Inherits:
-
Object
- Object
- ScoreCard
- Defined in:
- app/helpers/score_card_helper.rb
Instance Method Summary collapse
- #fraction(label, numerator, denominator, options = {}) ⇒ Object
- #generate ⇒ Object
-
#initialize(view, options = {}, size) ⇒ ScoreCard
constructor
A new instance of ScoreCard.
- #percent(label, value, options = {}) ⇒ Object
- #raw(content = nil, &block) ⇒ Object
- #score(label, value, options = {}) ⇒ Object
Constructor Details
#initialize(view, options = {}, size) ⇒ ScoreCard
Returns a new instance of ScoreCard.
15 16 17 18 19 20 |
# File 'app/helpers/score_card_helper.rb', line 15 def initialize(view, ={}, size) @view = view @options = @size = size @scores = [] end |
Instance Method Details
#fraction(label, numerator, denominator, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/helpers/score_card_helper.rb', line 52 def fraction(label, numerator, denominator, ={}) return score(label, numerator, ) unless denominator precision = .delete(:precision) if precision numerator = @view.number_with_precision(numerator, precision: precision) denominator = @view.number_with_precision(denominator, precision: precision) end value = "<span class=\"numerator\">#{numerator}</span>" + "<span class=\"divided-by\">/</span>" + "<span class=\"denominator\">#{denominator}</span>" score(label, value.html_safe, .merge(score_count_class: "fraction")) end |
#generate ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'app/helpers/score_card_helper.rb', line 76 def generate css = ["score"].concat Array(@options.fetch(:class, [])) css << "score-giant" if @size == :giant css << "score-large" if @size == :large css << "score-medium" if @size == :medium css << "score-small" if @size == :small @view.content_tag(:div, @scores.join.html_safe, @options.slice(:style).merge(:class => css.join(" "))) end |
#percent(label, value, options = {}) ⇒ Object
67 68 69 |
# File 'app/helpers/score_card_helper.rb', line 67 def percent(label, value, ={}) score(label, value, .reverse_merge(precision: 0).merge(score_count_class: "percent")) end |
#raw(content = nil, &block) ⇒ Object
71 72 73 74 |
# File 'app/helpers/score_card_helper.rb', line 71 def raw(content=nil, &block) content = @view.capture(&block) if block_given? @scores << content end |
#score(label, value, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/helpers/score_card_helper.rb', line 22 def score(label, value, ={}) score_count_class = Array(.delete(:score_count_class)) << "score-count" precision = .delete(:precision) css = Array(.fetch(:class, [])) if value.is_a?(Numeric) css << "zero" if value == 0 css << "positive" if value > 0 css << "negative" if value < 0 end if value.is_a?(Float) && (value.nan? || value.infinite?) value = "—".html_safe css << "nan" elsif precision value = @view.number_with_precision(value, precision: precision) if precision end value = value.to_s.gsub(/\./, '<span class="period">.</span>') value = [:prefix] + value if [:prefix] value << "<span class=\"unit\">#{[:unit]}</span>" if [:unit] @scores << @view.content_tag(:p, :class => css.join(" ")) do @view.content_tag(:span, value.html_safe, :class => score_count_class.join(" ")) + @view.content_tag(:span, label, :class => "score-label") end nil end |