2012-09-13 10:13:34 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
class WritingBook
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
include Mongoid::MultiParameterAttributes
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
include OrbitModel::LanguageRestrict
|
|
|
|
include OrbitModel::Status
|
|
|
|
include OrbitTag::Taggable
|
2012-09-13 10:13:34 +00:00
|
|
|
|
|
|
|
LANGUAGE_TYPES = [ "English", "Chinese" ]
|
|
|
|
|
|
|
|
|
|
|
|
field :paper_title, localize: true
|
|
|
|
field :book_title, localize: true
|
|
|
|
field :authors, localize: true
|
|
|
|
field :extracted_chapters, localize: true
|
|
|
|
field :publisher, localize: true
|
|
|
|
field :editor, localize: true
|
|
|
|
|
2013-04-29 06:59:08 +00:00
|
|
|
# has_and_belongs_to_many :tags, :class_name => "PersonalBookTag"
|
2012-09-13 10:13:34 +00:00
|
|
|
|
|
|
|
has_and_belongs_to_many :book_author_types
|
|
|
|
|
|
|
|
belongs_to :book_paper_type
|
|
|
|
|
|
|
|
field :year
|
|
|
|
field :language
|
|
|
|
field :publish_date , :type => Date
|
|
|
|
field :pages
|
|
|
|
field :isbn
|
|
|
|
field :keywords
|
|
|
|
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
|
2013-09-18 08:10:39 +00:00
|
|
|
|
|
|
|
paginates_per 10
|
2012-09-13 10:13:34 +00:00
|
|
|
|
|
|
|
has_many :writing_book_files, :autosave => true, :dependent => :destroy
|
|
|
|
|
|
|
|
accepts_nested_attributes_for :writing_book_files, :allow_destroy => true
|
|
|
|
|
2013-07-02 08:46:44 +00:00
|
|
|
# before_save :clean_checkboxs
|
2012-09-13 10:13:34 +00:00
|
|
|
|
|
|
|
validates :paper_title, :at_least_one => true
|
|
|
|
|
|
|
|
before_validation :add_http
|
|
|
|
|
|
|
|
after_save :save_writing_book_files
|
|
|
|
|
2013-02-05 10:22:23 +00:00
|
|
|
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-09-13 10:13:34 +00:00
|
|
|
|
|
|
|
def self.search( category_id = nil )
|
|
|
|
|
|
|
|
if category_id.to_s.size > 0
|
|
|
|
|
|
|
|
find(:all, :conditions => {writing_book_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_writing_book_files
|
|
|
|
self.writing_book_files.each do |t|
|
|
|
|
if t.should_destroy
|
|
|
|
t.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_link
|
2013-10-09 05:22:04 +00:00
|
|
|
title = []
|
2013-11-21 03:57:23 +00:00
|
|
|
title << self.authors if self.authors.present?
|
|
|
|
|
|
|
|
# title << self.paper_title if self.paper_title.present?
|
2013-10-09 05:22:04 +00:00
|
|
|
title << self.book_title if self.book_title.present?
|
|
|
|
title << self.publisher if self.publisher.present?
|
2013-11-21 03:57:23 +00:00
|
|
|
title << self.isbn if self.isbn.present?
|
|
|
|
|
|
|
|
pd = self.publish_date.strftime("%Y-%m-%d").split('-')
|
|
|
|
|
|
|
|
title << pd[0]+"/"+pd[1]
|
|
|
|
|
2013-10-09 05:22:04 +00:00
|
|
|
# title << "(#{self.journal_level_types.collect{|x| x.title}.join(', ')})"
|
|
|
|
title.join(', ')
|
2012-09-13 10:13:34 +00:00
|
|
|
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('')
|
|
|
|
self.book_author_type_ids.delete('')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|