FIX: all the specs

FIX: all the specs

diff --git a/lib/calendar_updater.rb b/lib/calendar_updater.rb
index 36512b1..453c69a 100644
--- a/lib/calendar_updater.rb
+++ b/lib/calendar_updater.rb
@@ -3,9 +3,11 @@ module DiscourseCalendar
     def self.update(post)
       calendar = post.calendar || {}
 
-      post.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD] = calendar.delete("type") || "dynamic"
+      previous_type = post.custom_fields[CALENDAR_CUSTOM_FIELD].dup
 
-      unless post.custom_fields[DiscourseCalendar::CALENDAR_DETAILS_CUSTOM_FIELD].present?
+      post.custom_fields[CALENDAR_CUSTOM_FIELD] = calendar.delete("type") || "dynamic"
+
+      if previous_type != post.custom_fields[CALENDAR_CUSTOM_FIELD] || post.calendar_details.blank?
         post.calendar_details = {}
       end
 
diff --git a/lib/event_validator.rb b/lib/event_validator.rb
index 778d525..45129c8 100644
--- a/lib/event_validator.rb
+++ b/lib/event_validator.rb
@@ -2,18 +2,16 @@ module DiscourseCalendar
   class EventValidator
     def initialize(post)
       @post = post
-      @calendar = post&.topic&.first_post&.custom_fields
+      @op = post.topic.first_post
     end
 
     def validate_event
       dates_count = DiscourseCalendar::Event::count(@post)
-      calendar_type = @calendar[DiscourseCalendar::CALENDAR_CUSTOM_FIELD] || "dynamic"
+      calendar_type = @op.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD] || "dynamic"
 
       if calendar_type == "dynamic"
         return false if has_too_many_dates?(dates_count)
-      end
-
-      if calendar_type == "static"
+      elsif calendar_type == "static"
         return false if dates_count > 0
       end
 
diff --git a/plugin.rb b/plugin.rb
index 1edfe7c..c210ee5 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -39,12 +39,14 @@ after_initialize do
   register_post_custom_field_type(DiscourseCalendar::CALENDAR_DETAILS_CUSTOM_FIELD, :json)
   register_post_custom_field_type(DiscourseCalendar::CALENDAR_CUSTOM_FIELD, :string)
 
-  whitelist_staff_user_custom_field(::DiscourseCalendar::HOLIDAY_CUSTOM_FIELD)
+  whitelist_staff_user_custom_field(DiscourseCalendar::HOLIDAY_CUSTOM_FIELD)
 
   class DiscourseCalendar::Calendar
     class << self
       def extract(post)
-        Nokogiri::HTML(post.cooked).css('div.calendar').map do |cooked_calendar|
+        cooked = PrettyText.cook(post.raw, topic_id: post.topic_id, user_id: post.user_id)
+
+        Nokogiri::HTML(cooked).css('div.calendar').map do |cooked_calendar|
           calendar = {}
 
           cooked_calendar.attributes.values.each do |attribute|
@@ -62,7 +64,9 @@ after_initialize do
   class DiscourseCalendar::Event
     class << self
       def count(post)
-        Nokogiri::HTML(post.cooked).css('span.discourse-local-date').count
+        cooked = PrettyText.cook(post.raw, topic_id: post.topic_id, user_id: post.user_id)
+
+        Nokogiri::HTML(cooked).css('span.discourse-local-date').count
       end
     end
   end
diff --git a/spec/jobs/ensure_expired_event_destruction_spec.rb b/spec/jobs/ensure_expired_event_destruction_spec.rb
index e033577..a8b15e8 100644
--- a/spec/jobs/ensure_expired_event_destruction_spec.rb
+++ b/spec/jobs/ensure_expired_event_destruction_spec.rb
@@ -1,23 +1,15 @@
-require 'rails_helper'
+require "rails_helper"
 
 describe DiscourseCalendar::EnsuredExpiredEventDestruction do
   before do
-    SiteSetting.queue_jobs = false
-
-    raw = <<~MD
-      [calendar]
-      [/calendar]
-    MD
-    topic = Fabricate(:topic, first_post: create_post(raw: raw))
-    @op = topic.first_post
+    Jobs.run_immediately!
+    SiteSetting.calendar_enabled = true
 
+    @op = create_post(raw: "[calendar]\n[/calendar]")
     expect(@op.calendar_details).to eq({})
 
-    raw = <<~MD
-      Rome [date="2018-06-05" time="10:20:00"] to [date="2018-06-06" time="11:20:00"]
-    MD
-
-    @post = create_post(raw: raw, topic: topic)
+    raw = 'Rome [date="2018-06-05" time="10:20:00"] to [date="2018-06-06" time="11:20:00"]'
+    @post = create_post(raw: raw, topic: @op.topic)
     CookedPostProcessor.new(@post).post_process
   end
 
@@ -27,7 +19,7 @@ describe DiscourseCalendar::EnsuredExpiredEventDestruction do
     @op.reload
 
     expect(@op.calendar_details[@post.post_number.to_s]).to eq([
-      "Rome  to", "2018-06-05T10:20:00Z", "2018-06-06T11:20:00Z", @post.user.username_lower
+      "Rome  to", "2018-06-05T10:20:00Z", "2018-06-06T11:20:00Z", @post.user.username_lower, nil
     ])
 
     freeze_time Time.strptime("2018-06-06 13:21:00 UTC", "%Y-%m-%d %H:%M:%S %Z")
@@ -40,9 +32,7 @@ describe DiscourseCalendar::EnsuredExpiredEventDestruction do
   it "wont destroy recurring events" do
     freeze_time Time.strptime("2018-06-03 09:21:00 UTC", "%Y-%m-%d %H:%M:%S %Z")
 
-    raw = <<~MD
-      Rome [date="2018-06-05" time="10:20:00" recurring="1.weeks"] to [date="2018-06-06" time="11:20:00"]
-    MD
+    raw = 'Rome [date="2018-06-05" time="10:20:00" recurring="1.weeks"] to [date="2018-06-06" time="11:20:00"]'
     @post = create_post(raw: raw, topic: @op.topic)
     CookedPostProcessor.new(@post).post_process
 
diff --git a/spec/jobs/update_holiday_usernames_spec.rb b/spec/jobs/update_holiday_usernames_spec.rb
index cf7681e..2cb67c1 100644
--- a/spec/jobs/update_holiday_usernames_spec.rb
+++ b/spec/jobs/update_holiday_usernames_spec.rb
@@ -2,21 +2,16 @@ require 'rails_helper'
 
 describe DiscourseCalendar::UpdateHolidayUsernames do
   before do
-    SiteSetting.queue_jobs = false
-
-    raw = <<~MD
-      [calendar]
-      [/calendar]
-    MD
-    @topic = Fabricate(:topic, first_post: create_post(raw: raw))
-    SiteSetting.holiday_calendar_topic_id = @topic.id
+    Jobs.run_immediately!
+    SiteSetting.calendar_enabled = true
+
+    @op = create_post(raw: "[calendar]\n[/calendar]")
+    SiteSetting.holiday_calendar_topic_id = @op.topic_id
   end
 
   it "should update users on holiday list" do
-    raw = <<~MD
-    Rome [date="2018-06-05" time="10:20:00"] to [date="2018-06-06" time="10:20:00"]
-    MD
-    post = create_post(raw: raw, topic: @topic)
+    raw = 'Rome [date="2018-06-05" time="10:20:00"] to [date="2018-06-06" time="10:20:00"]'
+    post = create_post(raw: raw, topic: @op.topic)
     CookedPostProcessor.new(post).post_process
 
     freeze_time Time.strptime("2018-06-05 18:40:00 UTC", "%Y-%m-%d %H:%M:%S %Z")
@@ -26,10 +21,8 @@ describe DiscourseCalendar::UpdateHolidayUsernames do
   end
 
   it "should have empty users on holiday list" do
-    raw = <<~MD
-    Rome [date="2018-06-05" time="10:20:00"] to [date="2018-06-06" time="10:20:00"]
-    MD
-    post = create_post(raw: raw, topic: @topic)
+    raw = 'Rome [date="2018-06-05" time="10:20:00"] to [date="2018-06-06" time="10:20:00"]'
+    post = create_post(raw: raw, topic: @op.topic)
     CookedPostProcessor.new(post).post_process
 
     freeze_time Time.strptime("2018-06-07 18:40:00 UTC", "%Y-%m-%d %H:%M:%S %Z")
diff --git a/spec/lib/calendar_updater_spec.rb b/spec/lib/calendar_updater_spec.rb
deleted file mode 100644
index e1db5ab..0000000
--- a/spec/lib/calendar_updater_spec.rb
+++ /dev/null
@@ -1,24 +0,0 @@
-require 'rails_helper'
-
-describe DiscourseCalendar::CalendarUpdater do
-  before do
-    SiteSetting.queue_jobs = false
-  end
-
-  it "will correctly update the calendar" do
-    post = create_post
-
-    expect(post.custom_fields).to eq({})
-
-    DiscourseCalendar::CalendarUpdater.update(post)
-
-    expect(post.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD]).to eq("dynamic")
-    expect(post.calendar_details).to eq({})
-
-    post.calendar = { "type" => "static" }
-
-    DiscourseCalendar::CalendarUpdater.update(post)
-
-    expect(post.custom_fields[DiscourseCalendar::CALENDAR_CUSTOM_FIELD]).to eq("static")
-  end
-end
diff --git a/spec/lib/dynamic_calendar_spec.rb b/spec/lib/dynamic_calendar_spec.rb
index d0a50ad..f53d7ca 100644
--- a/spec/lib/dynamic_calendar_spec.rb
+++ b/spec/lib/dynamic_calendar_spec.rb
@@ -1,106 +1,75 @@
-require 'rails_helper'
+require "rails_helper"
 
-describe 'Dynamic calendar' do

[... diff too long, it was truncated ...]

GitHub sha: daf377d2