asia_database/app/models/asia_teacher.rb

152 lines
5.3 KiB
Ruby
Raw Normal View History

2023-05-26 14:17:05 +00:00
class AsiaTeacher
include Mongoid::Document
include Mongoid::Timestamps
include Slug
field :email_id # for syncing
field :ukey, :type => String
field :academy, :type => String, :default => "", :localize => true
field :department, :type => String, :default => "", :localize => true
field :teacher, :type => String, :default => "", :localize => true, :as => :slug_title
field :discipline_expertise, :type => Array, :default => [], :localize => true
field :authors, :type => String, :default => "", :localize => true
belongs_to :asia_academy, index: true
belongs_to :asia_department, index: true
has_many :asia_projects
has_many :asia_papers
has_many :asia_patents
has_many :asia_tec_transfers
has_many :asia_exhibitions
scope :sort_order, ->{order({ukey: 1})}
index({ukey: 1}, { unique: false, background: true })
before_save do
if self.asia_academy_id_changed?
self.academy_translations = self.asia_academy.academy_name_translations rescue ""
end
if self.asia_department_id_changed?
self.department_translations = self.asia_department.department_name_translations rescue ""
end
AsiaExportStore.destroy_all
end
def url
"https://webap.asia.edu.tw/TchEportfolio/#{self.email_id}"
end
def parse_time(time_str)
DateTime.parse("0000-01-01 " + time_str)
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)
asia_teacher = self
value = asia_teacher.send(field) rescue ""
if field.include?(".")
value = asia_teacher
field.split(".").each{|f| value = value.send(f) rescue nil }
end
file_fields = []
link_fields = []
member_fields = []
if file_fields.include?(field)
files = asia_teacher.send(field.pluralize)
value = files.map do |file|
url = file.file.url
title = (file.title.blank? ? File.basename(file.file.path) : file.title)
"<li><a href='#{url}'' target='_blank'>#{title}</li>"
end
value = value.join("")
elsif link_fields.include?(field)
links = asia_teacher.send(field.pluralize)
value = links.map do |link|
url = link.url
title = (link.title.blank? ? url : link.title)
"<li><a href='#{url}'' target='_blank'>#{title}</li>"
end
value = value.join("")
elsif member_fields.include?(field)
members = asia_teacher.send(field.pluralize)
value = members.map{|m|
path = OrbitHelper.url_to_plugin_show(m.to_param, 'member') rescue '#'
((text_only rescue false) ? m.name : "<a href='#{path}'>#{m.name}</a>")
}
join_text = (text_only rescue false) ? "," : "<br>"
value = value.join(join_text)
elsif field == "member_profile" || field == "authors"
value = get_authors_show(asia_teacher)
end
strftime_hash = {}
if strftime_hash.keys.include?(field)
value = value.strftime(strftime_hash[field]) rescue value
end
value
value = (value =~ /\A#{URI::regexp(['http', 'https'])}\z/) ? "<a href='#{value}' target='blank'>#{value}</a>" : value
{
"key"=>field,
"title_class"=>"asia_teacher-#{field.gsub('_','-')}-field",
"value_class"=>"asia_teacher-#{field.gsub('_','-')}-value",
"title"=>I18n.t('asia_database.'+field),
"value"=>value
}
end
def display_field(field,text_only=false,title_is_paper_format=false)
asia_teacher = self
value = asia_teacher.send(field) rescue ""
if field.include?(".")
value = asia_teacher
field.split(".").each{|f| value = value.send(f) rescue nil }
end
file_fields = []
link_fields = []
member_fields = []
if file_fields.include?(field)
files = asia_teacher.send(field.pluralize)
value = files.map do |file|
url = file.file.url
title = (file.title.blank? ? File.basename(file.file.path) : file.title)
"<li><a href='#{url}'' target='_blank'>#{title}</li>"
end
value = value.join("")
elsif link_fields.include?(field)
links = asia_teacher.send(field.pluralize)
value = links.map do |link|
url = link.url
title = (link.title.blank? ? url : link.title)
"<li><a href='#{url}'' target='_blank'>#{title}</li>"
end
value = value.join("")
elsif member_fields.include?(field)
members = asia_teacher.send(field.pluralize)
value = members.map{|m|
path = OrbitHelper.url_to_plugin_show(m.to_param, 'member') rescue '#'
((text_only rescue false) ? m.name : "<a href='#{path}'>#{m.name}</a>")
}
join_text = (text_only rescue false) ? "," : "<br>"
value = value.join(join_text)
elsif field == "member_profile" || field == "authors"
value = get_authors_show(asia_teacher)
end
strftime_hash = {}
if strftime_hash.keys.include?(field)
value = value.strftime(strftime_hash[field]) rescue value
end
if field == "teacher"
link = OrbitHelper.url_to_plugin_show(asia_teacher.to_param,'asia_database')
tmp_title = value
url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank)
value = url_to_plugin_show_blank ? tmp_title : "<a href='#{link}' target='_blank' title='#{tmp_title}'>#{tmp_title}</a>"
end
value
end
end