personal-book/app/models/book.rb

201 lines
6.4 KiB
Ruby
Raw Normal View History

class Book
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic
include OrbitModel::Status
include MemberHelper
include Slug
2016-02-15 12:01:50 +00:00
field :book_title, as: :slug_title, localize: true
field :extracted_chapters, type: String, localize: true
field :publisher, type: String, localize: true
field :editor, type: String, localize: true
2014-08-11 11:18:35 +00:00
field :authors, 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
2014-12-05 06:37:24 +00:00
field :create_user_id, :type => BSON::ObjectId
field :update_user_id, :type => BSON::ObjectId
field :rss2_id, type: String
2014-08-11 11:18:35 +00:00
field :number_of_authors
2014-12-05 06:37:24 +00:00
2017-01-11 06:27:39 +00:00
# paginates_per 10
belongs_to :member_profile
2015-01-09 07:38:49 +00:00
belongs_to :book_type
has_many :book_files, autosave: true, dependent: :destroy
2014-08-11 11:18:35 +00:00
accepts_nested_attributes_for :book_files, :allow_destroy => true
2014-07-03 14:34:44 +00:00
has_and_belongs_to_many :book_author_types
before_validation :add_http
2016-04-11 09:53:23 +00:00
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publish_date => "desc", :publication_date => "desc") }
2015-12-02 10:26:24 +00:00
def create_link
title = []
2015-05-08 08:21:45 +00:00
# title << self.member_profile.name if self.member_profile.present?
2014-08-11 11:18:35 +00:00
title << self.authors if self.authors.present?
title << self.book_title if self.book_title.present?
2015-01-09 07:38:49 +00:00
title << self.extracted_chapters if self.extracted_chapters.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
2015-01-09 07:38:49 +00:00
title << self.pages if self.pages.present?
2016-01-11 12:34:30 +00:00
# title << self.year
2015-01-09 07:38:49 +00:00
2014-12-05 06:37:24 +00:00
title.join(', ')
end
2016-01-11 12:34:30 +00:00
def self.get_plugin_datas_to_member(datas)
2016-03-22 10:06:03 +00:00
page = Page.where(:module => "personal_book").first rescue nil
2016-03-24 17:33:29 +00:00
if !page.nil? && page.custom_string_field == "table"
2016-04-01 05:56:20 +00:00
if !page.custom_array_field.blank?
fields_to_show = page.custom_array_field
else
fields_to_show = [
"authors",
"book_title",
"extracted_chapters",
"publisher",
"isbn",
"publish_date"
]
end
2016-03-22 10:06:03 +00:00
else
fields_to_show = [
"year",
"book_title"
]
end
2016-01-11 12:34:30 +00:00
2016-02-15 11:06:48 +00:00
fields_to_remove = []
pd_title = []
fields_to_show.each do |t|
if (self.fields[t].type.to_s == "String" rescue false)
2016-02-15 12:01:50 +00:00
fields_to_remove << t if (datas.where(t.to_sym.ne => nil).or(t.to_sym.ne => "").count == 0 rescue false)
2016-02-15 11:06:48 +00:00
else
2016-02-15 12:05:30 +00:00
fields_to_remove << t if (datas.where(t.to_sym.ne => "").count == 0 rescue false)
2016-02-15 11:06:48 +00:00
end
pd_title << {
"plugin_data_title" => I18n.t("personal_book.#{t}")
} if !fields_to_remove.include?(t)
2016-01-11 12:34:30 +00:00
end
2016-02-15 11:06:48 +00:00
fields_to_show = fields_to_show - fields_to_remove
2016-01-11 12:34:30 +00:00
plugin_datas = datas.sort_for_frontend.collect do |p|
pd_data = []
fields_to_show.collect do |t|
2016-03-24 17:36:23 +00:00
if !page.nil? && page.custom_string_field == "table"
2016-03-22 10:06:03 +00:00
case t
2016-04-05 18:06:34 +00:00
when "book_title", "extracted_chapters"
pd_data << { "data_title" => "<a href='#{OrbitHelper.url_to_plugin_show(p.to_param,'personal_book')}' target='_blank'>#{p.send(t)}</a>" }
2016-04-06 05:08:32 +00:00
when "publish_date", "publication_date"
2016-03-22 10:06:03 +00:00
pd = ""
2016-04-06 05:12:20 +00:00
if !p.publication_date.nil?
pd = p.publication_date.strftime("%Y-%m-%d").split('-')
2016-03-22 10:06:03 +00:00
pd = pd[0]+"/"+pd[1]
end
pd_data << {"data_title" => pd}
when "author_type"
pd_data << {"data_title" => (p.book_author_types.collect{|bat| bat.title}.join(", ") rescue "")}
when "language"
2016-04-13 08:22:06 +00:00
pd_data << {"data_title" => (I18n.t("personal_book.#{p.language}") if !p.language.nil? rescue "")}
2016-03-22 10:06:03 +00:00
else
pd_data << { "data_title" => p.send(t) }
end
2016-01-11 12:34:30 +00:00
else
2016-03-22 10:06:03 +00:00
if t == "book_title"
pd_data << { "data_title" => "<a href='#{OrbitHelper.url_to_plugin_show(p.to_param,'personal_book')}' target='_blank'>#{p.create_link}</a>" }
else
pd_data << { "data_title" => p.send(t) }
end
2016-01-11 12:34:30 +00:00
end
end
{
"pd_datas" => pd_data
}
end
return [pd_title,plugin_datas]
end
2014-08-01 11:49:55 +00:00
def get_plugin_data(fields_to_show)
plugin_datas = []
fields_to_show.each do |field|
2014-10-03 06:11:11 +00:00
plugin_data = self.get_plugin_field_data(field) rescue nil
2014-08-01 11:49:55 +00:00
next if plugin_data.blank? or plugin_data['value'].blank?
plugin_datas << plugin_data
end
plugin_datas
end
def get_plugin_field_data(field)
case field
2015-01-09 07:38:49 +00:00
when "book_paper_type"
value = self.book_type.title rescue ""
when "author_type"
value = self.book_author_types.collect{|type| type.title}.join(',') rescue ""
2014-08-11 11:18:35 +00:00
when "author_name"
value = []
([I18n.locale]+(Site.first.in_use_locales-[I18n.locale])).each do |locale|
if self.member_profile.first_name_translations[locale] || self.member_profile.last_name_translations[locale]
value << "#{self.member_profile.first_name_translations[locale]} #{self.member_profile.last_name_translations[locale]}"
end
end
value = value.join(" / ")
2014-08-01 11:49:55 +00:00
when "language"
2016-04-13 08:22:06 +00:00
value = I18n.t("personal_book.#{self.language}") if !self.language.nil? rescue ""
2014-08-01 11:49:55 +00:00
when "publish_date", "publication_date"
value = self.send(field).to_date.strftime("%Y-%m-%d") rescue nil
when "file"
files = []
2014-12-05 06:37:24 +00:00
self.book_files.each do |book_file|
2014-08-01 11:49:55 +00:00
url = book_file.member_book_file.url
title = (book_file.title.blank? ? File.basename(book_file.member_book_file.path) : book_file.title)
files << "<li><a href='#{url}'' target='_blank'>#{title}</li>"
end
value = files.join("")
else
value = self.send(field) rescue ""
end
value = (value =~ /\A#{URI::regexp(['http', 'https'])}\z/) ? "<a href='#{value}' target='blank'>#{value}</a>" : value
2014-12-05 06:37:24 +00:00
2014-08-01 11:49:55 +00:00
{
"key"=>field,
"title_class"=>"book-#{field.gsub('_','-')}-field",
"value_class"=>"book-#{field.gsub('_','-')}-value",
"title"=>I18n.t('personal_book.'+field),
"value"=>value
}
end
protected
def add_http
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end