introduces post_event_enabled site setting

introduces post_event_enabled site setting

diff --git a/app/controllers/discourse_calendar/invitees_controller.rb b/app/controllers/discourse_calendar/invitees_controller.rb
index d6f1d2b..01d7f80 100644
--- a/app/controllers/discourse_calendar/invitees_controller.rb
+++ b/app/controllers/discourse_calendar/invitees_controller.rb
@@ -4,6 +4,13 @@ module DiscourseCalendar
   class InviteesController < ::ApplicationController
     before_action :ensure_logged_in
 
+    before_action :ensure_post_event_enabled
+    def ensure_post_event_enabled
+      if !SiteSetting.post_event_enabled
+        raise Discourse::NotFound
+      end
+    end
+
     def index
       post_event_invitees = PostEvent.find(params['post-event-id']).invitees
 
diff --git a/app/controllers/discourse_calendar/post_events_controller.rb b/app/controllers/discourse_calendar/post_events_controller.rb
index 1f19d5d..a1ed5ae 100644
--- a/app/controllers/discourse_calendar/post_events_controller.rb
+++ b/app/controllers/discourse_calendar/post_events_controller.rb
@@ -4,6 +4,13 @@ module DiscourseCalendar
   class PostEventsController < ::ApplicationController
     before_action :ensure_logged_in
 
+    before_action :ensure_post_event_enabled
+    def ensure_post_event_enabled
+      if !SiteSetting.post_event_enabled
+        raise Discourse::NotFound
+      end
+    end
+
     def index
       # TODO: optimize this
       post_events_topics_ids = PostEvent
diff --git a/app/controllers/discourse_calendar/upcoming_events_controller.rb b/app/controllers/discourse_calendar/upcoming_events_controller.rb
index f2f1502..29bea6f 100644
--- a/app/controllers/discourse_calendar/upcoming_events_controller.rb
+++ b/app/controllers/discourse_calendar/upcoming_events_controller.rb
@@ -4,7 +4,13 @@ module DiscourseCalendar
   class UpcomingEventsController < ::ApplicationController
     before_action :ensure_logged_in
 
-    def index
+    before_action :ensure_post_event_enabled
+    def ensure_post_event_enabled
+      if !SiteSetting.post_event_enabled
+        raise Discourse::NotFound
+      end
     end
+
+    def index; end
   end
 end
diff --git a/assets/javascripts/discourse/routes/upcoming-events-index.js.es6 b/assets/javascripts/discourse/routes/upcoming-events-index.js.es6
index 60b04e9..e8550ce 100644
--- a/assets/javascripts/discourse/routes/upcoming-events-index.js.es6
+++ b/assets/javascripts/discourse/routes/upcoming-events-index.js.es6
@@ -1,10 +1,18 @@
+import DiscourseURL from "discourse/lib/url";
 import Route from "@ember/routing/route";
+import { on } from "@ember/object/evented";
 
 export default Route.extend({
   queryParams: {
     invited: { refreshModel: true, replace: true }
   },
 
+  enforcePostEventEnabled: on("activate", function() {
+    if (!this.siteSettings.post_event_enabled) {
+      DiscourseURL.redirectTo("/404");
+    }
+  }),
+
   model(params) {
     return params;
   },
diff --git a/assets/javascripts/discourse/routes/upcoming-events.js.es6 b/assets/javascripts/discourse/routes/upcoming-events.js.es6
deleted file mode 100644
index f7f5da2..0000000
--- a/assets/javascripts/discourse/routes/upcoming-events.js.es6
+++ /dev/null
@@ -1,3 +0,0 @@
-import Route from "@ember/routing/route";
-
-export default Route.extend({});
diff --git a/assets/javascripts/initializers/add-event-ui-builder.js.es6 b/assets/javascripts/initializers/add-event-ui-builder.js.es6
index c6f785a..fe85c18 100644
--- a/assets/javascripts/initializers/add-event-ui-builder.js.es6
+++ b/assets/javascripts/initializers/add-event-ui-builder.js.es6
@@ -3,14 +3,6 @@ import showModal from "discourse/lib/show-modal";
 import { Promise } from "rsvp";
 
 function initializeEventUIBuilder(api) {
-  api.decorateWidget("hamburger-menu:generalLinks", () => {
-    return {
-      icon: "calendar-day",
-      route: "upcoming-events",
-      label: "upcoming_events.title"
-    };
-  });
-
   api.attachWidgetAction("post", "showEventUIBuilder", function(postId) {
     return new Promise(resolve => {
       if (postId) {
@@ -50,7 +42,10 @@ function initializeEventUIBuilder(api) {
 export default {
   name: "add-event-ui-builder",
 
-  initialize() {
-    withPluginApi("0.8.7", initializeEventUIBuilder);
+  initialize(container) {
+    const siteSettings = container.lookup("site-settings:main");
+    if (siteSettings.post_event_enabled) {
+      withPluginApi("0.8.7", initializeEventUIBuilder);
+    }
   }
 };
diff --git a/assets/javascripts/initializers/add-hamburger-menu-action.js.es6 b/assets/javascripts/initializers/add-hamburger-menu-action.js.es6
new file mode 100644
index 0000000..e126950
--- /dev/null
+++ b/assets/javascripts/initializers/add-hamburger-menu-action.js.es6
@@ -0,0 +1,22 @@
+import { withPluginApi } from "discourse/lib/plugin-api";
+
+function initializeHamburgerMenu(api) {
+  api.decorateWidget("hamburger-menu:generalLinks", () => {
+    return {
+      icon: "calendar-day",
+      route: "upcoming-events",
+      label: "upcoming_events.title"
+    };
+  });
+}
+
+export default {
+  name: "add-hamburger-menu-action",
+
+  initialize(container) {
+    const siteSettings = container.lookup("site-settings:main");
+    if (siteSettings.post_event_enabled) {
+      withPluginApi("0.8.7", initializeHamburgerMenu);
+    }
+  }
+};
diff --git a/assets/javascripts/initializers/post-event-decorator.js.es6 b/assets/javascripts/initializers/post-event-decorator.js.es6
index 1e8acf0..ce375bf 100644
--- a/assets/javascripts/initializers/post-event-decorator.js.es6
+++ b/assets/javascripts/initializers/post-event-decorator.js.es6
@@ -133,7 +133,10 @@ function initializePostEventDecorator(api) {
 export default {
   name: "post-event-decorator",
 
-  initialize() {
-    withPluginApi("0.8.7", initializePostEventDecorator);
+  initialize(container) {
+    const siteSettings = container.lookup("site-settings:main");
+    if (siteSettings.post_event_enabled) {
+      withPluginApi("0.8.7", initializePostEventDecorator);
+    }
   }
 };
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 61f75ad..0f62bcc 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2,6 +2,7 @@ en:
   site_settings:
     calendar_enabled: "Enable the discourse-calendar plugin. This will add support for a [calendar][/calendar] tag in the first post of a topic."
     events_enabled: "Enables users to create events on a topic."
+    post_event_enabled: "[experimental] Enables to attach an event to a post."
     holiday_calendar_topic_id: "Topic ID of staffs holiday / absence calendar."
     delete_expired_event_posts_after: "Posts with expired events will be automatically deleted after (n) hours. Set to -1 to disable deletion."
     all_day_event_start_time: "Events that do not have a start time specified will start at this time. Format is HH:mm. For 6:00 am, enter 06:00"
diff --git a/config/settings.yml b/config/settings.yml
index dfa1b6e..7f7a66e 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -43,6 +43,9 @@ plugins:
   close_to_working_day_hours_extension:
     default: 2
     client: true
+  post_event_enabled:
+    default: false
+    client: true
   displayed_invitees_limit:
     default: 10
     client: false
diff --git a/plugin.rb b/plugin.rb
index 1e068b1..5fe0a69 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -230,10 +230,8 @@ after_initialize do
     DiscourseCalendar::PostEventSerializer.new(object.post_event, scope: scope, root: false)
   end
 
-  reloadable_patch do |plugin|
-    add_to_serializer(:post, :include_post_event?) do
-      plugin.enabled?
-    end
+  add_to_serializer(:post, :include_post_event?) do
+    SiteSetting.post_event_enabled
   end
 
   Discourse::Application.routes.append do
@@ -253,20 +251,20 @@ after_initialize do
     get '/upcoming-events' => 'upcoming_events#index'
   end
 
-  DiscourseEvent.on(:post_destroyed) do |post|
-    if post.post_event
+  on(:post_destroyed) do |post|
+    if SiteSetting.post_event_enabled && post.post_event

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

GitHub sha: b2103e85