personal-diploma/app/models/diploma.rb

77 lines
1.8 KiB
Ruby

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
field :rss2_id
field :create_user_id, :type => BSON::ObjectId
field :update_user_id, :type => BSON::ObjectId
belongs_to :member_profile
paginates_per 10
before_validation :add_http
def duration
if !self.start_date.nil? or !self.end_date.nil?
(self.start_date.strftime('%Y.%m') rescue "")+' ~ '+(self.end_date.strftime('%Y.%m') rescue "")
else
""
end
end
def slug_title
[self.school_name,self.department,self.degree].join(' ')
end
def get_plugin_data(fields_to_show)
plugin_datas = []
fields_to_show.each do |field|
plugin_data = self.get_plugin_field_data(field)
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
protected
def add_http
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end