orbit-personalproject/app/models/project.rb

92 lines
2.0 KiB
Ruby

# encoding: utf-8
class Project
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
include OrbitModel::LanguageRestrict
include OrbitModel::Status
include OrbitTag::Taggable
LANGUAGE_TYPES = [ "English", "Chinese" ]
field :project_title, localize: true
field :job_title, localize: true
field :participator, localize: true
field :unit, localize: true
# has_and_belongs_to_many :tags, :class_name => "PersonalProjectTag"
belongs_to :project_category
field :year
field :language
field :keywords
field :abstract
field :period_start_date, :type => Date
field :period_end_date, :type => Date
field :url
field :note
field :create_user_id, :type => BSON::ObjectId
field :update_user_id, :type => BSON::ObjectId
paginates_per 10
has_many :project_files, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :project_files, :allow_destroy => true
# before_save :clean_checkboxs
validates :project_title, :at_least_one => true
before_validation :add_http
after_save :save_project_files
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?}
def self.search( category_id = nil )
if category_id.to_s.size > 0
find(:all, :conditions => {project_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
def save_project_files
self.project_files.each do |t|
if t.should_destroy
t.destroy
end
end
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
self.tagged_ids.delete('')
end
end