Skip to content

A platform for community discussion. Free, open, simple.

FIX: Autoload calendar helpers after development reloads (#43359)

Krzysztof Kotlarekd38c032c7968
diff --git a/plugins/discourse-events/lib/discourse_events/engine.rb b/plugins/discourse-events/lib/discourse_events/engine.rb
index 84db227323c..ea4eb178d4b 100644
--- a/plugins/discourse-events/lib/discourse_events/engine.rb
+++ b/plugins/discourse-events/lib/discourse_events/engine.rb
@@ -4,5 +4,17 @@ module DiscourseEvents
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscourseEvents
+
+ initializer "discourse_events.autoloading", before: :setup_main_autoloader do
+ directory = config.root.join("lib/discourse_events")
+ Rails.autoloaders.main.push_dir(directory, namespace: DiscourseEvents)
+
+ # These subtrees retain their explicit boot-time loading.
+ Rails.autoloaders.main.ignore(
+ directory.join("configuration"),
+ directory.join("events"),
+ directory.join("livestream"),
+ )
+ end
end
end
diff --git a/plugins/discourse-events/plugin.rb b/plugins/discourse-events/plugin.rb
index c5ac21b3e00..dfd17a53d9f 100644
--- a/plugins/discourse-events/plugin.rb
+++ b/plugins/discourse-events/plugin.rb
@@ -586,13 +586,6 @@ after_initialize do
require_relative "jobs/scheduled/delete_expired_event_posts"
require_relative "jobs/scheduled/monitor_event_dates"
require_relative "jobs/scheduled/update_holiday_usernames"
- require_relative "lib/discourse_events/calendar/extractor"
- require_relative "lib/discourse_events/calendar/validator"
- require_relative "lib/discourse_events/calendar/event_validator"
- require_relative "lib/discourse_events/group_timezones/extractor"
- require_relative "lib/discourse_events/holidays/finder"
- require_relative "lib/discourse_events/holidays/status"
- require_relative "lib/discourse_events/holidays/users_on_holiday"
register_post_custom_field_type(DiscourseEvents::CALENDAR_CUSTOM_FIELD, :string)
register_post_custom_field_type(DiscourseEvents::GROUP_TIMEZONES_CUSTOM_FIELD, :json)
diff --git a/plugins/discourse-events/spec/lib/discourse_events/calendar/extractor_spec.rb b/plugins/discourse-events/spec/lib/discourse_events/calendar/extractor_spec.rb
index 1fc9cf5f7ea..0bc48bc8166 100644
--- a/plugins/discourse-events/spec/lib/discourse_events/calendar/extractor_spec.rb
+++ b/plugins/discourse-events/spec/lib/discourse_events/calendar/extractor_spec.rb
@@ -54,6 +54,18 @@ describe DiscourseEvents::Calendar::Extractor do
expect(calendar_event.username).to eq(post.user.username)
end
+ it "removes the calendar event when its post is deleted" do
+ event_post =
+ create_post(topic: calendar_post.topic, raw: 'Rome [date="2018-06-05" time="12:34:56"]')
+
+ expect(DiscourseEvents::Calendar::Event.where(post_id: event_post.id)).to exist
+
+ PostDestroyer.new(Fabricate(:admin), event_post).destroy
+
+ expect(event_post.reload).to be_trashed
+ expect(DiscourseEvents::Calendar::Event.where(post_id: event_post.id)).not_to exist
+ end
+
it "raises an error when there are more than 2 dates" do
expect {
create_post(