From ea62d91014d0b1f5644e708cada4a2f26de90dd3 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Mon, 23 Aug 2021 09:50:35 +0200 Subject: [PATCH] Make ban UI more intuitive when a user is already banned --- app/models/user.rb | 8 +++--- app/views/modal/_ban.haml | 51 +++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 2b98d6a8..40b1d329 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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? diff --git a/app/views/modal/_ban.haml b/app/views/modal/_ban.haml index e4e6ec59..d0435f64 100644 --- a/app/views/modal/_ban.haml +++ b/app/views/modal/_ban.haml @@ -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'