41 lines
1.1 KiB
Ruby
41 lines
1.1 KiB
Ruby
|
require 'uri'
|
||
|
|
||
|
class AdImage
|
||
|
include Mongoid::Document
|
||
|
include Mongoid::Timestamps
|
||
|
|
||
|
mount_uploader :file, ImageUploader
|
||
|
|
||
|
field :title, type: String, localize: true
|
||
|
field :context, type: String, localize: true
|
||
|
field :weight, type: Integer, default: 1
|
||
|
field :out_link, type: String
|
||
|
field :link_open, type: String
|
||
|
field :postdate , :type => DateTime, :default => Time.now
|
||
|
field :deadline , :type => DateTime
|
||
|
field :youtube , :type => String
|
||
|
field :exchange_item
|
||
|
LINK_OPEN_TYPES = ["local", "new_window"]
|
||
|
|
||
|
belongs_to :banner
|
||
|
|
||
|
before_validation :add_http
|
||
|
# validates :file, presence: true
|
||
|
validates :out_link, format: { with: URI::regexp(%w(http https)) }, allow_blank: true
|
||
|
# validates :title, presence: true
|
||
|
|
||
|
scope :can_display, ->{self.and(AdImage.or({:postdate.lte=>Time.now},{:postdate=>nil}).selector,AdImage.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
|
||
|
|
||
|
def expired?
|
||
|
self.deadline<Time.now rescue false
|
||
|
end
|
||
|
|
||
|
protected
|
||
|
|
||
|
def add_http
|
||
|
unless self.out_link.blank? || self.out_link[/^http:\/\//] || self.out_link[/^https:\/\//]
|
||
|
self.out_link = 'http://' + self.out_link
|
||
|
end
|
||
|
end
|
||
|
end
|