2 # This file is part of SMR.
4 # SMR is free software: you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
9 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # SMR. If not, see <http://www.gnu.org/licenses/>.
20 # Collection of helper methods for use in all views.
21 module ApplicationHelper
24 # Return date the user is currently browsing, as +Time+.
26 if session[:smr_browse_date].nil? then Time.new
27 else Time.at(session[:smr_browse_date]) end
31 # Returns change from +val1+ to +val2+ in percent, as smr_humanize()d String.
32 def percentage_change(val1, val2)
33 return nil if val1.zero? or val2.blank?
34 smr_humanize(Percentage.change(val1, val2))
38 # Returns percentage of +val+ from +base+, as smr_humanize()d String.
39 def percentage_of(base, val)
40 if base.zero? then return nil end
41 smr_humanize(BigDecimal(val.to_s).as_percentage_of(base))
45 # humanize a number in ways useful in SMR
46 def smr_humanize(n, options={ :scientific=>false })
51 # handle 'inappropriate' data quickly
52 return '-' if n == 0.0
54 return n if n.is_a?(String)
56 # decide whether to round
57 skip_rounding = true if n.is_a?(Fixnum) and n.abs < 1000
58 skip_rounding = true if (n.nil? or n == false)
60 if n.is_a?(Percentage)
62 format = if n.to_f < 10 then '%.1f' else '%i' end
67 format = '%s'+n.strftime('%Y-%m-%d')
72 # do generic rounding if necessary
75 when 0..1 then format = '%.4f'
76 when 1..9 then format = '%.3f'
77 when 10..99 then format = '%.2f'
78 when 100..1000 then format = '%.1f'
80 format = if options[:scientific] then '%.3e' else '%i' end
85 (format + '%s') % [n, unit]
90 # Returns menu to browse SMR functionality in HTML format, see
91 # @smr_menu_items and smr_menu_addsubitem().
92 # TODO: supports one sublevel only, do we ever need more?
94 content_tag(:ul, nil, :id=>'smr_menu') {
95 @smr_menu_items.reduce('') { |ctag, top|
96 if top.second.is_a?(Hash) then
98 ctag << content_tag(:li) do
99 link_to(top.first, top.second.delete('_toplink')) +
100 content_tag(:ul, nil) do
101 top.second.reduce('') { |c, sub|
102 c << content_tag(:li, link_to(sub.first, sub.second))
108 ctag << content_tag(:li, link_to(top.first, top.second))
115 # Provide link back to original object from Smr::BlogItem. Returns nil if
116 # there is no reference on that item.
119 # - works only for +type=Comment+ right now and if the Comment is not
120 # older than 7 days (the later is intentional as we want to store THE
121 # TRUTH and not alternate it afterwards)
122 def smr_link_back(smr_blogitem, name)
123 raise 'smr_blogitem must be Smr::BlogItem' unless smr_blogitem.is_a?(Smr::BlogItem)
125 if smr_blogitem.type == 'Comment' and smr_blogitem.refid and smr_blogitem.date>7.days.ago then
126 link_to name, :controller=>:blog, :action=>:new, :id=>smr_blogitem.refid
133 # Create HTML container with page links.
135 # It will be positioned at +current_page+ with a maximum of +num_pages+
136 # and each link will direct to +basepath/?page=N+.
137 def smr_paginate(current_page, num_pages, basepath)
138 JustPaginate.page_navigation(current_page, num_pages) do |p|
139 '%s/?page=%i' % [basepath, p]