]> git.openstreetmap.org Git - rails.git/blob - app/helpers/banner_helper.rb
b96409d1e2b713f875067f19b2471514b761b5fa
[rails.git] / app / helpers / banner_helper.rb
1 module BannerHelper
2
3   # returns the least recently seen banner that is not hidden
4   def next_banner()
5     active_banners = {
6       :sotmus2016 => {
7         :id => 'sotmus2016',
8         :alt => 'State of the Map US 2016',
9         :link => 'http://stateofthemap.us/',
10         :img => 'banners/sotmus-2016.jpg'
11       },
12       :sotm2016 => {
13         :id => 'sotm2016',
14         :alt => 'State of the Map 2016',
15         :link => 'http://2016.stateofthemap.org/',
16         :img => 'banners/sotm-2016.jpg'
17       }
18     }
19
20     bannerKey = nil
21     cookieKey = nil
22     queuePos = 9999
23
24     active_banners.each do |k, v|
25       ckey = cookie_id(v[:id]).to_sym
26       cval = cookies[ckey] || 0
27       next if cval == 'hide'
28
29
30       # rotate all banner queue positions
31       index = cval.to_i
32       if index > 0
33         cookies[ckey] = index - 1
34       end
35
36       # pick banner with mininum queue position
37       if index <= queuePos
38         bannerKey = k
39         cookieKey = ckey
40         queuePos = index
41       end
42     end
43
44     unless bannerKey.nil?
45       cookies[cookieKey] = active_banners.length   # bump to end of queue
46       active_banners[bannerKey]
47     end
48   end
49
50   def cookie_id(key)
51     "_osm_banner_#{key}"
52   end
53
54 end