[Cleanup] Refactor user warnable code

This commit is contained in:
Earlopain 2022-09-26 20:15:35 +02:00
parent 3d59d23508
commit 5139aaf4db
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
9 changed files with 30 additions and 48 deletions

View File

@ -1,28 +1,22 @@
module UserWarnable
extend ActiveSupport::Concern
WARNING_TYPES = {
'warning' => 1,
'record' => 2,
'ban' => 3,
'unmark' => nil
}
included do
scope :user_warned, -> { where('warning_type IS NOT NULL') }
validates :warning_type, inclusion: { :in => [1, 2, 3, nil] }
enum warning_type: {
warning: 1,
record: 2,
ban: 3,
}
scope :user_warned, -> { where("warning_type IS NOT NULL") }
end
def user_warned!(type, user=CurrentUser.id)
unless WARNING_TYPES.has_key?(type)
errors.add(:warning_type, 'invalid warning type')
return
end
update({warning_type: WARNING_TYPES[type], warning_user_id: user})
def user_warned!(type, user)
update({ warning_type: type, warning_user_id: user })
end
def remove_user_warning!
update_columns({warning_type: nil, warning_user_id: nil})
update_columns({ warning_type: nil, warning_user_id: nil })
end
def was_warned?
@ -31,11 +25,11 @@ module UserWarnable
def warning_type_string
case warning_type
when 1
when "warning"
"User received a warning for the contents of this message."
when 2
when "record"
"User received a record for the contents of this message."
when 3
when "ban"
"User was banned for the contents of this message."
else
"[This is a bug with the website. Woo!]"

View File

@ -89,7 +89,7 @@ class BlipsController < ApplicationController
if params[:record_type] == 'unmark'
@blip.remove_user_warning!
else
@blip.user_warned!(params[:record_type])
@blip.user_warned!(params[:record_type], CurrentUser.user)
end
respond_with(@blip)
end

View File

@ -84,7 +84,7 @@ class CommentsController < ApplicationController
if params[:record_type] == 'unmark'
@comment.remove_user_warning!
else
@comment.user_warned!(params[:record_type])
@comment.user_warned!(params[:record_type], CurrentUser.user)
end
respond_with(@comment)
end

View File

@ -74,7 +74,7 @@ class ForumPostsController < ApplicationController
if params[:record_type] == 'unmark'
@forum_post.remove_user_warning!
else
@forum_post.user_warned!(params[:record_type])
@forum_post.user_warned!(params[:record_type], CurrentUser.user)
end
respond_with(@forum_post)
end

View File

@ -4,13 +4,13 @@ $(() => {
$('.item-mark-user-warned').on('click', function(evt) {
evt.preventDefault();
const target = $(evt.target);
const type = target.data('item-type');
const type = target.data('item-route');
const id = target.data('item-id');
const record_type = target.data('record-type');
$.ajax({
type: "POST",
url: `/${type}s/${id}/warning.json`,
url: `/${type}/${id}/warning.json`,
data: {
'record_type': record_type
},

View File

@ -65,14 +65,7 @@
<span><%= link_to_ip blip.creator_ip_addr %></span>
</li>
<li>|</li>
<% if !blip.was_warned? %>
<li>Mark for:</li>
<li><%= tag.a "Warning", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'blip', 'item-id': blip.id, 'record-type': 'warning' } %></li>
<li><%= tag.a "Record", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'blip', 'item-id': blip.id, 'record-type': 'record' } %></li>
<li><%= tag.a "Ban", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'blip', 'item-id': blip.id, 'record-type': 'ban' } %></li>
<% else %>
<li><%= tag.a "Unmark", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'blip', 'item-id': blip.id, 'record-type': 'unmark' } %></li>
<% end %>
<%= render "user_warnable/buttons", model: blip %>
<% end %>
</menu>
</div>

View File

@ -63,14 +63,7 @@
<span><%= link_to_ip comment.creator_ip_addr %></span>
</li>
<li>|</li>
<% if !comment.was_warned? %>
<li>Mark for:</li>
<li><%= tag.a "Warning", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'comment', 'item-id': comment.id, 'record-type': 'warning' } %></li>
<li><%= tag.a "Record", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'comment', 'item-id': comment.id, 'record-type': 'record' } %></li>
<li><%= tag.a "Ban", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'comment', 'item-id': comment.id, 'record-type': 'ban' } %></li>
<% else %>
<li><%= tag.a "Unmark", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'comment', 'item-id': comment.id, 'record-type': 'unmark' } %></li>
<% end %>
<%= render "user_warnable/buttons", model: comment %>
<% end %>
</menu>
<% if comment.editable_by?(CurrentUser.user) %>

View File

@ -71,14 +71,8 @@
<span><%= link_to_ip forum_post.creator_ip_addr %></span>
</li>
<li>|</li>
<% if !forum_post.was_warned? %>
<li>Mark for:</li>
<li><%= tag.a "Warning", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'forum_post', 'item-id': forum_post.id, 'record-type': 'warning' } %></li>
<li><%= tag.a "Record", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'forum_post', 'item-id': forum_post.id, 'record-type': 'record' } %></li>
<li><%= tag.a "Ban", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'forum_post', 'item-id': forum_post.id, 'record-type': 'ban' } %></li>
<% else %>
<li><%= tag.a "Unmark", href: '#', class: 'item-mark-user-warned', data: { 'item-type': 'forum_post', 'item-id': forum_post.id, 'record-type': 'unmark' } %></li>
<% end %>
<%= render "user_warnable/buttons", model: forum_post %>
<li>|</li>
<% end %>
<% if params[:controller] == "forum_posts" %>

View File

@ -0,0 +1,8 @@
<% if model.was_warned? %>
<li><%= tag.a "Unmark", href: "#", class: "item-mark-user-warned", data: { "item-route": model.model_name.route_key, "item-id": model.id, "record-type": "unmark" } %></li>
<% else %>
<li>Mark for:</li>
<li><%= tag.a "Warning", href: "#", class: "item-mark-user-warned", data: { "item-route": model.model_name.route_key, "item-id": model.id, "record-type": "warning" } %></li>
<li><%= tag.a "Record", href: "#", class: "item-mark-user-warned", data: { "item-route": model.model_name.route_key, "item-id": model.id, "record-type": "record" } %></li>
<li><%= tag.a "Ban", href: "#", class: "item-mark-user-warned", data: { "item-route": model.model_name.route_key, "item-id": model.id, "record-type": "ban" } %></li>
<% end %>