ruling_timer/app/models/ruling_timer_notify.rb

49 lines
1.8 KiB
Ruby

class RulingTimerNotify
include Mongoid::Document
include Mongoid::Timestamps
field :time_offset, type: String, default: "+8"
field :content, type: String, default: "" #store comment 、 file name 、 section name
field :type, type: Integer, default: 0 # 0 => comments, 1 => file, 2 => section(列表)
field :data # store section change or other data
field :is_hidden, type: Boolean, default: false
belongs_to :update_user, :class_name=>"User", :foreign_key => :update_user_id #更新提醒的使用者
belongs_to :user
belongs_to :ruling_timer_task
belongs_to :ruling_timer_comment
belongs_to :ruling_timer_file
before_create do
if self.update_user_id.nil?
self.update_user_id = self.ruling_timer_task.update_user_id rescue nil
end
end
def convert_datetime(time)
if time.class == Time
return time.to_datetime
elsif time.class == DateTime
return time
elsif time.class == String
return DateTime.parse(time)
else
return Time.at(time).to_datetime #time is seconds
end
end
def display_updated_at
update_time = convert_datetime(self.updated_at)
time_now = DateTime.now
formate_date1 = I18n.t("ruling_timer.formate_date1")
formate_date2 = I18n.t("ruling_timer.formate_date2")
formate_date3 = I18n.t("ruling_timer.formate_date3")
formate_date4 = I18n.t("ruling_timer.formate_date4")
diff = time_now.to_time - update_time.to_time
diff_time = Time.at(diff)
if diff < 3600
diff_time.strftime(formate_date4).gsub(/0+(\d)/){|f| $1}
elsif diff < 86400
diff_time.strftime(formate_date3)
elsif time_now.year == update_time.year
update_time.strftime(formate_date2).gsub(/am|pm/){|p| I18n.t("ruling_timer.#{p}")}
else
update_time.strftime(formate_date1).gsub(/am|pm/){|p| I18n.t("ruling_timer.#{p}")}
end
end
end