DEV: Add destroy theme service

This commit is contained in:
Martin Brennan 2024-11-07 17:11:23 +10:00
parent ae3a1bf2c7
commit 2e75913b2b
No known key found for this signature in database
GPG Key ID: BD981EFEEC8F5675
4 changed files with 40 additions and 37 deletions

View File

@ -191,32 +191,7 @@ class Admin::ThemesController < Admin::AdminController
on_failed_policy(:ban_in_allowlist_mode) { raise Discourse::InvalidAccess }
on_failed_policy(:ban_for_remote_theme) { raise Discourse::InvalidAccess }
on_success { |theme:| render json: serialize_data(theme, ThemeSerializer), status: :created }
on_failure { |theme:| render json: @theme.errors, status: :unprocessable_entity }
end
end
# def create
def create_old
ban_in_allowlist_mode!
@theme =
Theme.new(
name: theme_params[:name],
user_id: theme_user.id,
user_selectable: theme_params[:user_selectable] || false,
color_scheme_id: theme_params[:color_scheme_id],
component: [true, "true"].include?(theme_params[:component]),
)
set_fields
respond_to do |format|
if @theme.save
update_default_theme
log_theme_change(nil, @theme)
format.json { render json: serialize_data(@theme, ThemeSerializer), status: :created }
else
format.json { render json: @theme.errors, status: :unprocessable_entity }
end
on_failure { |theme:| render json: theme.errors, status: :unprocessable_entity }
end
end
@ -281,13 +256,10 @@ class Admin::ThemesController < Admin::AdminController
end
def destroy
@theme = Theme.find_by(id: params[:id])
raise Discourse::InvalidParameters.new(:id) unless @theme
StaffActionLogger.new(current_user).log_theme_destroy(@theme)
@theme.destroy
respond_to { |format| format.json { head :no_content } }
Themes::Destroy.call(service_params) do
on_model_not_found(:theme) { raise Discourse::NotFound }
on_success { render json: {}, status: :no_content }
end
end
def bulk_destroy

View File

@ -20,9 +20,12 @@ class Themes::Create
policy :ban_for_remote_theme
step :set_theme_fields
step :save_theme
step :update_default_theme
step :log_theme_change
transaction do
step :save_theme
step :update_default_theme
step :log_theme_change
end
private

View File

@ -0,0 +1,28 @@
# frozen_string_literal: true
class Themes::Destroy
include Service::Base
params { attribute :id, :integer }
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

View File

@ -990,7 +990,7 @@ RSpec.describe Admin::ThemesController do
it "returns the right response when an invalid id is given" do
delete "/admin/themes/9999.json"
expect(response.status).to eq(400)
expect(response.status).to eq(404)
end
it "deletes the field's javascript cache" do