2017-03-22 11:12:02 +08:00
|
|
|
class TopicStatusUpdate < ActiveRecord::Base
|
|
|
|
include Trashable
|
2013-05-28 09:00:53 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
2013-05-28 09:00:53 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :topic_id, presence: true
|
|
|
|
validates :execute_at, presence: true
|
|
|
|
validates :status_type, presence: true
|
|
|
|
validates :status_type, uniqueness: { scope: [:topic_id, :deleted_at] }
|
|
|
|
validate :ensure_update_will_happen
|
|
|
|
|
|
|
|
before_save do
|
|
|
|
self.created_at ||= Time.zone.now if execute_at
|
|
|
|
|
|
|
|
if (execute_at_changed? && !execute_at_was.nil?) || user_id_changed?
|
|
|
|
self.send("cancel_auto_#{self.class.types[status_type]}_job")
|
2016-05-25 02:27:48 +08:00
|
|
|
end
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
after_save do
|
2017-04-03 17:28:41 +08:00
|
|
|
if (execute_at_changed? || user_id_changed?) && topic
|
2017-03-22 11:12:02 +08:00
|
|
|
now = Time.zone.now
|
|
|
|
time = execute_at < now ? now : execute_at
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
self.send("schedule_auto_#{self.class.types[status_type]}_job", time)
|
2013-07-04 09:47:12 +08:00
|
|
|
end
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
def self.types
|
|
|
|
@types ||= Enum.new(
|
|
|
|
close: 1,
|
2017-04-03 17:28:41 +08:00
|
|
|
open: 2,
|
|
|
|
publish_to_category: 3
|
2017-03-22 11:12:02 +08:00
|
|
|
)
|
2014-10-30 04:26:32 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
def self.ensure_consistency!
|
|
|
|
TopicStatusUpdate.where("execute_at < ?", Time.zone.now).find_each do |topic_status_update|
|
|
|
|
topic_status_update.send(
|
|
|
|
"schedule_auto_#{self.types[topic_status_update.status_type]}_job",
|
|
|
|
topic_status_update.execute_at
|
|
|
|
)
|
2013-06-07 05:04:10 +08:00
|
|
|
end
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
def duration
|
|
|
|
if (self.execute_at && self.created_at)
|
|
|
|
((self.execute_at - self.created_at) / 1.hour).round(2)
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
private
|
2013-05-28 09:00:53 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
def ensure_update_will_happen
|
|
|
|
if created_at && (execute_at < created_at)
|
|
|
|
errors.add(:execute_at, I18n.t(
|
|
|
|
'activerecord.errors.models.topic_status_update.attributes.execute_at.in_the_past'
|
|
|
|
))
|
|
|
|
end
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
def cancel_auto_close_job
|
|
|
|
Jobs.cancel_scheduled_job(:toggle_topic_closed, topic_status_update_id: id)
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
2017-03-22 11:12:02 +08:00
|
|
|
alias_method :cancel_auto_open_job, :cancel_auto_close_job
|
2013-05-28 09:00:53 +08:00
|
|
|
|
2017-04-03 17:28:41 +08:00
|
|
|
def cancel_auto_publish_to_category_job
|
|
|
|
Jobs.cancel_scheduled_job(:publish_topic_to_category, topic_status_update_id: id)
|
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
def schedule_auto_open_job(time)
|
2017-04-03 17:28:41 +08:00
|
|
|
topic.update_status('closed', true, user) if !topic.closed
|
2015-07-25 04:39:03 +08:00
|
|
|
|
2017-04-03 17:28:41 +08:00
|
|
|
Jobs.enqueue_at(time, :toggle_topic_closed,
|
|
|
|
topic_status_update_id: id,
|
|
|
|
state: false
|
|
|
|
)
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
def schedule_auto_close_job(time)
|
2017-04-03 17:28:41 +08:00
|
|
|
topic.update_status('closed', false, user) if topic.closed
|
2013-07-04 04:44:45 +08:00
|
|
|
|
2017-04-03 17:28:41 +08:00
|
|
|
Jobs.enqueue_at(time, :toggle_topic_closed,
|
|
|
|
topic_status_update_id: id,
|
|
|
|
state: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def schedule_auto_publish_to_category_job(time)
|
|
|
|
Jobs.enqueue_at(time, :publish_topic_to_category, topic_status_update_id: id)
|
2013-07-04 04:44:45 +08:00
|
|
|
end
|
2013-05-28 09:00:53 +08:00
|
|
|
end
|
2017-03-22 11:12:02 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: topic_status_updates
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# execute_at :datetime not null
|
|
|
|
# status_type :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# topic_id :integer not null
|
|
|
|
# based_on_last_post :boolean default(FALSE), not null
|
|
|
|
# deleted_at :datetime
|
|
|
|
# deleted_by_id :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2017-04-03 17:28:41 +08:00
|
|
|
# category_id :integer
|
2017-03-22 11:12:02 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# idx_topic_id_status_type_deleted_at (topic_id,status_type) UNIQUE
|
|
|
|
# index_topic_status_updates_on_user_id (user_id)
|
|
|
|
#
|