Make ban UI more intuitive when a user is already banned

This commit is contained in:
Karina Kwiatek 2021-08-23 09:50:35 +02:00
parent 9a35584284
commit ea62d91014
2 changed files with 40 additions and 19 deletions

View File

@ -49,7 +49,7 @@ class User < ApplicationRecord
has_one :profile, dependent: :destroy
has_one :theme, dependent: :destroy
has_many :user_bans, dependent: :destroy
has_many :bans, class_name: 'UserBan', dependent: :destroy
has_many :banned_users, class_name: 'UserBan',
foreign_key: 'banned_by_id',
dependent: :nullify
@ -224,11 +224,11 @@ class User < ApplicationRecord
# endregion
def banned?
self.user_bans.current.any?
self.bans.current.any?
end
def unban
self.user_bans.current.update!(expires_at: DateTime.now)
self.bans.current.update!(expires_at: DateTime.now)
end
# Bans a user.
@ -237,7 +237,7 @@ class User < ApplicationRecord
# @param reason [String] Reason for the ban. This is displayed to the user.
# @param banned_by [User] User who instated the ban
def ban(duration, duration_unit = 'hours', reason = nil, banned_by = nil)
self.user_bans.create(expires_at: expiry, reason: reason, banned_by: banned_by)
self.bans.create(expires_at: expiry, reason: reason, banned_by: banned_by)
end
def can_export?

View File

@ -1,3 +1,4 @@
- current_ban = user.bans.current.first
.modal.fade#modal-ban{ aria: { hidden: true, labelledby: 'modal-ban-label' }, role: :dialog, tabindex: -1 }
.modal-dialog
.modal-content
@ -9,18 +10,38 @@
%span.sr-only Close
= bootstrap_form_tag(url: '/mod/ban', html: { method: :post, novalidate: :novalidate }) do |f|
= f.hidden_field :user, value: user.screen_name
.modal-body#ban-control-super
= f.check_box :ban, label: t('views.modal.bancontrol.ban'), checked: user.banned?
#ban-controls{ style: user.banned? ? '' : 'display: none' }
= f.check_box :permaban, label: t('views.modal.bancontrol.permanent'), checked: user.permanently_banned?
#ban-controls-time{ style: user.permanently_banned? ? 'display: none' : '' }
= f.text_field :duration, label: '', required: true
.form-check.form-check-inline
= f.radio_button :duration_unit, 'hours', label: 'Hours', checked: true
= f.radio_button :duration_unit, 'days', label: 'Days'
= f.radio_button :duration_unit, 'weeks', label: 'Weeks'
= f.radio_button :duration_unit, 'months', label: 'Months'
= f.text_field :reason, placeholder: t('views.modal.bancontrol.reason'), value: user.ban_reason
.modal-footer
%button.btn.btn-default{ name: 'stop-time', type: :button, data: { dismiss: :modal } }= t 'views.actions.close'
= f.submit t('views.modal.bancontrol.hammertime'), class: 'btn btn-primary', name: 'hammer-time'
- if current_ban.nil?
.modal-body#ban-control-super
= f.check_box :ban, label: t('views.modal.bancontrol.ban'), checked: user.banned?
#ban-controls{ style: user.banned? ? '' : 'display: none' }
= f.check_box :permaban, label: t('views.modal.bancontrol.permanent'), checked: user.permanently_banned?
#ban-controls-time{ style: user.permanently_banned? ? 'display: none' : '' }
= f.text_field :duration, label: '', required: true
.form-check.form-check-inline
= f.radio_button :duration_unit, 'hours', label: 'Hours', checked: true
= f.radio_button :duration_unit, 'days', label: 'Days'
= f.radio_button :duration_unit, 'weeks', label: 'Weeks'
= f.radio_button :duration_unit, 'months', label: 'Months'
= f.text_field :reason, placeholder: t('views.modal.bancontrol.reason'), value: user.ban_reason
.modal-footer
%button.btn.btn-default{ name: 'stop-time', type: :button, data: { dismiss: :modal } }= t 'views.actions.close'
= f.submit t('views.modal.bancontrol.hammertime'), class: 'btn btn-primary', name: 'hammer-time'
- else
= f.hidden_field :ban, value: '0'
.modal-body#ban-control-super
- if current_ban.expires_at.nil?
This user is currently permanently banned for
%strong= current_ban.reason
- else
This user is currently banned until
%strong= current_ban.expires_at
for
%strong= current_ban.reason
- if current_ban.banned_by.present?
%br
This ban was instated by
%strong= current_ban.banned_by.safe_name
on
%strong= current_ban.created_at
.modal-footer
= f.submit 'Unban', class: 'btn btn-primary', name: 'hammer-time'