class Banner < ActiveRecord::Base
  attr_accessible :uri, :uri_cache,  :body, :published_at, :published

  mount_uploader :uri, BannerUploader

  validates_presence_of :uri
  validates_length_of :body, :maximum => 1024
  validates_presence_of :published_at

  default_scope order('published_at DESC')
  scope :visible_to_user, lambda { where(:published => true).where('published_at <= ?', Time.now).order('published_at DESC') }

  def self.random_visible_to_user(limit)
    if limit == 1
      return visible_to_user.offset(rand(count)).first
    end
    return find(visible_to_user.pluck(:id).sample(limit))
  end

end
