class RulingTimerComment include Mongoid::Document include Mongoid::Timestamps field :content, type: String, default: "" belongs_to :uploader, :class_name=>"User", :foreign_key => :uploader_id #上傳者 belongs_to :ruling_timer_task has_many :ruling_timer_notifies, :autosave => true, :dependent => :destroy before_create do self.uploader_id = self.ruling_timer_task.update_user_id rescue nil if self.uploader_id.nil? end after_save do all_user_ids = self.ruling_timer_task.get_all_user_ids rescue [] ruling_timer_temps = RulingTimerTemp.where(:user_id.in=>all_user_ids).to_a ruling_timer_temps.each do |temp| notify = self.ruling_timer_notifies.where(:user=>temp.user).first if notify.nil? notify = self.ruling_timer_notifies.new(:type=>0,:user=>temp.user) end notify.is_hidden = false notify.ruling_timer_task = self.ruling_timer_task notify.update_user_id = self.uploader_id notify.content = self.content notify.save 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.to_i - update_time.to_time.to_i diff_time = Time.at(diff).utc 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