links-rails7/app/models/web_link.rb

48 lines
1.4 KiB
Ruby

# encoding: utf-8
class WebLink
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
include OrbitTag::Taggable
include OrbitCategory::Categorizable
field :image_description, type: String, localize: true
mount_uploader :image, ImageUploader
field :title, localize: true
field :context, localize: true
field :order_position, type: Integer, default: -1
field :url, localize: true
field :create_user_id
field :update_user_id
field :rss2_id, type: String
field :click_count, type: Integer, default: 0
field :postdate, type: DateTime, default: Time.now
field :deadline, type: DateTime
field :link_open, type: String
LINK_OPEN_TYPES = ["local", "new_window"]
scope :can_display, ->{where(is_hidden: false).and(WebLink.unscoped.or({:postdate.lte=>Time.now},{:postdate=>nil}).selector,WebLink.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector).order_by([:is_top,:desc],[:order_position,:asc])}
before_save :add_http
protected
def add_http
temp_url = {}
Site.first.in_use_locales.each do |locale|
locale = locale.to_s
tmp = self.url_translations[locale]
temp_url[locale] = tmp
unless tmp.match(/^http(s|):\/\//) || tmp.blank? || tmp.start_with?('/')
temp_url[locale] = 'https://' + tmp
end
end
self.url_translations = temp_url
end
end