[Votes] Unify index view code

This commit is contained in:
Earlopain 2022-10-08 23:59:33 +02:00
parent 696ae62ef6
commit c2d224cba9
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
6 changed files with 96 additions and 162 deletions

View File

@ -7,6 +7,10 @@ class CommentVote < UserVote
CommentVote.where(comment_id: comment_ids, user_id: user_id).index_by(&:comment_id)
end
def self.model_creator_column
:creator
end
def validate_user_can_vote
allowed = user.can_comment_vote_with_reason
if allowed != true
@ -24,17 +28,4 @@ class CommentVote < UserVote
errors.add :base, "You cannot vote on sticky comments"
end
end
def self.search(params)
q = super
if params[:comment_creator_name].present? && allow_complex_parameters?(params)
comment_creator_id = User.name_to_id(params[:comment_creator_name])
if comment_creator_id
q = q.joins(:comment).where("comments.creator_id = ?", comment_creator_id)
else
q = q.none
end
end
q
end
end

View File

@ -1,6 +1,10 @@
class PostVote < UserVote
validate :validate_user_can_vote
def self.model_creator_column
:uploader
end
def validate_user_can_vote
if user.younger_than(3.days) && score == -1
errors.add(:user, "must be 3 days old to downvote posts")

View File

@ -38,10 +38,6 @@ class UserVote < ApplicationRecord
end
module SearchMethods
def allow_complex_parameters?(params)
(params.keys & ["#{model_type}_id", "user_name", "user_id"]).any?
end
def search(params)
q = super
@ -62,7 +58,18 @@ class UserVote < ApplicationRecord
q = q.where("user_id = ?", params[:user_id].to_i)
end
if allow_complex_parameters?(params)
allow_complex_params = (params.keys & ["#{model_type}_id", "user_name", "user_id"]).any?
if allow_complex_params
if params[:"#{model_type}_creator_name"].present?
creator_id = User.name_to_id(params[:"#{model_type}_creator_name"])
if creator_id
q = q.joins(model_type).where(model_type => { "#{model_creator_column}_id": creator_id })
else
q = q.none
end
end
if params[:timeframe].present?
q = q.where("#{table_name}.updated_at >= ?", params[:timeframe].to_i.days.ago)
end
@ -81,7 +88,7 @@ class UserVote < ApplicationRecord
end
end
if params[:order] == "ip_addr" && allow_complex_parameters?(params)
if params[:order] == "ip_addr" && allow_complex_params
q = q.order(:user_ip_addr)
else
q = q.apply_default_order(params)

View File

@ -1,73 +1 @@
<div id="c-comment-votes">
<div id="a-index">
<%# path is a string here because of duplicate routes %>
<%= form_search(path: "comment_votes") do |f| %>
<%= f.input :user_name, label: "Voter Username", autocomplete: "user" %>
<%= f.input :comment_id, label: "Comment ID" %>
<br>
<%= f.input :comment_creator_name, label: "Comment Creator Username", autocomplete: "user" %>
<%= f.input :timeframe, label: "Timeframe", include_blank: true, collection: [["Last Week", "7"], ["Last Month", "30"], ["Last Three Months", "90"], ["Last Year", "360"]] %>
<%= f.input :score, label: "Type", include_blank: true, collection: [["Upvote", "1"], ["Locked", "0"], ["Downvote", "-1"]] %>
<%= f.input :user_ip_addr, label: "IP Address" %>
<%= f.input :duplicates_only, label: "Duplicates Only", as: :boolean %>
<%= f.input :order, collection: [["Created", "id"], ["IP Address", "ip_addr"]] %>
<%= f.submit "Search" %>
<% end %>
<table class="striped" id='votes'>
<thead>
<tr>
<th>ID</th>
<th>Comment</th>
<th>Comment Creator</th>
<th>Voter</th>
<th>Email</th>
<th>Signed Up</th>
<th>Vote</th>
<th>Created</th>
<th>Updated</th>
<th>IP</th>
</tr>
</thead>
<tbody>
<% @comment_votes.each do |vote| %>
<tr id="r<%= vote.id %>">
<td><%= vote.id %></td>
<td><%= link_to vote.comment_id, comment_path(vote.comment) %></td>
<td><%= mod_link_to_user vote.comment.creator, :negative %></td>
<td><%= mod_link_to_user vote.user, :negative %></td>
<td><%= vote.user.email %>
<td title="Signed up at <%= vote.user.created_at.strftime("%c") %>"><%= time_ago_in_words(vote.user.created_at) %> ago
<td>
<% if vote.is_positive? %><span class='greentext'>Up</span>
<% elsif vote.is_locked? %><span class='yellowtext'>Locked</span>
<% else %><span class='redtext'>Down</span>
<% end %></td>
<td title="Created at <%= vote.created_at.strftime("%c") %>"><%= time_ago_in_words(vote.created_at) %> ago
</td>
<td title="Updated at <%= vote.updated_at.strftime("%c") %>"><%= time_ago_in_words(vote.updated_at) %> ago
</td>
<td><%= link_to_ip vote.user_ip_addr %></td>
</tr>
<% end %>
</tbody>
</table>
<br/>
<%= tag.button "Select All", id: "select-all-votes" %><br/>
<%= tag.button "Lock Votes", id: "lock-votes" %> Set the votes to 0, preventing the user
from voting on the image again<br/>
<%= tag.button "Delete Votes", id: "delete-votes" %> Remove the votes
<%= javascript_tag nonce: true do -%>
new Danbooru.VoteManager('comment');
<% end -%>
<div id="paginator">
<%= numbered_paginator(@comment_votes) %>
</div>
</div>
</div>
<% content_for(:page_title) do %>
Comment Votes
<% end %>
<%= render "user_votes/common_index", type: CommentVote, votes: @comment_votes %>

View File

@ -1,70 +1 @@
<div id="c-post-votes">
<div id="a-index">
<%# path is a string here because of duplicate routes %>
<%= form_search(path: "post_votes") do |f| %>
<%= f.input :user_name, label: "Username", autocomplete: "user" %>
<%= f.input :post_id, label: "Post ID" %>
<br>
<%= f.input :timeframe, label: "Timeframe", include_blank: true, collection: [["Last Week", "7"], ["Last Month", "30"], ["Last Three Months", "90"], ["Last Year", "360"]] %>
<%= f.input :score, label: "Type", include_blank: true, collection: [["Upvote", "1"], ["Locked", "0"], ["Downvote", "-1"]] %>
<%= f.input :user_ip_addr, label: "IP Address" %>
<%= f.input :duplicates_only, label: "Duplicates Only", as: :boolean %>
<%= f.input :order, collection: [["Created", "id"], ["IP Address", "ip_addr"]] %>
<%= f.submit "Search" %>
<% end %>
<table class='striped' id='votes'>
<thead>
<tr>
<th>ID</th>
<th>Post</th>
<th>Voter</th>
<th>Email</th>
<th>Signed Up</th>
<th>Vote</th>
<th>Created</th>
<th>Updated</th>
<th>IP</th>
</tr>
</thead>
<tbody>
<% @post_votes.each do |vote| %>
<tr id="r<%= vote.id %>">
<td><%= vote.id %></td>
<td><%= link_to vote.post_id, post_path(id: vote.post_id) %></td>
<td><%= mod_link_to_user vote.user, :negative %></td>
<td><%= vote.user.email %>
<td title="Signed up at <%= vote.user.created_at.strftime("%c") %>"><%= time_ago_in_words(vote.user.created_at) %> ago
<td>
<% if vote.is_positive? %><span class='greentext'>Up</span>
<% elsif vote.is_locked? %><span class='yellowtext'>Locked</span>
<% else %><span class='redtext'>Down</span>
<% end %></td>
<td title="Created at <%= vote.created_at.strftime("%c") %>"><%= time_ago_in_words(vote.created_at) %> ago
</td>
<td title="Updated at <%= vote.updated_at.strftime("%c") %>"><%= time_ago_in_words(vote.updated_at) %> ago
</td>
<td><%= link_to_ip vote.user_ip_addr %></td>
</tr>
</tbody>
<% end %>
</table>
<br/>
<%= tag.button "Select All", id: "select-all-votes" %><br/>
<%= tag.button "Lock Votes", id: "lock-votes" %> Set the votes to 0, preventing the user from
voting on the image again<br/>
<%= tag.button "Delete Votes", id: "delete-votes" %> Remove the votes
<%= javascript_tag nonce: true do -%>
new Danbooru.VoteManager('post');
<% end -%>
<div id="paginator">
<%= numbered_paginator(@post_votes) %>
</div>
</div>
</div>
<% content_for(:page_title) do %>
Post Votes
<% end %>
<%= render "user_votes/common_index", type: PostVote, votes: @post_votes %>

View File

@ -0,0 +1,73 @@
<div id="c-<%= type.model_name.plural %>">
<div id="a-index">
<%# path is a string here because of duplicate routes %>
<%= form_search(path: type.model_name.route_key) do |f| %>
<%= f.input :user_name, label: "Voter Username", autocomplete: "user" %>
<%= f.input :"#{type.model_type}_id", label: "#{type.model_type.capitalize} ID" %>
<br>
<%= f.input :"#{type.model_type}_creator_name", label: "#{type.model_type.capitalize} Creator Username", autocomplete: "user" %>
<%= f.input :timeframe, label: "Timeframe", include_blank: true, collection: [["Last Week", "7"], ["Last Month", "30"], ["Last Three Months", "90"], ["Last Year", "360"]] %>
<%= f.input :score, label: "Type", include_blank: true, collection: [["Upvote", "1"], ["Locked", "0"], ["Downvote", "-1"]] %>
<%= f.input :user_ip_addr, label: "IP Address" %>
<%= f.input :duplicates_only, label: "Duplicates Only", as: :boolean %>
<%= f.input :order, collection: [["Created", "id"], ["IP Address", "ip_addr"]] %>
<%= f.submit "Search" %>
<% end %>
<table class="striped" id='votes'>
<thead>
<tr>
<th>ID</th>
<th><%= type.model_type.capitalize %></th>
<th><%= type.model_type.capitalize %> Creator</th>
<th>Voter</th>
<th>Email</th>
<th>Signed Up</th>
<th>Vote</th>
<th>Created</th>
<th>Updated</th>
<th>IP</th>
</tr>
</thead>
<tbody>
<% votes.each do |vote| %>
<tr id="r<%= vote.id %>">
<td><%= vote.id %></td>
<td><%= link_to vote.send("#{type.model_type}_id"), vote.send(type.model_type) %></td>
<td><%= mod_link_to_user vote.send(type.model_type).send(type.model_creator_column), :negative %></td>
<td><%= mod_link_to_user vote.user, :negative %></td>
<td><%= vote.user.email %>
<td title="Signed up at <%= vote.user.created_at.strftime("%c") %>"><%= time_ago_in_words(vote.user.created_at) %> ago
<td>
<% if vote.is_positive? %><span class='greentext'>Up</span>
<% elsif vote.is_locked? %><span class='yellowtext'>Locked</span>
<% else %><span class='redtext'>Down</span>
<% end %></td>
<td title="Created at <%= vote.created_at.strftime("%c") %>"><%= time_ago_in_words(vote.created_at) %> ago
</td>
<td title="Updated at <%= vote.updated_at.strftime("%c") %>"><%= time_ago_in_words(vote.updated_at) %> ago
</td>
<td><%= link_to_ip vote.user_ip_addr %></td>
</tr>
<% end %>
</tbody>
</table>
<br/>
<%= tag.button "Select All", id: "select-all-votes" %><br/>
<%= tag.button "Lock Votes", id: "lock-votes" %> Set the votes to 0, preventing the user
from voting on the <%= type.model_type %> again<br/>
<%= tag.button "Delete Votes", id: "delete-votes" %> Remove the votes
<%= javascript_tag nonce: true do -%>
new Danbooru.VoteManager('<%= type.model_type %>');
<% end -%>
<div id="paginator">
<%= numbered_paginator(votes) %>
</div>
</div>
</div>
<% content_for(:page_title) do %>
<%= type.model_name.plural.titleize %>
<% end %>