class Book include Mongoid::Document include Mongoid::Timestamps include Mongoid::Attributes::Dynamic include Admin::PersonalBooksHelper include OrbitModel::Status include MemberHelper include Slug HideTimeInLinK = (File.exist?("#{Rails.root}/hide_time_fields_for_plugins.txt") rescue false) 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 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 field :create_user_id, :type => BSON::ObjectId field :update_user_id, :type => BSON::ObjectId field :rss2_id, type: String field :number_of_authors # paginates_per 10 belongs_to :member_profile belongs_to :book_type has_many :book_files, autosave: true, dependent: :destroy accepts_nested_attributes_for :book_files, :allow_destroy => true has_and_belongs_to_many :book_author_types before_validation :add_http index({year: -1, :publish_date => -1, :publication_date => -1, _id: -1}, { unique: false, background: false }) scope :sort_year_date, ->{ order_by(:year => "desc", :publish_date => "desc", :publication_date => "desc", :id => "desc") } scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publish_date => "desc", :publication_date => "desc", :id => "desc") } def book_paper_type self.book_type.title rescue "" end def create_link title = [] # title << self.member_profile.name if self.member_profile.present? title << self.authors if self.authors.present? title << self.book_title if self.book_title.present? title << self.extracted_chapters if self.extracted_chapters.present? title << self.publisher if self.publisher.present? title << self.isbn if self.isbn.present? title << self.pages if self.pages.present? unless HideTimeInLinK if self.publish_date title << (self.publish_date.strftime("%b. %Y") rescue nil) elsif self.publication_date title << (self.publication_date.strftime("%b. %Y") rescue nil) elsif self.year.present? title << self.year end end title.join(', ') end def self.get_plugin_datas_to_member(datas) page = Page.where(:module => "personal_book").first rescue nil if !page.nil? && page.custom_string_field == "table" 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 else fields_to_show = [ "year", "book_title" ] end fields_to_remove = [] pd_title = [] fields_to_show.each do |t| if (self.fields[t].type.to_s == "String" rescue false) fields_to_remove << t if (datas.where(t.to_sym.ne => nil).or(t.to_sym.ne => "").count == 0 rescue false) else fields_to_remove << t if (datas.where(t.to_sym.ne => "").count == 0 rescue false) end pd_title << { "plugin_data_title" => I18n.t("personal_book.#{t}") } if !fields_to_remove.include?(t) end fields_to_show = fields_to_show - fields_to_remove plugin_datas = datas.sort_for_frontend.collect do |p| pd_data = [] fields_to_show.collect do |t| if !page.nil? && page.custom_string_field == "table" case t when "book_title", "extracted_chapters" link = OrbitHelper.url_to_plugin_show(p.to_param,'personal_book') url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank) tmp_title = p.send(t) pd_data << { "data_title" => (url_to_plugin_show_blank ? tmp_title : "#{tmp_title}") } when "publish_date", "publication_date" pd = "" if !p.publication_date.nil? pd = p.publication_date.strftime("%Y-%m-%d").split('-') 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" pd_data << {"data_title" => (I18n.t("#{p.language}") if !p.language.nil? rescue "")} else pd_data << { "data_title" => p.send(t) } end else if t == "book_title" link = OrbitHelper.url_to_plugin_show(p.to_param,'personal_book') url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank) tmp_title = p.create_link pd_data << { "data_title" => (url_to_plugin_show_blank ? tmp_title : "#{tmp_title}") } else pd_data << { "data_title" => p.send(t) } end end end { "pd_datas" => pd_data } end return [pd_title,plugin_datas] end def get_plugin_data(fields_to_show) plugin_datas = [] fields_to_show.each do |field| plugin_data = self.get_plugin_field_data(field) rescue nil 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 when "book_paper_type" value = (self.book_type.title rescue "") when "author_type" value = (self.book_author_types.collect{|type| type.title}.join(',') rescue "") when "author_name" value = [] name_translations = self.member_profile.name_translations rescue {} ([I18n.locale]+(Site.first.in_use_locales-[I18n.locale])).each do |locale| if name_translations[locale] value << name_translations[locale] end end value = value.join(" / ") when "language" value = I18n.t("#{self.language}") if !self.language.nil? rescue "" when "publish_date", "publication_date" value = self.send(field).to_date.strftime("%Y-%m-%d") rescue nil when "file" files = [] self.book_files.each do |book_file| url = book_file.member_book_file.url title = ((book_file.title.blank? ? File.basename(book_file.member_book_file.path) : book_file.title) rescue "") files << (url.nil? ? "" : "