personal-diploma/app/models/diploma.rb

138 lines
3.8 KiB
Ruby
Raw Normal View History

2014-07-02 03:54:14 +00:00
class Diploma
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include Slug
field :school_name, localize: true
field :country, localize: true
field :department, localize: true
field :degree, localize: true
field :language
field :keywords
field :start_date , :type => Date
field :end_date , :type => Date
field :url
field :note
2014-07-14 13:27:59 +00:00
field :rss2_id
2014-07-02 03:54:14 +00:00
field :create_user_id, :type => BSON::ObjectId
field :update_user_id, :type => BSON::ObjectId
belongs_to :member_profile
2017-01-11 06:34:21 +00:00
# paginates_per 10
2014-07-02 03:54:14 +00:00
before_validation :add_http
2015-12-02 10:28:23 +00:00
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:end_date => "desc", :start_date => "desc") }
2014-07-02 03:54:14 +00:00
def duration
2014-07-15 07:34:51 +00:00
if !self.start_date.nil? or !self.end_date.nil?
2015-01-09 07:42:05 +00:00
(self.start_date.strftime('%Y.%m') rescue "")+' ~ '+(self.end_date.strftime('%Y.%m') rescue I18n.t('personal_diploma.up_to_today'))
2014-07-15 07:34:51 +00:00
else
""
end
2014-07-02 03:54:14 +00:00
end
def slug_title
2014-07-30 06:20:29 +00:00
[self.school_name,self.department,self.degree].join(' ')
2014-07-02 03:54:14 +00:00
end
2015-01-09 07:42:05 +00:00
def self.get_plugin_datas_to_member(datas)
2017-03-06 12:55:45 +00:00
page = Page.where(:module => "personal_honor").first rescue nil
if !page.nil? && !page.custom_array_field.blank?
fields_to_show = page.custom_array_field
else
fields_to_show = [
"country",
"school_name",
"department",
"degree",
"duration"
]
end
2016-01-13 10:44:28 +00:00
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)
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_diploma.#{t}")
} if !fields_to_remove.include?(t)
end
fields_to_show = fields_to_show - fields_to_remove
2015-01-09 07:42:05 +00:00
2015-12-02 10:28:23 +00:00
plugin_datas = datas.sort_for_frontend.collect do |p|
2015-01-09 07:42:05 +00:00
pd_data = []
fields_to_show.collect do |t|
if t == "school_name"
2016-01-11 12:37:03 +00:00
pd_data << { "data_title" => "<a href='#{OrbitHelper.url_to_plugin_show(p.to_param,'personal_diploma')}' target='_blank'>#{p.send(t)}</a>" }
2015-01-09 07:42:05 +00:00
elsif t == "duration"
if !p.send('start_date').nil? or !p.send('end_date').nil?
date = (p.send('start_date').strftime('%Y.%m') rescue "")+' ~ '+(p.send('end_date').strftime('%Y.%m') rescue I18n.t('personal_diploma.up_to_today'))
else
date = ""
end
pd_data << { "data_title" => date }
else
pd_data << { "data_title" => p.send(t) }
end
end
{
"pd_datas" => pd_data
}
end
return [pd_title,plugin_datas]
end
2014-08-01 11:46:28 +00:00
def get_plugin_data(fields_to_show)
plugin_datas = []
fields_to_show.each do |field|
2014-10-03 06:12:35 +00:00
plugin_data = self.get_plugin_field_data(field) rescue nil
2014-08-01 11:46:28 +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 "language"
value = I18n.t(self.language) rescue ""
else
value = self.send(field) rescue ""
end
value = (value =~ /\A#{URI::regexp(['http', 'https'])}\z/) ? "<a href='#{value}' target='blank'>#{value}</a>" : value
{
"key"=>field,
"title_class"=>"diploma-#{field.gsub('_','-')}-field",
"value_class"=>"diploma-#{field.gsub('_','-')}-value",
"title"=>I18n.t('personal_diploma.'+field),
"value"=>value
}
end
2014-07-02 03:54:14 +00:00
protected
def add_http
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end