organized code into Smr module
[smr.git] / gui / app / helpers / application_helper.rb
blobf188a0f3359162ad83cb018509ffa1346b4b03c9
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
7 # version.
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/>.
17 require 'percentage'
20 # Collection of helper methods for use in all views.
21 module ApplicationHelper
23     ##
24     # Return date the user is currently browsing, as +Time+.
25     def smr_browse_date
26         if session[:smr_browse_date].nil? then Time.new
27         else Time.at(session[:smr_browse_date]) end
28     end
30     ##
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))
35     end
37     ##
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))
42     end
44     ##
45     # humanize a number in ways useful in SMR
46     def smr_humanize(n, options={ :scientific=>false })
47         unit = String.new
48         format = '%s'
49         skip_rounding=false
51         # handle 'inappropriate' data quickly
52         return '-' if n == 0.0
53         return '' if n == ''
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)
61             unit = '%'
62             format = if n.to_f < 10 then '%.1f' else '%i' end
63             skip_rounding = true           
64         end
66         if n.is_a?(Time)
67             format = '%s'+n.strftime('%Y-%m-%d')
68             n = nil 
69             skip_rounding = true
70         end
72         # do generic rounding if necessary
73         unless skip_rounding
74             case n.abs
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'
79             else
80                 format = if options[:scientific] then '%.3e' else '%i' end
81             end
82         end
84         # return a String
85         (format + '%s') % [n, unit]
86     end
89     ##
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?
93     def smr_menu
94         content_tag(:ul, nil, :id=>'smr_menu') {
95           @smr_menu_items.reduce('') { |ctag, top|
96             if top.second.is_a?(Hash) then
97                 # sub level
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))
103                         }.html_safe
104                     end
105                 end
106             else
107                 # top level
108                 ctag << content_tag(:li, link_to(top.first, top.second))
109             end
110           }.html_safe
111         }
112     end
114     ##
115     # Provide link back to original object from Smr::BlogItem. Returns nil if
116     # there is no reference on that item.
117     #
118     # NOTE:
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
127         else
128             nil
129         end
130     end
132     ##
133     # Create HTML container with page links.
134     #
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]
140         end.html_safe
141     end