Auto-close time can be entered in 3 ways, so a topic can close at any time

This commit is contained in:
Neil Lalonde
2013-11-26 19:06:20 -05:00
parent 67b6d37da0
commit a9ab98ef9e
19 changed files with 193 additions and 80 deletions

View File

@ -146,7 +146,7 @@ class Topic < ActiveRecord::Base
self.bumped_at ||= Time.now
self.last_post_user_id ||= user_id
if !@ignore_category_auto_close and self.category and self.category.auto_close_days and self.auto_close_at.nil?
set_auto_close(self.category.auto_close_days)
set_auto_close(self.category.auto_close_days * 24)
end
end
@ -602,9 +602,10 @@ class Topic < ActiveRecord::Base
end
end
# TODO: change this method, along with category's auto_close_days. Use hours.
def auto_close_days=(num_days)
@ignore_category_auto_close = true
set_auto_close(num_days)
set_auto_close(num_days * 24)
end
def self.auto_close
@ -622,10 +623,27 @@ class Topic < ActiveRecord::Base
end
end
def set_auto_close(num_days, by_user=nil)
num_days = num_days.to_i
self.auto_close_at = (num_days > 0 ? num_days.days.from_now : nil)
if num_days > 0
# Valid arguments for the auto close time:
# * An integer, which is the number of hours from now to close the topic.
# * A time, like "12:00", which is the time at which the topic will close in the current day
# or the next day if that time has already passed today.
# * A timestamp, like "2013-11-25 13:00", when the topic should close.
# * A timestamp with timezone in JSON format. (e.g., "2013-11-26T21:00:00.000Z")
# * nil, to prevent the topic from automatically closing.
def set_auto_close(arg, by_user=nil)
if arg.is_a?(String) and matches = /^([\d]{1,2}):([\d]{1,2})$/.match(arg.strip)
now = Time.zone.now
self.auto_close_at = Time.zone.local(now.year, now.month, now.day, matches[1].to_i, matches[2].to_i)
self.auto_close_at += 1.day if self.auto_close_at < now
elsif arg.is_a?(String) and arg.include?('-') and timestamp = Time.zone.parse(arg)
self.auto_close_at = timestamp
self.errors.add(:auto_close_at, :invalid) if timestamp < Time.zone.now
else
num_hours = arg.to_i
self.auto_close_at = (num_hours > 0 ? num_hours.hours.from_now : nil)
end
unless self.auto_close_at.nil?
self.auto_close_started_at ||= Time.zone.now
if by_user and by_user.staff?
self.auto_close_user = by_user