FEATURE: better handling of dates with no time

This commit is contained in:
Joffrey JAFFEUX 2018-06-11 15:35:10 +02:00
parent 7aeeff4e18
commit 30099a14bc
3 changed files with 36 additions and 14 deletions

View File

@ -13,11 +13,20 @@ module DiscourseSimpleCalendar
end
first_date = dates[0]
from = Time.strptime("#{first_date["date"]} #{first_date["time"]} UTC", "%Y-%m-%d %H:%M %Z")
if first_date['time']
from = Time.strptime("#{first_date['date']} #{first_date['time']} UTC", "%Y-%m-%d %H:%M %Z")
else
from = Time.strptime("#{first_date['date']} UTC", "%Y-%m-%d %Z").beginning_of_day
end
if dates.count == 2
second_date = dates[1]
to = Time.strptime("#{second_date["date"]} #{second_date["time"]} UTC", "%Y-%m-%d %H:%M %Z")
if second_date['time']
to = Time.strptime("#{second_date['date']} #{second_date['time']} UTC", "%Y-%m-%d %H:%M %Z")
else
to = Time.strptime("#{second_date['date']} UTC", "%Y-%m-%d %Z").end_of_day
end
end
post_number = post.post_number.to_s
@ -39,17 +48,6 @@ module DiscourseSimpleCalendar
op.custom_fields[DiscourseSimpleCalendar::CALENDAR_DETAILS_CUSTOM_FIELD] = current_details
op.save_custom_fields(true)
op.publish_change_to_clients!(:calendar_change)
Jobs.cancel_scheduled_job(:destroy_expired_event, post_id: post.id)
if to
enqueue_in = (to + 1.day - Time.now.utc).seconds
else
enqueue_in = (from.end_of_day + 1.day - Time.now.utc).seconds
end
enqueue_in = 30.seconds if enqueue_in < 0
Jobs.enqueue_in(enqueue_in.to_i, :destroy_expired_event, post_id: post.id)
end
end
end

View File

@ -66,7 +66,9 @@ after_initialize do
date = {}
cooked_date.attributes.values.each do |attribute|
if attribute.name && ['data-date', 'data-time'].include?(attribute.name)
date[attribute.name.gsub('data-', '')] = CGI.escapeHTML(attribute.value || "")
unless attribute.value == 'undefined'
date[attribute.name.gsub('data-', '')] = CGI.escapeHTML(attribute.value || "")
end
end
end
date

View File

@ -60,4 +60,26 @@ describe DiscourseSimpleCalendar::EventUpdater do
expect(op.custom_fields[DiscourseSimpleCalendar::CALENDAR_DETAILS_CUSTOM_FIELD][post.post_number.to_s]).not_to be_present
end
it "will work with no time date" do
raw = <<~MD
[calendar]
[/calendar]
MD
topic = Fabricate(:topic, first_post: create_post(raw: raw))
op = topic.first_post
raw = <<~MD
Rome [date="2018-06-05"] [date="2018-06-11"]
MD
post = create_post(raw: raw, topic: topic)
DiscourseSimpleCalendar::EventUpdater.update(post)
op.reload
detail = op.custom_fields[DiscourseSimpleCalendar::CALENDAR_DETAILS_CUSTOM_FIELD][post.post_number.to_s ]
expect(detail[DiscourseSimpleCalendar::FROM_INDEX]).to eq("2018-06-05T00:00:00Z")
expect(detail[DiscourseSimpleCalendar::TO_INDEX]).to eq("2018-06-11T23:59:59Z")
end
end