2014-04-11 08:55:33 +00:00
|
|
|
require 'uri'
|
|
|
|
|
2014-04-11 07:47:33 +00:00
|
|
|
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
|
2014-05-09 08:42:23 +00:00
|
|
|
field :postdate , :type => DateTime, :default => Time.now
|
|
|
|
field :deadline , :type => DateTime
|
2016-02-16 12:21:14 +00:00
|
|
|
field :sort_number, :type => Integer
|
2016-05-18 08:29:39 +00:00
|
|
|
field :language_enabled, :type => Array, :default => ["en","zh_tw"]
|
2016-02-16 12:21:14 +00:00
|
|
|
|
2014-04-11 07:47:33 +00:00
|
|
|
LINK_OPEN_TYPES = ["local", "new_window"]
|
|
|
|
|
2014-04-11 08:26:34 +00:00
|
|
|
belongs_to :banner
|
2014-04-11 07:47:33 +00:00
|
|
|
|
2014-04-11 08:55:33 +00:00
|
|
|
before_validation :add_http
|
|
|
|
validates :file, presence: true
|
|
|
|
validates :out_link, format: { with: URI::regexp(%w(http https)) }, allow_blank: true
|
2014-08-14 11:54:21 +00:00
|
|
|
# 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)}
|
2016-02-16 12:21:14 +00:00
|
|
|
scope :is_expired, ->{self.and(AdImage.or({:deadline.lte=>Time.now}).selector)}
|
|
|
|
scope :not_expired, ->{self.and(AdImage.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
|
2014-08-14 11:54:21 +00:00
|
|
|
|
|
|
|
def expired?
|
|
|
|
self.deadline<Time.now rescue false
|
|
|
|
end
|
2014-04-11 08:55:33 +00:00
|
|
|
|
|
|
|
protected
|
2014-04-11 07:47:33 +00:00
|
|
|
|
2014-04-11 08:55:33 +00:00
|
|
|
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
|
2014-04-11 07:47:33 +00:00
|
|
|
end
|