2014-11-25 10:34:14 +00:00
|
|
|
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
|
2016-02-17 12:37:37 +00:00
|
|
|
field :sort_number, :type => Integer
|
2015-05-08 07:47:29 +00:00
|
|
|
field :language_enabled, :type => Array, :default => ["en","zh_tw"]
|
2015-05-08 09:00:55 +00:00
|
|
|
field :exchange_item, :default => "1"
|
2014-11-25 10:34:14 +00:00
|
|
|
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)}
|
2016-02-17 12:37:37 +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-11-25 10:34:14 +00:00
|
|
|
|
|
|
|
def expired?
|
|
|
|
self.deadline<Time.now rescue false
|
|
|
|
end
|
|
|
|
|
2016-02-17 12:37:37 +00:00
|
|
|
def embed_url
|
|
|
|
uri = URI.parse(self.youtube) rescue nil
|
2016-02-23 06:57:02 +00:00
|
|
|
if !uri.nil? && uri.host == "www.youtube.com"
|
2016-02-17 12:37:37 +00:00
|
|
|
params = CGI.parse(uri.query)
|
|
|
|
v = params['v'].first
|
|
|
|
return "http://www.youtube.com/embed/#{v}"
|
|
|
|
else
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def youtube_thumb
|
|
|
|
uri = URI.parse(self.youtube) rescue nil
|
2016-02-22 18:16:31 +00:00
|
|
|
if !uri.nil? && uri.host == "www.youtube.com"
|
2016-02-17 12:37:37 +00:00
|
|
|
params = CGI.parse(uri.query)
|
|
|
|
v = params['v'].first
|
|
|
|
return "http://img.youtube.com/vi/#{v}/0.jpg"
|
|
|
|
else
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-25 10:34:14 +00:00
|
|
|
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
|