ruling_timer/app/models/ruling_timer_file.rb

60 lines
2.2 KiB
Ruby

class RulingTimerFile
include Mongoid::Document
include Mongoid::Timestamps
mount_uploader :file, AssetUploader
field :time_offset, type: String, default: "+8"
field :description, 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
end
after_save do
if self[:file].present?
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=>1,: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 = "<a href=\"#{self.file.url}\" title=\"#{self[:file]}\">#{self[:file]}</a>"
notify.save
end
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