Class: Comparison::Presenter
- Inherits:
-
Comparator
- Object
- Comparator
- Comparison::Presenter
- Extended by:
- Forwardable
- Includes:
- ActionView::Helpers::TranslationHelper
- Defined in:
- lib/comparison/presenter.rb
Constant Summary collapse
- ARROWS =
{ up: '↑', down: '↓', none: '' }
Instance Attribute Summary
Attributes inherited from Comparator
Instance Method Summary collapse
-
#arrow ⇒ Object
Returns the I18n translation for ‘comparison.icons`.
-
#classes ⇒ Object
Returns the I18n translation for ‘comparison.classes`.
-
#css ⇒ Object
(also: #style)
Returns the I18n translation for ‘comparison.css`.
-
#difference(**options) ⇒ Object
Returns ‘Comparator#absolute` presented as currency.
-
#icon ⇒ Object
Returns the I18n translation for ‘comparison.icons`.
-
#percentage(delimiter: ',', precision: 0, **options) ⇒ Object
(also: #change)
Returns ‘Comparator#relative` formatted as a percentage.
Methods inherited from Comparator
#absolute, #initialize, #relative
Constructor Details
This class inherits a constructor from Comparison::Comparator
Instance Method Details
#arrow ⇒ Object
Returns the I18n translation for ‘comparison.icons`. (See also `#icon`.)
This method is intended to display a graphical representation of the comparison. Typically this would be an arrow pointing up or down.
The default implementation is as follows:
en:
comparison:
arrows:
positive_html: '↑'
negative_html: '↓'
nochange_html: ''
For example, to generate up and down arrows using Glyphicons included with Bootstrap, you could add the following translations to your application:
‘#arrows` and its sister method `#icon` perform roughly identical tasks with roughly identical intentions. The difference between the two methods is in the context in which they are intended to be used.
‘#arrows` is meant to be used from view contexts with limited functionality such as an HTML email. As such, the translations you specify should be simple enough, like HTML character entities, to work within said view context.
‘#icons` is meant to be used from full-featured view contexts. As such, `#icons` is the one to use to generate HTML tags.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/comparison/presenter.rb', line 111 def arrow case when positive? t 'comparison.arrows.positive_html', default: ARROWS[:up] when negative? t 'comparison.arrows.negative_html', default: ARROWS[:down] else t 'comparison.arrows.nochange_html', default: ARROWS[:none] end end |
#classes ⇒ Object
Returns the I18n translation for ‘comparison.classes`. (See also `#css`.)
Use these translations to specify CSS classes for tags that contain comparison data. For example:
en:
comparison:
classes:
positive: 'comparison positive'
negative: 'comparison negative'
nochange: 'comparison nochange'
.comparison.positive
color: #3c763d;
background-color: #dff0d8;
.comparison.negative
color: #a94442;
background-color: #f2dede;
.comparison.nochange
color: #777777;
content_tag cmp.difference, class: cmp.classes
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/comparison/presenter.rb', line 148 def classes case when positive? t 'comparison.classes.positive' when negative? t 'comparison.classes.negative' else t 'comparison.classes.nochange' end end |
#css ⇒ Object Also known as: style
Returns the I18n translation for ‘comparison.css`. (See also `#classes`.)
Use these translations to specify raw CSS style rules to be used when formatting comparison data. For example:
en:
comparison:
css:
positive: 'color: #3c763d; background-color: #dff0d8;'
negative: 'color: #a94442; background-color: #f2dede;'
nochange: 'color: #777777;'
content_tag cmp.difference, style: cmp.css
‘#css` and its sister method `#classes` perform very similar tasks. Use `#css` when you need to embed the CSS style rules in an HTML tag using the style attribute. Use `#classes` when you want have the CSS style rules defined in a class and want to add that class to the HTML tag.
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/comparison/presenter.rb', line 178 def css case when positive? t 'comparison.css.positive', default: '' when negative? t 'comparison.css.negative', default: '' else t 'comparison.css.nochange', default: '' end end |
#difference(**options) ⇒ Object
Returns ‘Comparator#absolute` presented as currency.
17 18 19 20 21 22 23 |
# File 'lib/comparison/presenter.rb', line 17 def difference(**) if positive? number_to_currency absolute, format: '+%u%n', ** else number_to_currency absolute, ** end end |
#icon ⇒ Object
Returns the I18n translation for ‘comparison.icons`. (See also `#arrow`.)
This method is intended to display a graphical representation of the comparison. Typically this would be an arrow pointing up or down.
No default implementation is included. You must provide the translations yourself or you will get missing translations.
For example, to generate up and down arrows using Glyphicons included with Bootstrap, you could add the following translations to your application:
en:
comparison:
icons:
positive_html: '<span class="glyphicon glyphicon-arrow-up"></span>'
negative_html: '<span class="glyphicon glyphicon-arrow-down"></span>'
nochange_html: '<span class="glyphicon glyphicon-minus"></span>'
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/comparison/presenter.rb', line 70 def icon case when positive? t 'comparison.icons.positive_html' when negative? t 'comparison.icons.negative_html' else t 'comparison.icons.nochange_html' end end |
#percentage(delimiter: ',', precision: 0, **options) ⇒ Object Also known as: change
Returns ‘Comparator#relative` formatted as a percentage.
If the relative percentage evaluates to Infinity or -Infinity, nil is returned. If it evaluates to NaN, 0 is returned.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/comparison/presenter.rb', line 30 def percentage(delimiter: ',', precision: 0, **) case when nan? || zero? number_to_percentage 0, precision: precision, ** when infinite? # TODO: Return nil, or lookup an optional representation in I18n? nil when positive? number_to_percentage relative, delimiter: delimiter, precision: precision, format: '+%n%', ** else number_to_percentage relative, delimiter: delimiter, precision: precision, ** end end |