class OtherPaper include Mongoid::Document include Mongoid::Timestamps include OrbitModel::Status include MemberHelper include Admin::OtherPapersHelper include Slug field :title, :type => String, :default => "", :localize => true, as: :slug_title field :issue_date, :type => DateTime, :default => DateTime.now field :newsletter, :type => String, :default => "", :localize => true field :vol_no, :type => String, :default => "" field :isbn, :type => String, :default => "" field :url, :type => String, :default => "" field :keywords, :type => String, :default => "" field :note, :type => String, :default => "" field :authors, :type => String, :default => "", :localize => true belongs_to :other_paper_status field :rss2_id belongs_to :member_profile index({:issue_date=>-1, :id=>-1}, { unique: false, background: false }) scope :sort_hash, ->{ order_by({:issue_date=>-1, :id=>-1}) } scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by({:issue_date=>-1, :id=>-1}) } before_save do end def parse_time(time_str) DateTime.parse("0000-01-01 " + time_str) end def create_link title = '' title += get_authors_text(self, true, :en) if self.authors.present? || self.member_profile_id.present? title += ",\"#{self.title},\"" if self.title.present? title += " #{self.newsletter}" if self.newsletter.present? title += " #{self.vol_no}" if self.vol_no.present? title += ", #{self.issue_date.strftime('%b. %Y')}" if self.issue_date.present? title.sub(/^\s*,/,'').gsub(/,(\s*,)+/,',') end def self.get_plugin_datas_to_member(datas) page = Page.where(:module => "personal_other_paper").first rescue nil title_is_paper_format = true if !page.nil? && page.custom_string_field == "table" title_is_paper_format = false if !page.custom_array_field.blank? fields_to_show = page.custom_array_field else fields_to_show = ["title", "issue_date", "newsletter", "url"] end else fields_to_show = ["issue_date", "title"] end fields_to_remove = [] pd_title = [] fields_to_show.each do |t| if (self.fields[t].type.to_s == "String" || self.fields[t].type.to_s == "Object" rescue false) fields_to_remove << t if (datas.where(t.to_sym.ne => nil, t.to_sym.ne => "").count == 0 rescue false) elsif (self.relations.include?(t.pluralize) rescue false) fields_to_remove << t if (datas.where(t.pluralize.to_sym.ne=>[]).count == 0 rescue false) elsif [].include?(t) fields_to_remove << t if (datas.select{|d| d.send(t) != " ~ " }.count == 0 rescue false) else fields_to_remove << t if (datas.where(t.to_sym.ne => nil).count == 0 rescue false) end pd_title << { "plugin_data_title" => I18n.t("personal_other_paper.#{t}") } if !fields_to_remove.include?(t) end fields_to_show = fields_to_show - fields_to_remove plugin_datas = datas.sort_for_frontend.collect.with_index do |p,idx| pd_data = [] fields_to_show.collect do |t| pd_data << { "data_title" => p.display_field(t, false, title_is_paper_format) } end { "pd_datas" => pd_data, "type-sort" => (p.other_paper_status.sort_position.to_i rescue 1000), "sort-index" => idx } end plugin_datas = plugin_datas.sort_by{|pd| [pd["type-sort"], pd["sort-index"]]} 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) other_paper = self value = other_paper.send(field) rescue "" if field.include?(".") value = other_paper field.split(".").each{|f| value = value.send(f) rescue nil } end file_fields = [] link_fields = [] member_fields = [] if file_fields.include?(field) files = other_paper.send(field.pluralize) value = files.map do |file| url = file.file.url title = (file.title.blank? ? File.basename(file.file.path) : file.title) "