From 0aa14333e56a4287a8a36631d39b4ddeac57e203 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 6 Sep 2018 12:52:05 +0530 Subject: [PATCH 1/2] minor fixes and refactoring --- .../initializers/add-holiday-flair.js.es6 | 2 ++ plugin.rb | 21 ++++++++++--------- .../ensure_expired_event_destruction_spec.rb | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/assets/javascripts/initializers/add-holiday-flair.js.es6 b/assets/javascripts/initializers/add-holiday-flair.js.es6 index 9522f281..430b6f1a 100644 --- a/assets/javascripts/initializers/add-holiday-flair.js.es6 +++ b/assets/javascripts/initializers/add-holiday-flair.js.es6 @@ -15,6 +15,8 @@ export default { withPluginApi('0.1', api => { const usersOnHoliday = Discourse.Site.current().users_on_holiday; + if (!usersOnHoliday) { return; } + api.addPosterIcon(cfs => { const onHoliday = customBool(cfs.on_holiday); if (!onHoliday) { return; } diff --git a/plugin.rb b/plugin.rb index 49620544..d68bd5ad 100644 --- a/plugin.rb +++ b/plugin.rb @@ -25,20 +25,21 @@ after_initialize do TO_INDEX = 2 USERNAME_INDEX = 3 - autoload :CalendarValidator, "#{Rails.root}/plugins/discourse-simple-calendar/lib/calendar_validator" - autoload :CalendarUpdater, "#{Rails.root}/plugins/discourse-simple-calendar/lib/calendar_updater" - autoload :CalendarDestroyer, "#{Rails.root}/plugins/discourse-simple-calendar/lib/calendar_destroyer" - autoload :EventValidator, "#{Rails.root}/plugins/discourse-simple-calendar/lib/event_validator" - autoload :EventUpdater, "#{Rails.root}/plugins/discourse-simple-calendar/lib/event_updater" - autoload :EventDestroyer, "#{Rails.root}/plugins/discourse-simple-calendar/lib/event_destroyer" - def self.users_on_holiday PluginStore.get(PLUGIN_NAME, USERS_ON_HOLIDAY_KEY) end end - require File.expand_path("../jobs/scheduled/ensure_expired_event_destruction", __FILE__) - require File.expand_path("../jobs/scheduled/update_holiday_usernames", __FILE__) + [ + "../lib/calendar_validator.rb", + "../lib/calendar_updater.rb", + "../lib/calendar_destroyer.rb", + "../lib/event_validator.rb", + "../lib/event_updater.rb", + "../lib/event_destroyer.rb", + "../jobs/scheduled/ensure_expired_event_destruction.rb", + "../jobs/scheduled/update_holiday_usernames.rb", + ].each { |path| load File.expand_path(path, __FILE__) } register_post_custom_field_type(DiscourseSimpleCalendar::CALENDAR_DETAILS_CUSTOM_FIELD, :json) register_post_custom_field_type(DiscourseSimpleCalendar::CALENDAR_CUSTOM_FIELD, :string) @@ -161,6 +162,6 @@ after_initialize do end add_to_serializer(:site, :users_on_holiday) do - PluginStore.get(PLUGIN_NAME, DiscourseSimpleCalendar::USERS_ON_HOLIDAY_KEY) + DiscourseSimpleCalendar.users_on_holiday end end diff --git a/spec/lib/ensure_expired_event_destruction_spec.rb b/spec/lib/ensure_expired_event_destruction_spec.rb index 80cd2676..773bc6f5 100644 --- a/spec/lib/ensure_expired_event_destruction_spec.rb +++ b/spec/lib/ensure_expired_event_destruction_spec.rb @@ -27,7 +27,7 @@ describe DiscourseSimpleCalendar::EventUpdater do expect(post.deleted_at).to be_nil expect(op.custom_fields[DiscourseSimpleCalendar::CALENDAR_DETAILS_CUSTOM_FIELD][post_number]).to be_present - DiscourseSimpleCalendar::EnsuredExpiredEventDestruction.new.execute + DiscourseSimpleCalendar::EnsuredExpiredEventDestruction.new.execute(nil) post.reload op.reload From 9a493a9dbca804ea5d68a1e0ee7625e5b306b345 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 6 Sep 2018 23:57:47 +0530 Subject: [PATCH 2/2] DEV: Remove dates extract method and use local_dates post attribute --- lib/event_updater.rb | 7 +++---- plugin.rb | 21 --------------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/lib/event_updater.rb b/lib/event_updater.rb index c838a1cd..ea92e05d 100644 --- a/lib/event_updater.rb +++ b/lib/event_updater.rb @@ -2,8 +2,7 @@ module DiscourseSimpleCalendar class EventUpdater def self.update(post) op = post.topic.first_post - - dates = DiscourseSimpleCalendar::Dates.extract(post.raw, post.topic_id, post.user.id) + dates = post.local_dates # if we don’t have any date it's not an event anymore if dates.empty? @@ -14,7 +13,7 @@ module DiscourseSimpleCalendar first_date = dates[0] if first_date['time'] - from = Time.strptime("#{first_date['date']} #{first_date['time']} UTC", "%Y-%m-%d %H:%M %Z") + from = Time.strptime("#{first_date['date']} #{first_date['time']} UTC", "%Y-%m-%d %H:%M:%S %Z") else from = Time.strptime("#{first_date['date']} UTC", "%Y-%m-%d %Z").beginning_of_day end @@ -23,7 +22,7 @@ module DiscourseSimpleCalendar second_date = dates[1] if second_date['time'] - to = Time.strptime("#{second_date['date']} #{second_date['time']} UTC", "%Y-%m-%d %H:%M %Z") + to = Time.strptime("#{second_date['date']} #{second_date['time']} UTC", "%Y-%m-%d %H:%M:%S %Z") else to = Time.strptime("#{second_date['date']} UTC", "%Y-%m-%d %Z").end_of_day end diff --git a/plugin.rb b/plugin.rb index d68bd5ad..da65ec7e 100644 --- a/plugin.rb +++ b/plugin.rb @@ -66,27 +66,6 @@ after_initialize do end end - # should be moved into discourse-local-dates plugin code - class DiscourseSimpleCalendar::Dates - class << self - def extract(raw, topic_id, user_id = nil) - cooked = PrettyText.cook(raw, topic_id: topic_id, user_id: user_id) - - Nokogiri::HTML(cooked).css('span.discourse-local-date').map do |cooked_date| - date = {} - cooked_date.attributes.values.each do |attribute| - if attribute.name && ['data-date', 'data-time'].include?(attribute.name) - unless attribute.value == 'undefined' - date[attribute.name.gsub('data-', '')] = CGI.escapeHTML(attribute.value || "") - end - end - end - date - end - end - end - end - on(:post_process_cooked) do |doc, post| validator = DiscourseSimpleCalendar::EventValidator.new(post)