FIX: Ensure calendar events are recreated when first post changes

This commit is contained in:
Bianca Nenciu 2020-03-20 19:55:17 +02:00
parent bb973f6638
commit 0f922cdcb8
2 changed files with 17 additions and 1 deletions

View File

@ -22,17 +22,22 @@ module DiscourseCalendar
def self.update(post) def self.update(post)
calendar = extract(post) calendar = extract(post)
return destroy(post) if calendar.size != 1 return destroy(post) if calendar.size != 1
calendar = calendar.first calendar = calendar.first
post.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD] = calendar.delete("type") || "dynamic" post.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD] = calendar.delete("type") || "dynamic"
post.save_custom_fields post.save_custom_fields
Post.where(topic_id: post.topic_id).each { |p| CalendarEvent.update(p) }
end end
def self.destroy(post) def self.destroy(post)
return if post.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD].blank?
post.custom_fields.delete(DiscourseCalendar::CALENDAR_CUSTOM_FIELD) post.custom_fields.delete(DiscourseCalendar::CALENDAR_CUSTOM_FIELD)
post.save_custom_fields post.save_custom_fields
CalendarEvent.where(topic_id: post.topic_id).destroy_all
end end
end end
end end

View File

@ -74,6 +74,17 @@ describe CalendarEvent do
expect(post.deleted_at).to eq(nil) expect(post.deleted_at).to eq(nil)
end 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 describe "all day event site settings" do
before do before do
SiteSetting.all_day_event_start_time = "06:30" SiteSetting.all_day_event_start_time = "06:30"