]> git.openstreetmap.org Git - rails.git/blob - app/helpers/banner_helper.rb
Add thunderforest API key to example configuration
[rails.git] / app / helpers / banner_helper.rb
1 module BannerHelper
2   def active_banners
3     BANNERS.reject do |_k, v|
4       enddate = v[:enddate]
5       begin
6         parsed = enddate && Date.parse(enddate)
7       rescue
8         parsed = nil
9       end
10       parsed.is_a?(Date) && parsed.past?
11     end
12   end
13
14   # returns the least recently seen banner that is not hidden
15   def next_banner
16     banners = active_banners
17     banner_key = nil
18     cookie_key = nil
19     min_index = 9999
20
21     banners.each do |k, v|
22       ckey = banner_cookie(v[:id]).to_sym
23       cval = cookies[ckey] || 0
24       next if cval == "hide"
25
26       # rotate all banner queue positions
27       index = cval.to_i
28       cookies[ckey] = index - 1 if index > 0
29
30       # pick banner with mininum queue position
31       next if index > min_index
32
33       banner_key = k
34       cookie_key = ckey
35       min_index = index
36     end
37
38     unless banner_key.nil?
39       cookies[cookie_key] = banners.length # bump to end of queue
40       banners[banner_key]
41     end
42   end
43
44   def banner_cookie(key)
45     "_osm_banner_#{key}"
46   end
47 end