discourse/app/services/flags/destroy_flag.rb
Loïc Guitaut 7f607699b8 DEV: Refactor flag related services a bit
Extracted from https://github.com/discourse/discourse/pull/29129.

This patch makes the code more compliant with the upcoming service docs
best practices.
2024-10-18 10:10:28 +02:00

44 lines
681 B
Ruby

# frozen_string_literal: true
class Flags::DestroyFlag
include Service::Base
model :flag
policy :not_system
policy :not_used
policy :invalid_access
transaction do
step :destroy
step :log
end
private
def fetch_flag(id:)
Flag.find_by(id: id)
end
def not_system(flag:)
!flag.system?
end
def not_used(flag:)
!flag.used?
end
def invalid_access(guardian:, flag:)
guardian.can_edit_flag?(flag)
end
def destroy(flag:)
flag.destroy!
end
def log(guardian:, flag:)
StaffActionLogger.new(guardian.user).log_custom(
"delete_flag",
flag.slice(:name, :description, :applies_to, :enabled),
)
end
end