links/app/models/web_link.rb

40 lines
1.0 KiB
Ruby
Raw Normal View History

2014-05-05 11:33:35 +00:00
# encoding: utf-8
2014-05-05 08:20:37 +00:00
class WebLink
include Mongoid::Document
2014-05-05 11:33:35 +00:00
include Mongoid::Timestamps
include OrbitModel::Status
2014-05-21 08:07:53 +00:00
include OrbitModel::Impression
2014-05-05 11:33:35 +00:00
include OrbitTag::Taggable
include OrbitCategory::Categorizable
field :title, localize: true
field :context, localize: true
2015-05-05 08:59:29 +00:00
field :order_position, type: Integer, default: -1
2014-05-05 11:33:35 +00:00
field :url, localize: true
field :create_user_id
field :update_user_id
2014-06-16 09:18:11 +00:00
field :rss2_id, type: String
2015-01-06 05:46:22 +00:00
field :link_open, type: String
LINK_OPEN_TYPES = ["local", "new_window"]
2014-05-05 11:33:35 +00:00
2015-05-05 08:59:29 +00:00
scope :can_display, ->{where(is_hidden: false).order_by([:is_top,:desc],[:order_position,:asc])}
2014-05-05 11:33:35 +00:00
2014-06-06 11:06:02 +00:00
before_save :add_http
2014-05-05 11:33:35 +00:00
protected
def add_http
2014-06-06 11:06:02 +00:00
temp_url = {}
Site.first.in_use_locales.each do |locale|
locale = locale.to_s
temp_url[locale] = self.url_translations[locale]
unless /https?:\/\/[\S]+/.match(self.url_translations[locale]) || self.url_translations[locale].blank?
temp_url[locale] = 'http://' + self.url_translations[locale]
end
2014-05-05 11:33:35 +00:00
end
2014-06-06 11:06:02 +00:00
self.url_translations = temp_url
2014-05-05 11:33:35 +00:00
end
end