DEV: Update plugin to match latest guidelines (#390)

- Define extension modules
- Use different files instead of plugin.rb
- Make sure plugin is disabled according to the setting
This commit is contained in:
Bianca Nenciu 2023-02-23 15:25:06 +02:00 committed by GitHub
parent c25461d0d2
commit 8f69c1165b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 241 additions and 232 deletions

View File

@ -1,11 +0,0 @@
# frozen_string_literal: true
module Admin::DiscourseCalendar
class AdminDiscourseCalendarController < Admin::AdminController
before_action :ensure_calendar_enabled
def ensure_calendar_enabled
raise Discourse::NotFound if !SiteSetting.calendar_enabled
end
end
end

View File

@ -1,7 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
module Admin::DiscourseCalendar module Admin::DiscourseCalendar
class AdminHolidaysController < AdminDiscourseCalendarController class AdminHolidaysController < Admin::AdminController
requires_plugin DiscourseCalendar::PLUGIN_NAME
def index def index
region_code = params[:region_code] region_code = params[:region_code]

View File

@ -2,7 +2,11 @@
module DiscoursePostEvent module DiscoursePostEvent
class DiscoursePostEventController < ::ApplicationController class DiscoursePostEventController < ::ApplicationController
requires_plugin DiscourseCalendar::PLUGIN_NAME
before_action :ensure_discourse_post_event_enabled before_action :ensure_discourse_post_event_enabled
private
def ensure_discourse_post_event_enabled def ensure_discourse_post_event_enabled
raise Discourse::NotFound if !SiteSetting.discourse_post_event_enabled raise Discourse::NotFound if !SiteSetting.discourse_post_event_enabled
end end

View File

@ -0,0 +1,45 @@
# frozen_string_literal: true
module DiscoursePostEvent
module ExportCsvControllerExtension
def export_entity
if post_event_export? && ensure_can_export_post_event
Jobs.enqueue(
:export_csv_file,
entity: export_params[:entity],
user_id: current_user.id,
args: export_params[:args],
)
StaffActionLogger.new(current_user).log_entity_export(export_params[:entity])
render json: success_json
else
super
end
end
private
def export_params
if post_event_export?
@_export_params ||=
begin
params.require(:entity)
params.permit(:entity, args: %i[id]).to_h
end
else
super
end
end
def post_event_export?
params[:entity] === "post_event"
end
def ensure_can_export_post_event
return if !SiteSetting.discourse_post_event_enabled
post_event = DiscoursePostEvent::Event.find(export_params[:args][:id])
post_event && guardian.can_act_on_discourse_post_event?(post_event)
end
end
end

View File

@ -0,0 +1,37 @@
# frozen_string_literal: true
module DiscoursePostEvent
module ExportPostEventCsvReportExtension
def post_event_export(&block)
return enum_for(:post_event_export) unless block_given?
guardian = Guardian.new(current_user)
event = DiscoursePostEvent::Event.includes(invitees: :user).find(@extra[:id])
guardian.ensure_can_act_on_discourse_post_event!(event)
event
.invitees
.order(:id)
.each do |invitee|
yield(
[
invitee.user.username,
DiscoursePostEvent::Invitee.statuses[invitee.status],
invitee.created_at,
invitee.updated_at,
]
)
end
end
def get_header(entity)
if SiteSetting.discourse_post_event_enabled && entity === "post_event"
%w[username status first_answered_at last_updated_at]
else
super
end
end
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
module DiscoursePostEvent
module PostExtension
def self.prepended(base)
base.class_eval do
has_one :event,
dependent: :destroy,
class_name: "DiscoursePostEvent::Event",
foreign_key: :id
validate :valid_event
end
end
def valid_event
return unless self.raw_changed?
validator = DiscoursePostEvent::EventValidator.new(self)
validator.validate_event
end
end
end

195
plugin.rb
View File

@ -12,7 +12,7 @@ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
gem "rrule", "0.4.4", require: false gem "rrule", "0.4.4", require: false
load File.expand_path("../lib/calendar_settings_validator.rb", __FILE__) require_relative "lib/calendar_settings_validator.rb"
enabled_site_setting :calendar_enabled enabled_site_setting :calendar_enabled
@ -50,6 +50,7 @@ after_initialize do
add_to_serializer :basic_category, :sort_topics_by_event_start_date do add_to_serializer :basic_category, :sort_topics_by_event_start_date do
object.custom_fields["sort_topics_by_event_start_date"] object.custom_fields["sort_topics_by_event_start_date"]
end end
add_to_serializer :basic_category, :disable_topic_resorting do add_to_serializer :basic_category, :disable_topic_resorting do
object.custom_fields["disable_topic_resorting"] object.custom_fields["disable_topic_resorting"]
end end
@ -74,22 +75,22 @@ after_initialize do
end end
module ::DiscourseCalendar module ::DiscourseCalendar
PLUGIN_NAME ||= "discourse-calendar" PLUGIN_NAME = "discourse-calendar"
# Type of calendar ('static' or 'dynamic') # Type of calendar ('static' or 'dynamic')
CALENDAR_CUSTOM_FIELD ||= "calendar" CALENDAR_CUSTOM_FIELD = "calendar"
# User custom field set when user is on holiday # User custom field set when user is on holiday
HOLIDAY_CUSTOM_FIELD ||= "on_holiday" HOLIDAY_CUSTOM_FIELD = "on_holiday"
# List of all users on holiday # List of all users on holiday
USERS_ON_HOLIDAY_KEY ||= "users_on_holiday" USERS_ON_HOLIDAY_KEY = "users_on_holiday"
# User region used in finding holidays # User region used in finding holidays
REGION_CUSTOM_FIELD ||= "holidays-region" REGION_CUSTOM_FIELD = "holidays-region"
# List of groups # List of groups
GROUP_TIMEZONES_CUSTOM_FIELD ||= "group-timezones" GROUP_TIMEZONES_CUSTOM_FIELD = "group-timezones"
def self.users_on_holiday def self.users_on_holiday
PluginStore.get(PLUGIN_NAME, USERS_ON_HOLIDAY_KEY) || [] PluginStore.get(PLUGIN_NAME, USERS_ON_HOLIDAY_KEY) || []
@ -106,11 +107,11 @@ after_initialize do
end end
module ::DiscoursePostEvent module ::DiscoursePostEvent
PLUGIN_NAME ||= "discourse-post-event" PLUGIN_NAME = "discourse-post-event"
# Topic where op has a post event custom field # Topic where op has a post event custom field
TOPIC_POST_EVENT_STARTS_AT ||= "TopicEventStartsAt" TOPIC_POST_EVENT_STARTS_AT = "TopicEventStartsAt"
TOPIC_POST_EVENT_ENDS_AT ||= "TopicEventEndsAt" TOPIC_POST_EVENT_ENDS_AT = "TopicEventEndsAt"
class Engine < ::Rails::Engine class Engine < ::Rails::Engine
engine_name PLUGIN_NAME engine_name PLUGIN_NAME
@ -123,11 +124,10 @@ after_initialize do
add_admin_route "admin.calendar", "calendar" add_admin_route "admin.calendar", "calendar"
%w[ %w[
../app/controllers/admin/admin_discourse_calendar_controller.rb app/controllers/admin/discourse_calendar/admin_holidays_controller.rb
../app/controllers/admin/discourse_calendar/admin_holidays_controller.rb app/models/discourse_calendar/disabled_holiday.rb
../app/models/discourse_calendar/disabled_holiday.rb app/services/discourse_calendar/holiday.rb
../app/services/discourse_calendar/holiday.rb ].each { |path| require_relative path }
].each { |path| load File.expand_path(path, __FILE__) }
Discourse::Application.routes.append do Discourse::Application.routes.append do
mount ::DiscourseCalendar::Engine, at: "/" mount ::DiscourseCalendar::Engine, at: "/"
@ -147,23 +147,26 @@ after_initialize do
# DISCOURSE POST EVENT # DISCOURSE POST EVENT
%w[ %w[
../app/controllers/discourse_post_event_controller.rb app/controllers/discourse_post_event_controller.rb
../app/controllers/discourse_post_event/invitees_controller.rb app/controllers/discourse_post_event/events_controller.rb
../app/controllers/discourse_post_event/events_controller.rb app/controllers/discourse_post_event/invitees_controller.rb
../app/controllers/discourse_post_event/upcoming_events_controller.rb app/controllers/discourse_post_event/upcoming_events_controller.rb
../app/models/discourse_post_event/event.rb app/models/discourse_post_event/event_date.rb
../app/models/discourse_post_event/event_date.rb app/models/discourse_post_event/event.rb
../app/models/discourse_post_event/invitee.rb app/models/discourse_post_event/invitee.rb
../lib/discourse_post_event/event_parser.rb app/serializers/discourse_post_event/event_serializer.rb
../lib/discourse_post_event/event_validator.rb app/serializers/discourse_post_event/invitee_serializer.rb
../lib/discourse_post_event/rrule_generator.rb jobs/regular/discourse_post_event/bulk_invite.rb
../jobs/regular/discourse_post_event/bulk_invite.rb jobs/regular/discourse_post_event/bump_topic.rb
../jobs/regular/discourse_post_event/send_reminder.rb jobs/regular/discourse_post_event/send_reminder.rb
../jobs/regular/discourse_post_event/bump_topic.rb lib/discourse_post_event/event_finder.rb
../lib/discourse_post_event/event_finder.rb lib/discourse_post_event/event_parser.rb
../app/serializers/discourse_post_event/invitee_serializer.rb lib/discourse_post_event/event_validator.rb
../app/serializers/discourse_post_event/event_serializer.rb lib/discourse_post_event/export_csv_controller_extension.rb
].each { |path| load File.expand_path(path, __FILE__) } lib/discourse_post_event/export_csv_file_extension.rb
lib/discourse_post_event/post_extension.rb
lib/discourse_post_event/rrule_generator.rb
].each { |path| require_relative path }
::ActionController::Base.prepend_view_path File.expand_path("../app/views", __FILE__) ::ActionController::Base.prepend_view_path File.expand_path("../app/views", __FILE__)
@ -185,16 +188,9 @@ after_initialize do
end end
reloadable_patch do reloadable_patch do
Post.class_eval do ExportCsvController.class_eval { prepend DiscoursePostEvent::ExportCsvControllerExtension }
has_one :event, dependent: :destroy, class_name: "DiscoursePostEvent::Event", foreign_key: :id Jobs::ExportCsvFile.class_eval { prepend DiscoursePostEvent::ExportPostEventCsvReportExtension }
Post.class_eval { prepend DiscoursePostEvent::PostExtension }
validate :valid_event
def valid_event
return unless self.raw_changed?
validator = DiscoursePostEvent::EventValidator.new(self)
validator.validate_event
end
end
end end
add_to_class(:user, :can_create_discourse_post_event?) do add_to_class(:user, :can_create_discourse_post_event?) do
@ -317,20 +313,20 @@ after_initialize do
# DISCOURSE CALENDAR # DISCOURSE CALENDAR
%w[ %w[
../app/models/calendar_event.rb app/models/calendar_event.rb
../app/serializers/user_timezone_serializer.rb app/serializers/user_timezone_serializer.rb
../jobs/scheduled/create_holiday_events.rb jobs/scheduled/create_holiday_events.rb
../jobs/scheduled/delete_expired_event_posts.rb jobs/scheduled/delete_expired_event_posts.rb
../jobs/scheduled/update_holiday_usernames.rb jobs/scheduled/monitor_event_dates.rb
../jobs/scheduled/monitor_event_dates.rb jobs/scheduled/update_holiday_usernames.rb
../lib/calendar_validator.rb lib/calendar_validator.rb
../lib/calendar.rb lib/calendar.rb
../lib/event_validator.rb lib/event_validator.rb
../lib/group_timezones.rb lib/group_timezones.rb
../lib/time_sniffer.rb lib/holiday_status.rb
../lib/users_on_holiday.rb lib/time_sniffer.rb
../lib/holiday_status.rb lib/users_on_holiday.rb
].each { |path| load File.expand_path(path, __FILE__) } ].each { |path| require_relative path }
register_post_custom_field_type(DiscourseCalendar::CALENDAR_CUSTOM_FIELD, :string) register_post_custom_field_type(DiscourseCalendar::CALENDAR_CUSTOM_FIELD, :string)
register_post_custom_field_type(DiscourseCalendar::GROUP_TIMEZONES_CUSTOM_FIELD, :json) register_post_custom_field_type(DiscourseCalendar::GROUP_TIMEZONES_CUSTOM_FIELD, :json)
@ -476,92 +472,6 @@ after_initialize do
add_to_serializer(:site, :include_users_on_holiday?) { scope.is_staff? } add_to_serializer(:site, :include_users_on_holiday?) { scope.is_staff? }
reloadable_patch do
module DiscoursePostEvent::ExportCsvControllerExtension
def export_entity
if post_event_export? && ensure_can_export_post_event
Jobs.enqueue(
:export_csv_file,
entity: export_params[:entity],
user_id: current_user.id,
args: export_params[:args],
)
StaffActionLogger.new(current_user).log_entity_export(export_params[:entity])
render json: success_json
else
super
end
end
private
def export_params
if post_event_export?
@_export_params ||=
begin
params.require(:entity)
params.permit(:entity, args: %i[id]).to_h
end
else
super
end
end
def post_event_export?
params[:entity] === "post_event"
end
def ensure_can_export_post_event
return if !SiteSetting.discourse_post_event_enabled
post_event = DiscoursePostEvent::Event.find(export_params[:args][:id])
post_event && guardian.can_act_on_discourse_post_event?(post_event)
end
end
require_dependency "export_csv_controller"
class ::ExportCsvController
prepend DiscoursePostEvent::ExportCsvControllerExtension
end
module ExportPostEventCsvReportExtension
def post_event_export(&block)
return enum_for(:post_event_export) unless block_given?
guardian = Guardian.new(current_user)
event = DiscoursePostEvent::Event.includes(invitees: :user).find(@extra[:id])
guardian.ensure_can_act_on_discourse_post_event!(event)
event
.invitees
.order(:id)
.each do |invitee|
yield(
[
invitee.user.username,
DiscoursePostEvent::Invitee.statuses[invitee.status],
invitee.created_at,
invitee.updated_at,
]
)
end
end
def get_header(entity)
if SiteSetting.discourse_post_event_enabled && entity === "post_event"
%w[username status first_answered_at last_updated_at]
else
super
end
end
end
class Jobs::ExportCsvFile
prepend ExportPostEventCsvReportExtension
end
on(:reduce_cooked) do |fragment, post| on(:reduce_cooked) do |fragment, post|
if SiteSetting.discourse_post_event_enabled if SiteSetting.discourse_post_event_enabled
fragment fragment
@ -647,7 +557,6 @@ after_initialize do
field :topic_id, component: :text field :topic_id, component: :text
end end
end end
end
query = query =
Proc.new do |notifications, data| Proc.new do |notifications, data|