personal-journal/app/models/journal_paper.rb

158 lines
4.9 KiB
Ruby
Raw Normal View History

2014-06-04 03:55:13 +00:00
class JournalPaper
include Mongoid::Document
include Mongoid::Timestamps
2014-06-12 10:27:26 +00:00
include Mongoid::Attributes::Dynamic
2014-06-04 03:55:13 +00:00
include OrbitModel::Status
2014-06-25 05:26:03 +00:00
include MemberHelper
include Slug
2014-06-04 03:55:13 +00:00
2014-06-25 05:26:03 +00:00
field :paper_title, as: :slug_title, type: String, localize: true
2014-06-04 03:55:13 +00:00
field :journal_title, type: String, localize: true
2014-08-11 11:16:52 +00:00
field :authors, type: String, localize: true
field :associated_project , type: String, localize: true
2014-06-04 03:55:13 +00:00
field :year, type: String
field :language, type: String
field :isbn, type: String
field :vol_no, type: String
field :issue_no, type: String
field :form_to_start, type: String
field :form_to_end, type: String
field :total_pages, type: String
field :keywords, type: String
field :abstract, type: String
2014-06-12 10:27:26 +00:00
field :publication_date, type: DateTime
2014-06-04 03:55:13 +00:00
field :url, type: String
field :note, type: String
2014-08-11 11:16:52 +00:00
field :number_of_authors
2014-07-15 05:52:03 +00:00
field :rss2_id
2014-06-04 03:55:13 +00:00
paginates_per 10
2014-06-12 10:27:26 +00:00
#relations
2014-06-04 03:55:13 +00:00
belongs_to :member_profile
2014-06-12 10:27:26 +00:00
belongs_to :journal_paper_type
has_and_belongs_to_many :journal_levels
has_many :journal_paper_files, autosave: true, dependent: :destroy
accepts_nested_attributes_for :journal_paper_files
2014-08-11 11:16:52 +00:00
# has_and_belongs_to_many :journal_paper_authors, dependent: :destroy
# accepts_nested_attributes_for :journal_paper_authors
2014-06-12 10:27:26 +00:00
has_and_belongs_to_many :journal_paper_author_types
2014-08-11 11:16:52 +00:00
# after_save :save_journal_paper_files, :save_journal_paper_authors
2014-06-18 04:09:19 +00:00
2014-06-12 10:27:26 +00:00
before_validation :add_http
2014-07-11 03:09:31 +00:00
# validates :paper_title, presence: true
2014-06-12 10:27:26 +00:00
def create_link
title = []
title << self.member_profile.name if self.member_profile_id.present?
2014-08-11 11:16:52 +00:00
title << (!self.authors.blank? ? self.authors : nil)
2014-06-12 10:27:26 +00:00
if !self.publication_date.nil?
pd = self.publication_date.strftime("%Y-%m-%d").split('-')
title << pd[0]
end
title << self.paper_title if self.paper_title.present?
title << self.journal_title if self.journal_title.present?
title << self.vol_no if (self.vol_no.present? && self.vol_no != "0")
title << self.issue_no if (self.issue_no.present? && self.issue_no != "0")
title << "pp"+self.form_to_start+"-"+self.form_to_end if (self.form_to_start.present? && self.form_to_start != "0")
title << ( !self.journal_levels.blank? ? "(#{self.journal_levels.collect{|x| x.title}.join(', ')})" : nil)
2014-07-30 06:58:17 +00:00
title.join(', ').gsub(', ,',',')
2014-06-12 10:27:26 +00:00
end
2014-08-11 11:16:52 +00:00
# def self.member_data
# members = MemberProfile.all
# member_data = []
# members.each do |m|
# if (m.first_name || m.last_name)
# member_data << {"memberId" => m.id.to_s, "memberName" => m.name}
# else
# member_data << {"memberId" => m.id.to_s, "memberName" => "No Name"}
# end
# end
# member_data.to_json
# end
# def save_journal_paper_files
# self.journal_paper_files.each do |t|
# if t.should_destroy
# t.destroy
# end
# end
# end
# def save_journal_paper_authors
# self.journal_paper_authors.each do |t|
# if t.should_destroy
# t.destroy
# end
# end
# end
2014-06-18 04:09:19 +00:00
2014-08-01 10:56:56 +00:00
def get_plugin_data(fields_to_show)
plugin_datas = []
fields_to_show.each do |field|
2014-10-03 06:15:35 +00:00
plugin_data = self.get_plugin_field_data(field) rescue nil
2014-08-01 10:56:56 +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
when "level_type"
value = self.journal_levels.collect{|level| level.title}.join(',') rescue ""
when "author_type"
value = self.journal_paper_author_types.collect{|type| type.title}.join(',') rescue ""
when "paper_type"
value = self.journal_paper_type.title rescue ""
2014-08-11 11:16:52 +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 10:56:56 +00:00
when "publication_date"
value = self.publication_date.to_date.strftime("%Y-%m-%d") rescue ""
when "language"
value = I18n.t(self.language) rescue ""
when "file"
files = []
self.journal_paper_files.each do |file|
url = file.journal_file.url
title = (file.title.blank? ? File.basename(file.journal_file.path) : file.title)
files << "<li><a href='#{url}'' target='_blank'>#{title}</li>"
end
value = files.join("")
else
value = self.send(field) rescue ""
end
{
"key"=>field,
"title_class"=>"journal-#{field.gsub('_','-')}-field",
"value_class"=>"journal-#{field.gsub('_','-')}-value",
"title"=>I18n.t('personal_journal.'+field),
"value"=>value
}
2014-06-25 05:26:03 +00:00
end
2014-06-12 10:27:26 +00:00
protected
def add_http
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
2014-06-04 03:55:13 +00:00
end