97 lines
2.3 KiB
Ruby
97 lines
2.3 KiB
Ruby
class Book
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
include Mongoid::Attributes::Dynamic
|
|
|
|
include OrbitModel::Status
|
|
include MemberHelper
|
|
include Slug
|
|
|
|
field :book_title, as: :slug_title, type: String, localize: true
|
|
field :extracted_chapters, type: String, localize: true
|
|
field :publisher, type: String, localize: true
|
|
field :editor, type: String, localize: true
|
|
field :year, type: String
|
|
field :language, type: String
|
|
field :publish_date, type: DateTime
|
|
field :pages, type: String
|
|
field :isbn, type: String
|
|
field :keywords, type: String
|
|
field :publication_date, type: DateTime
|
|
field :url, type: String
|
|
field :note, type: String
|
|
|
|
paginates_per 10
|
|
|
|
belongs_to :member_profile
|
|
|
|
has_many :book_files, autosave: true, dependent: :destroy
|
|
accepts_nested_attributes_for :book_files
|
|
|
|
has_and_belongs_to_many :book_authors
|
|
accepts_nested_attributes_for :book_authors
|
|
|
|
has_and_belongs_to_many :book_author_types
|
|
|
|
belongs_to :book_type
|
|
before_validation :add_http
|
|
|
|
after_save :save_book_files, :save_book_authors
|
|
|
|
|
|
def create_link
|
|
title = []
|
|
title << "#{self.book_authors.collect{|j| j.name}.join(', ')}" if self.book_authors.present?
|
|
title << self.book_title if self.book_title.present?
|
|
title << self.publisher if self.publisher.present?
|
|
title << self.isbn if self.isbn.present?
|
|
|
|
if !self.publish_date.nil?
|
|
pd = self.publish_date.strftime("%Y-%m-%d").split('-')
|
|
title << pd[0]+"/"+pd[1]
|
|
end
|
|
|
|
title.join(', ')
|
|
end
|
|
|
|
def self.member_data
|
|
members = MemberProfile.all
|
|
member_data = []
|
|
members.each do |m|
|
|
member_data << {"memberId" => m.id.to_s, "memberName" => m.name}
|
|
end
|
|
member_data.to_json
|
|
end
|
|
|
|
def save_book_files
|
|
self.book_files.each do |t|
|
|
if t.should_destroy
|
|
t.destroy
|
|
end
|
|
end
|
|
end
|
|
|
|
def save_book_authors
|
|
self.book_authors.each do |t|
|
|
if t.should_destroy
|
|
t.destroy
|
|
end
|
|
end
|
|
end
|
|
|
|
def authors
|
|
authors = []
|
|
authors << self.member_profile.name if self.member_profile_id.present?
|
|
authors << (!self.book_authors.blank? ? "#{self.book_authors.collect{|j| j.name}.join(', ')}" : nil)
|
|
authors.join(', ')
|
|
end
|
|
|
|
protected
|
|
|
|
def add_http
|
|
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
|
|
self.url = 'http://' + self.url
|
|
end
|
|
end
|
|
end
|