Add ability to reorder server rules from admin interface (#34737)

This commit is contained in:
Claire 2025-05-20 14:49:11 +02:00 committed by GitHub
parent 8ed0408adb
commit d7cb6068b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 97 additions and 2 deletions

View file

@ -50,6 +50,22 @@ module Admin
redirect_to admin_rules_path
end
def move_up
authorize @rule, :update?
@rule.move!(-1)
redirect_to admin_rules_path
end
def move_down
authorize @rule, :update?
@rule.move!(+1)
redirect_to admin_rules_path
end
private
def set_rule

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M440-800v487L216-537l-56 57 320 320 320-320-56-57-224 224v-487h-80Z"/></svg>

After

Width:  |  Height:  |  Size: 173 B

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M440-800v487L216-537l-56 57 320 320 320-320-56-57-224 224v-487h-80Z"/></svg>

After

Width:  |  Height:  |  Size: 173 B

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M440-160v-487L216-423l-56-57 320-320 320 320-56 57-224-224v487h-80Z"/></svg>

After

Width:  |  Height:  |  Size: 173 B

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M440-160v-487L216-423l-56-57 320-320 320 320-56 57-224-224v487h-80Z"/></svg>

After

Width:  |  Height:  |  Size: 173 B

View file

@ -1127,6 +1127,15 @@ a.name-tag,
}
}
.rule-actions {
display: flex;
flex-direction: column;
a.table-action-link {
padding-inline-start: 0;
}
}
.dashboard__counters.admin-account-counters {
margin-top: 10px;
}

View file

@ -22,4 +22,18 @@ class Rule < ApplicationRecord
validates :text, presence: true, length: { maximum: TEXT_SIZE_LIMIT }
scope :ordered, -> { kept.order(priority: :asc, id: :asc) }
def move!(offset)
rules = Rule.ordered.to_a
position = rules.index(self)
rules.delete_at(position)
rules.insert(position + offset, self)
transaction do
rules.each.with_index do |rule, index|
rule.update!(priority: index)
end
end
end
end

View file

@ -7,5 +7,7 @@
.announcements-list__item__meta
= rule.hint
%div
.rule-actions
= table_link_to 'arrow_upward', t('admin.rules.move_up'), move_up_admin_rule_path(rule), method: :post if can?(:update, rule) && !rule_iteration.first?
= table_link_to 'delete', t('admin.rules.delete'), admin_rule_path(rule), method: :delete, data: { confirm: t('admin.accounts.are_you_sure') } if can?(:destroy, rule)
= table_link_to 'arrow_downward', t('admin.rules.move_down'), move_down_admin_rule_path(rule), method: :post if can?(:update, rule) && !rule_iteration.last?