fix syntax
[opensuse:software-o-o.git] / app / helpers / application_helper.rb
1 # Methods added to this helper will be available to all templates in the application.
2 module ApplicationHelper
3
4   @@theme_prefix = nil
5
6   def theme_prefix
7     return @@theme_prefix if @@theme_prefix
8     @@theme_prefix = '/themes'
9     if ActionController::Base.relative_url_root
10       @@theme_prefix = ActionController::Base.relative_url_root + @@theme_prefix
11     end
12     @@theme_prefix
13   end
14
15   def compute_asset_host(source)
16     if defined? USE_STATIC
17       if source.slice(0, theme_prefix.length) == theme_prefix
18         return "http://static.opensuse.org"
19       end
20       return "http://static.opensuse.org/hosts/#{USE_STATIC}"
21     end
22     super(source)
23   end
24
25   def setup_baseproject
26     if @baseproject
27       scr = "setCookie('search_baseproject','#{@baseproject}');\n"
28     else
29       scr = "var p = getCookie('search_baseproject'); if (p) { $(\"#baseproject option[value='\" + p + \"']\").attr('selected', 'selected'); }\n"
30     end
31     "<script type=\"text/javascript\">\n" +
32       "//<![CDATA[\n" +
33       scr +
34       "//]]>\n" +
35       "</script>\n"
36   end
37
38   def top_downloads
39     r = Rails.cache.read('top_downloads')
40     # it's possible we will have to enqueue one on cold caches
41     Delayed::Job.enqueue SearchHelperJob.new unless r
42     return r
43   end
44
45   def time_diff time
46     Time.now - Time.parse(time)
47   end
48
49   def fuzzy_time_string(time)
50     return "unknown date" if time.blank?
51     return "now" if time_diff(time) < 60
52     diff = Integer(time_diff(time)/60) # now minutes
53     return diff.to_s + (diff == 1 ? " min ago" : " mins ago") if diff < 60
54     diff = Integer(diff/60) # now hours
55     return diff.to_s + (diff == 1 ? " hour ago" : " hours ago") if diff < 24
56     diff = Integer(diff/24) # now days
57     return diff.to_s + (diff == 1 ? " day ago" : " days ago") if diff < 14
58     diff = Integer(diff/7) # now weeks
59     return diff.to_s + (diff == 1 ? " week ago" : " weeks ago") if diff < 9
60     diff = Integer(diff/4.1) # roughly months
61     return diff.to_s + " months ago"  if diff < 24
62     diff = Integer(diff/12) # years
63     return diff.to_s + " years ago"
64   end
65
66
67 end
68
69 module Enumerable
70   def version_sort
71     sort_by { |key,val|
72        key.gsub(/_SP/, '.') \
73           .gsub(/_Factory/, '_100') \
74           .gsub(/_Tumbleweed/, '_99') \
75           .split(/_/) \
76           .map { |v| v =~ /\A\d+(\.\d+)?\z/ ? -(v.to_f) : v.downcase }
77     }
78   end
79 end