orbit-basic/vendor/built_in_modules/web_resource/app/models/web_link.rb

65 lines
1.4 KiB
Ruby
Raw Normal View History

2012-02-15 07:38:27 +00:00
# encoding: utf-8
class WebLink
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
2013-12-16 08:08:12 +00:00
include Sunspot::Mongoid2
2013-04-26 03:18:57 +00:00
include OrbitCategory::Categorizable
include OrbitModel::LanguageRestrict
include OrbitModel::Status
include OrbitModel::TimeFrame
include OrbitTag::Taggable
2012-07-25 20:10:15 +00:00
field :title, localize: true
field :context, localize: true
2012-02-15 07:38:27 +00:00
field :url, localize: true
2012-02-15 07:38:27 +00:00
field :create_user_id
field :update_user_id, :class_name => "User"
2012-02-15 07:38:27 +00:00
2013-11-05 03:23:37 +00:00
scope :can_display,where(is_hidden: false)
# belongs_to :web_link_category
2012-02-15 07:38:27 +00:00
before_save :clean_tags
validates :title, :at_least_one => true
2012-02-15 07:38:27 +00:00
before_validation :add_http
# validates :url, :presence => true, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i
2012-02-16 03:30:54 +00:00
def self.search( category_id = nil )
2012-02-15 07:38:27 +00:00
2012-02-16 03:30:54 +00:00
if category_id.to_s.size > 0
2012-02-15 07:38:27 +00:00
2012-07-25 20:10:15 +00:00
find(:all, :conditions => {web_link_category_id: category_id}).desc( :is_top, :title )
2012-02-16 03:30:54 +00:00
2012-02-15 07:38:27 +00:00
else
2012-07-25 20:10:15 +00:00
find(:all).desc( :is_top, :title)
2012-02-15 07:38:27 +00:00
end
end
def self.widget_datas
where( :is_hidden => false ).desc(:is_top, :created_at)
2012-02-15 07:38:27 +00:00
end
protected
def add_http
unless self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
def clean_tags
2013-05-03 04:11:48 +00:00
self.tagged_ids.delete('')
end
2012-02-15 07:38:27 +00:00
end