diff --git a/lib/event_updater.rb b/lib/event_updater.rb index 27f5abd5..c838a1cd 100644 --- a/lib/event_updater.rb +++ b/lib/event_updater.rb @@ -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 diff --git a/plugin.rb b/plugin.rb index 7ec2b27c..9a5a76c7 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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 diff --git a/spec/lib/event_updater_spec.rb b/spec/lib/event_updater_spec.rb index e5005aac..868959fc 100644 --- a/spec/lib/event_updater_spec.rb +++ b/spec/lib/event_updater_spec.rb @@ -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