diff --git a/lib/calendar.rb b/lib/calendar.rb index 3b047b48..7025e995 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -22,17 +22,22 @@ module DiscourseCalendar def self.update(post) calendar = extract(post) - return destroy(post) if calendar.size != 1 calendar = calendar.first post.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD] = calendar.delete("type") || "dynamic" post.save_custom_fields + + Post.where(topic_id: post.topic_id).each { |p| CalendarEvent.update(p) } end def self.destroy(post) + return if post.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD].blank? + post.custom_fields.delete(DiscourseCalendar::CALENDAR_CUSTOM_FIELD) post.save_custom_fields + + CalendarEvent.where(topic_id: post.topic_id).destroy_all end end end diff --git a/spec/models/calendar_event_spec.rb b/spec/models/calendar_event_spec.rb index 9af68922..055dcf5e 100644 --- a/spec/models/calendar_event_spec.rb +++ b/spec/models/calendar_event_spec.rb @@ -74,6 +74,17 @@ describe CalendarEvent do expect(post.deleted_at).to eq(nil) end + it "recreates calendar event for all posts" do + post = create_post(raw: 'Rome [date="2018-06-05" time="10:20:00"]', topic: calendar_post.topic) + expect(CalendarEvent.count).to eq(1) + + PostDestroyer.new(Discourse.system_user, calendar_post.reload).destroy + expect(CalendarEvent.count).to eq(0) + + PostDestroyer.new(Discourse.system_user, calendar_post.reload).recover + expect(CalendarEvent.count).to eq(1) + end + describe "all day event site settings" do before do SiteSetting.all_day_event_start_time = "06:30"