Class: AdMan::Advertisement

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/ad_man/advertisement.rb

Class Method Summary collapse

Class Method Details

.render_random_ad(keyword_id = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/ad_man/advertisement.rb', line 28

def self.render_random_ad(keyword_id = nil)
  ads = fetch_ads(keyword_id)
  if ads.present? 
    total_times = 1.0
    total_priority = 0.0
    ads.each { |advertisement| 
      total_times += advertisement.display_count
      total_priority += advertisement.priority
    }
    ad = ads[rand(ads.size)]
    while((ad.display_count / total_times) > (ad.priority / total_priority))
      ad = ads[rand(ads.size)]
    end
    ad
  end
end