discourse/app/services/upcoming_changes/toggle.rb
Martin Brennan 29b3b2329a
FEATURE: Use dropdown for upcoming change toggle (#36091)
To make things a bit clearer for admins, and to bury the option to
enable changes only for specific groups a bit since we don't think
most sites will do this, this commit changes the upcoming change
toggle to an "Enabled for:" dropdown with the following options:

* No one
* Everyone
* Staff only
* Specific group(s)

We automatically save the staff site setting group in the background
if staff only is selected, and we only save specific groups (and toggle
the
change on) once more than one group is selected.


---------

Co-authored-by: awesomerobot <kris.aubuchon@discourse.org>
2025-11-25 11:26:50 +10:00

45 lines
1.3 KiB
Ruby
Vendored

# frozen_string_literal: true
class UpcomingChanges::Toggle
include Service::Base
params do
attribute :setting_name, :string
attribute :enabled, :boolean
validates :setting_name, presence: true
validates :enabled, inclusion: [true, false]
end
policy :current_user_is_admin
policy :setting_is_available
transaction { step :toggle }
private
def current_user_is_admin(guardian:)
guardian.is_admin?
end
def setting_is_available(params:)
SiteSetting.respond_to?(params.setting_name)
end
def toggle(params:, guardian:)
# TODO (martin) Remove this once we release upcoming changes,
# otherwise it will be confusing for people to see log messages
# about upcoming changes via "What's new?" experimental toggles
# before we update that UI.
if SiteSetting.enable_upcoming_changes
previous_value = SiteSetting.public_send(params.setting_name)
SiteSetting.send("#{params.setting_name}=", params.enabled)
StaffActionLogger.new(guardian.user).log_upcoming_change_toggle(
params.setting_name,
previous_value,
params.enabled,
{ context: I18n.t("staff_action_logs.upcoming_changes.log_manually_toggled") },
)
else
SiteSetting.set_and_log(params.setting_name, params.enabled, guardian.user)
end
end
end