discourse/app/services/themes/destroy.rb
Martin Brennan 4b947d2404
DEV: Partially refactor themes controller to services (#33141)
This commit starts to introduce services to replace actions in the
ThemesController. We will start with the low langing fruit:

* Create
* Destroy
* BulkDestroy
* GetTranslations

Endpoints like #import and #update will be much harder.
Also, the https://github.com/discourse/discourse-theme-creator plugin
overrides some of these controller actions directly, so we need
to be careful.
2025-06-23 10:14:58 +10:00

50 lines
1.1 KiB
Ruby

# frozen_string_literal: true
# Destroys a theme and logs the staff action. Related records are destroyed
# by ActiveRecord dependent: :destroy. Cannot be used to destroy system themes.
#
# @example
# Themes::Destroy.call(
# guardian: guardian,
# params: {
# id: theme.id,
# }
# )
#
class Themes::Destroy
include Service::Base
# @!method self.call(guardian:, params:)
# @param [Guardian] guardian
# @param [Hash] params
# @option params [Integer] :id The ID of the theme to destroy, must be greater than 0.
# @return [Service::Base::Context]
params do
attribute :id, :integer
# Negative theme IDs are for system themes only, which cannot be destroyed.
validates :id, presence: true, numericality: { only_integer: true, greater_than: 0 }
end
model :theme
transaction do
step :destroy_theme
step :log_theme_destroy
end
private
def fetch_theme(params:)
Theme.find_by(id: params.id)
end
def destroy_theme(theme:)
theme.destroy
end
def log_theme_destroy(theme:, guardian:)
StaffActionLogger.new(guardian.user).log_theme_destroy(theme)
end
end