106 lines
2.4 KiB
Ruby
106 lines
2.4 KiB
Ruby
# encoding: utf-8
|
|
|
|
class Research
|
|
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 :research_title, localize: true
|
|
field :authors, localize: true
|
|
field :extracted_chapters, localize: true
|
|
|
|
has_and_belongs_to_many :tags, :class_name => "PersonalResearchTag"
|
|
|
|
field :year
|
|
field :language
|
|
field :publish_date , :type => Date
|
|
field :keywords
|
|
field :url
|
|
field :note
|
|
field :create_user_id, :type => BSON::ObjectId
|
|
field :update_user_id, :type => BSON::ObjectId
|
|
|
|
# field :is_top, :type => Boolean, :default => false
|
|
# field :is_hot, :type => Boolean, :default => false
|
|
# field :is_hidden, :type => Boolean, :default => false
|
|
|
|
has_many :research_files, :autosave => true, :dependent => :destroy
|
|
|
|
accepts_nested_attributes_for :research_files, :allow_destroy => true
|
|
|
|
# before_save :update_avliable_language, :clean_checkboxs
|
|
|
|
validates :research_title, :at_least_one => true
|
|
|
|
before_validation :add_http
|
|
|
|
after_save :save_research_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 => {research_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 is_top?
|
|
self.is_top
|
|
end
|
|
|
|
def sorted_tags
|
|
#tags.order_by(I18n.locale, :asc)
|
|
tags
|
|
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
|
|
|
|
def save_research_files
|
|
self.research_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.tag_ids.delete('')
|
|
end
|
|
|
|
end |