orbit-basic/vendor/built_in_modules/personal_honor/app/models/honor.rb

73 lines
1.5 KiB
Ruby
Raw Normal View History

2012-10-08 08:20:21 +00:00
# encoding: utf-8
class Honor
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
2013-04-29 06:59:08 +00:00
include OrbitModel::LanguageRestrict
include OrbitModel::Status
2013-04-29 06:59:08 +00:00
include OrbitTag::Taggable
2012-10-08 08:20:21 +00:00
LANGUAGE_TYPES = [ "English", "Chinese" ]
2013-04-29 06:59:08 +00:00
# has_and_belongs_to_many :tags, :class_name => "PersonalHonorTag"
2012-10-08 08:20:21 +00:00
belongs_to :honor_category
field :year
field :award_name
field :awarding_unit
field :language
field :keywords
field :url
field :note
2012-11-02 10:28:23 +00:00
field :create_user_id, :type => BSON::ObjectId
field :update_user_id, :type => BSON::ObjectId
paginates_per 10
2012-10-08 08:20:21 +00:00
# before_save :clean_checkboxs
2012-10-08 08:20:21 +00:00
validates_presence_of :award_name
before_validation :add_http
validates :url, :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, :unless => Proc.new{self.url.blank?}
2012-10-08 08:20:21 +00:00
def self.search( category_id = nil )
if category_id.to_s.size > 0
find(:all, :conditions => {honor_category_id: category_id}).desc( :is_top, :title )
else
find(:all).desc( :is_top, :title)
end
end
def self.widget_datas
where( :is_hidden => false ).desc(:is_top, :created_at)
end
protected
def add_http
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
def clean_checkboxs
2013-05-03 04:11:48 +00:00
self.tagged_ids.delete('')
2012-10-08 08:20:21 +00:00
end
end