orbit-basic/vendor/built_in_modules/personal_conference/app/models/writing_conference.rb

129 lines
3.0 KiB
Ruby
Raw Normal View History

2012-09-13 10:13:34 +00:00
# encoding: utf-8
2013-01-23 10:35:00 +00:00
class WritingConference
2012-09-13 10:13:34 +00:00
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
# scope :available_for_lang, ->(locale){ where("available_for_#{locale}".to_sym => true) }
LANGUAGE_TYPES = [ "English", "Chinese" ]
field :paper_title, localize: true
2013-01-23 10:35:00 +00:00
field :conference_title, localize: true
2012-09-13 10:13:34 +00:00
field :authors, localize: true
field :location, localize: true
field :sponsor, localize: true
2013-01-23 10:35:00 +00:00
has_and_belongs_to_many :tags, :class_name => "PersonalConferenceTag"
2012-09-13 10:13:34 +00:00
2013-01-23 10:35:00 +00:00
has_and_belongs_to_many :conference_author_types
has_and_belongs_to_many :conference_paper_types
2012-09-13 10:13:34 +00:00
field :year
field :language
field :period_start_date, :type => Date
field :period_end_date, :type => Date
field :keywords
field :abstract
field :publication_date, :type => Date
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
2012-09-13 10:13:34 +00:00
# field :is_top, :type => Boolean, :default => false
# field :is_hot, :type => Boolean, :default => false
# field :is_hidden, :type => Boolean, :default => false
2013-01-23 10:35:00 +00:00
has_many :writing_conference_files, :autosave => true, :dependent => :destroy
2012-09-13 10:13:34 +00:00
2013-01-23 10:35:00 +00:00
accepts_nested_attributes_for :writing_conference_files, :allow_destroy => true
2012-09-13 10:13:34 +00:00
# before_save :update_avliable_language, :clean_checkboxs
validates :paper_title, :at_least_one => true
before_validation :add_http
2013-01-23 10:35:00 +00:00
after_save :save_writing_conference_files
2012-09-13 10:13:34 +00:00
validates :url, :format => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix, :unless => Proc.new{self.url.blank?}
def self.search( category_id = nil )
if category_id.to_s.size > 0
2013-01-23 10:35:00 +00:00
find(:all, :conditions => {writing_conference_category_id: category_id}).desc( :is_top, :title )
2012-09-13 10:13:34 +00:00
else
find(:all).desc( :is_top, :title)
end
end
def self.widget_datas
where( :is_hidden => false ).desc(:is_top, :created_at)
end
def is_top?
self.is_top
end
def sorted_tags
tags.order_by(I18n.locale, :asc)
end
def update_avliable_language
VALID_LOCALES.each do |locale|
if (title_translations[locale].blank? rescue true)
self["available_for_#{locale}".to_sym] = false
else
self["available_for_#{locale}".to_sym] = true
end
end
end
2013-01-23 10:35:00 +00:00
def save_writing_conference_files
self.writing_conference_files.each do |t|
2012-09-13 10:13:34 +00:00
if t.should_destroy
t.destroy
end
end
end
def create_link
title = ["\"#{self.paper_title}\""]
2013-01-23 10:35:00 +00:00
title << self.conference_title
2012-09-13 10:13:34 +00:00
title << self.sponsor
title << self.location
title << "#{period_start_date}-#{period_end_date}"
2013-01-23 10:35:00 +00:00
# title << "(#{self.conference_paper_types.collect{|x| x.title}.join(', ')})"
2012-09-13 10:13:34 +00:00
title.join(', ')
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.tag_ids.delete('')
2013-01-23 10:35:00 +00:00
self.conference_author_type_ids.delete('')
self.conference_level_type_ids.delete('')
2012-09-13 10:13:34 +00:00
end
2013-01-23 10:35:00 +00:00
end