Sha256: ba6de739c1095f948ecfdf33f80a6ad28b17c66bb0a530c075463e45e68589e0
Contents?: true
Size: 1.2 KB
Versions: 3
Compression:
Stored size: 1.2 KB
Contents
class Ad < ActiveRecord::Base FREQUENCIES = 1..10 AUDIENCES = %w(all logged_in logged_out) validates_presence_of :html validates_inclusion_of :audience, :in => AUDIENCES validates_inclusion_of :frequency, :in => FREQUENCIES attr_accessible :html, :name, :frequency, :audience, :published, :time_constrained, :start_date, :end_date, :location def self.display(location, logged_in = false) return nil if location.blank? ads = find(:all, :conditions => ["location = ? AND published = ? AND (time_constrained = ? OR (start_date > ? AND end_date < ?)) AND (audience IN (?) )", location.to_s, true, false, Time.now, Time.now, audiences_for(logged_in) ]) ad = random_weighted(ads.map{|a| [a, a.frequency] }) ad ? ad.html.html_safe : '' end def self.audiences_for(logged_in) ["all", "logged_#{logged_in ? 'in' : 'out'}"] end def self.frequencies_for_select FREQUENCIES.map{|f| [f, f.to_s]} end def self.audiences_for_select AUDIENCES.map{|a| [a, a.to_s]} end def self.random_weighted(items) total = 0 pick = nil items.each do |item, weight| pick = item if rand(total += weight) < weight end pick end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
community_engine-2.3.2 | app/models/ad.rb |
community_engine-2.3.1 | app/models/ad.rb |
community_engine-2.3.0 | app/models/ad.rb |