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