FIX: sort calendar_details for better holidays support (#71)

FIX: sort calendar_details for better holidays support (#71)

diff --git a/jobs/scheduled/create_holiday_events.rb b/jobs/scheduled/create_holiday_events.rb
index 5f064ea..334cd9a 100644
--- a/jobs/scheduled/create_holiday_events.rb
+++ b/jobs/scheduled/create_holiday_events.rb
@@ -11,6 +11,7 @@ module Jobs
       require "holidays" unless defined?(Holidays)
 
       regions_and_user_ids = Hash.new { |h, k| h[k] = [] }
+
       UserCustomField
         .where(name: ::DiscourseCalendar::REGION_CUSTOM_FIELD)
         .pluck(:user_id, :value)
@@ -31,13 +32,13 @@ module Jobs
         .map { |user_id, timezone| [user_id, (TZInfo::Timezone.get(timezone) rescue nil)] }
         .to_h
 
-      regions_and_user_ids.keys.each do |region|
-        holidays = Holidays
+      regions_and_user_ids.each do |region, user_ids|
+        Holidays
           .between(Date.today, 6.months.from_now, [region], :observed)
           .filter { |holiday| (1..5) === holiday[:date].wday }
           .each do |holiday|
 
-          regions_and_user_ids[region].each do |user_id|
+          user_ids.each do |user_id|
             next unless usernames[user_id]
 
             date = if tz = user_ids_and_timezones[user_id]
@@ -65,7 +66,7 @@ module Jobs
       return if SiteSetting.all_day_event_start_time.empty? || SiteSetting.all_day_event_end_time.empty?
 
       @holiday_hour ||= begin
-        split = SiteSetting.all_day_event_start_time.split(':')
+        split = SiteSetting.all_day_event_start_time.split(":")
         { hour: split.first, min: split.second }
       end
     end
diff --git a/plugin.rb b/plugin.rb
index b278da1..dbca9b7 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -392,48 +392,37 @@ after_initialize do
   end
 
   add_to_serializer(:post, :calendar_details) do
-    result = []
-    grouped_events = {}
+    grouped = {}
+    standalones = []
 
-    CalendarEvent.where(topic_id: object.topic_id).each do |event|
+    CalendarEvent.where(topic_id: object.topic_id).order(:start_date, :end_date).find_each do |event|
       if event.post_id
-        # Events with no `post_id` are holidays
-
-        result <<
-          {
-            type: :standalone,
-            post_number: event.post_number,
-            message: event.description,
-            from: event.start_date,
-            to: event.end_date,
-            username: event.username,
-            recurring: event.recurrence,
-            post_url: Post.url('-', event.topic_id, event.post_number)
-          }
+        standalones << {
+          type: :standalone,
+          post_number: event.post_number,
+          message: event.description,
+          from: event.start_date,
+          to: event.end_date,
+          username: event.username,
+          recurring: event.recurrence,
+          post_url: Post.url("-", event.topic_id, event.post_number)
+        }
       else
-        identifier =
-          "#{event.region.split('_').first}-#{
-            event.start_date.strftime('%W')
-          }-#{(event.end_date || event.start_date).strftime('%W')}"
+        identifier = "#{event.region.split("_").first}-#{event.start_date.strftime("%W")}"
 
-        if grouped_events[identifier]
-          grouped_events[identifier][:to] = event.start_date
-        else
-          grouped_events[identifier] = {
-            type: :grouped,
-            name: event.description,
-            from: event.start_date,
-            usernames: [],
-            identifier: identifier
-          }
-        end
+        grouped[identifier] ||= {
+          type: :grouped,
+          name: event.description,
+          from: event.start_date,
+          usernames: []
+        }
 
-        grouped_events[identifier][:usernames] << event.username
-        grouped_events[identifier][:usernames].uniq!
+        grouped[identifier][:usernames] << event.username
+        grouped[identifier][:usernames].uniq!
       end
     end
 
-    result.concat(grouped_events.values)
+    standalones + grouped.values
   end
 
   add_to_serializer(:post, :include_calendar_details?) { object.is_first_post? }

GitHub sha: 44521c65

This commit appears in #71 which was approved by jjaffeux. It was merged by ZogStriP.