mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: created edit and delete flags (#27484)
Allow admins to create edit and delete flags.
This commit is contained in:
committed by
GitHub
parent
a86590ffd6
commit
c3fadc7330
58
app/services/flags/create_flag.rb
Normal file
58
app/services/flags/create_flag.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Flags::CreateFlag
|
||||
include Service::Base
|
||||
|
||||
contract
|
||||
policy :invalid_access
|
||||
model :flag, :instantiate_flag
|
||||
|
||||
transaction do
|
||||
step :create
|
||||
step :log
|
||||
end
|
||||
|
||||
class Contract
|
||||
attribute :name, :string
|
||||
attribute :description, :string
|
||||
attribute :enabled, :boolean
|
||||
attribute :applies_to
|
||||
validates :name, presence: true
|
||||
validates :description, presence: true
|
||||
validates :name, length: { maximum: Flag::MAX_NAME_LENGTH }
|
||||
validates :description, length: { maximum: Flag::MAX_DESCRIPTION_LENGTH }
|
||||
validates :applies_to, inclusion: { in: Flag.valid_applies_to_types }, allow_nil: false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def instantiate_flag(name:, description:, applies_to:, enabled:)
|
||||
Flag.new(
|
||||
name: name,
|
||||
description: description,
|
||||
applies_to: applies_to,
|
||||
enabled: enabled,
|
||||
notify_type: true,
|
||||
)
|
||||
end
|
||||
|
||||
def invalid_access(guardian:)
|
||||
guardian.can_create_flag?
|
||||
end
|
||||
|
||||
def create(flag:)
|
||||
flag.save!
|
||||
end
|
||||
|
||||
def log(guardian:, flag:)
|
||||
StaffActionLogger.new(guardian.user).log_custom(
|
||||
"create_flag",
|
||||
{
|
||||
name: flag.name,
|
||||
description: flag.description,
|
||||
applies_to: flag.applies_to,
|
||||
enabled: flag.enabled,
|
||||
},
|
||||
)
|
||||
end
|
||||
end
|
||||
49
app/services/flags/destroy_flag.rb
Normal file
49
app/services/flags/destroy_flag.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
# 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(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",
|
||||
{
|
||||
name: flag.name,
|
||||
description: flag.description,
|
||||
applies_to: flag.applies_to,
|
||||
enabled: flag.enabled,
|
||||
},
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
VALID_DIRECTIONS = %w[up down]
|
||||
|
||||
class ReorderFlag
|
||||
class Flags::ReorderFlag
|
||||
include Service::Base
|
||||
|
||||
contract
|
||||
@@ -1,6 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ToggleFlag
|
||||
class Flags::ToggleFlag
|
||||
include Service::Base
|
||||
|
||||
contract
|
||||
62
app/services/flags/update_flag.rb
Normal file
62
app/services/flags/update_flag.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Flags::UpdateFlag
|
||||
include Service::Base
|
||||
|
||||
contract
|
||||
model :flag
|
||||
policy :not_system
|
||||
policy :not_used
|
||||
policy :invalid_access
|
||||
|
||||
transaction do
|
||||
step :update
|
||||
step :log
|
||||
end
|
||||
|
||||
class Contract
|
||||
attribute :name, :string
|
||||
attribute :description, :string
|
||||
attribute :enabled, :boolean
|
||||
attribute :applies_to
|
||||
validates :name, presence: true
|
||||
validates :description, presence: true
|
||||
validates :name, length: { maximum: Flag::MAX_NAME_LENGTH }
|
||||
validates :description, length: { maximum: Flag::MAX_DESCRIPTION_LENGTH }
|
||||
validates :applies_to, inclusion: { in: Flag.valid_applies_to_types }, allow_nil: false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_flag(id:)
|
||||
Flag.find(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 update(flag:, name:, description:, applies_to:, enabled:)
|
||||
flag.update!(name: name, description: description, applies_to: applies_to, enabled: enabled)
|
||||
end
|
||||
|
||||
def log(guardian:, flag:)
|
||||
StaffActionLogger.new(guardian.user).log_custom(
|
||||
"update_flag",
|
||||
{
|
||||
name: flag.name,
|
||||
description: flag.description,
|
||||
applies_to: flag.applies_to,
|
||||
enabled: flag.enabled,
|
||||
},
|
||||
)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user