diff --git a/app/models/comment_vote.rb b/app/models/comment_vote.rb index 3ee1e5698..c2f6fcb61 100644 --- a/app/models/comment_vote.rb +++ b/app/models/comment_vote.rb @@ -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 diff --git a/app/models/post_vote.rb b/app/models/post_vote.rb index aae07494c..4c00c207f 100644 --- a/app/models/post_vote.rb +++ b/app/models/post_vote.rb @@ -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") diff --git a/app/models/user_vote.rb b/app/models/user_vote.rb index 9c4e1c0de..b7fb9c871 100644 --- a/app/models/user_vote.rb +++ b/app/models/user_vote.rb @@ -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) diff --git a/app/views/comment_votes/index.html.erb b/app/views/comment_votes/index.html.erb index adb8fb790..e17823136 100644 --- a/app/views/comment_votes/index.html.erb +++ b/app/views/comment_votes/index.html.erb @@ -1,73 +1 @@ -
-
- <%# 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" %> -
- <%= 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 %> - - - - - - - - - - - - - - - - - - <% @comment_votes.each do |vote| %> - - - - - - - - - - - <% end %> - -
IDCommentComment CreatorVoterEmailSigned UpVoteCreatedUpdatedIP
<%= vote.id %><%= link_to vote.comment_id, comment_path(vote.comment) %><%= mod_link_to_user vote.comment.creator, :negative %><%= mod_link_to_user vote.user, :negative %><%= vote.user.email %> - "><%= time_ago_in_words(vote.user.created_at) %> ago - - <% if vote.is_positive? %>Up - <% elsif vote.is_locked? %>Locked - <% else %>Down - <% end %>"><%= time_ago_in_words(vote.created_at) %> ago - "><%= time_ago_in_words(vote.updated_at) %> ago - <%= link_to_ip vote.user_ip_addr %>
-
- <%= tag.button "Select All", id: "select-all-votes" %>
- <%= tag.button "Lock Votes", id: "lock-votes" %> Set the votes to 0, preventing the user - from voting on the image again
- <%= tag.button "Delete Votes", id: "delete-votes" %> Remove the votes - - <%= javascript_tag nonce: true do -%> - new Danbooru.VoteManager('comment'); - <% end -%> - -
- <%= numbered_paginator(@comment_votes) %> -
-
-
- -<% content_for(:page_title) do %> - Comment Votes -<% end %> +<%= render "user_votes/common_index", type: CommentVote, votes: @comment_votes %> diff --git a/app/views/post_votes/index.html.erb b/app/views/post_votes/index.html.erb index 4ce77573e..04e4f22a9 100644 --- a/app/views/post_votes/index.html.erb +++ b/app/views/post_votes/index.html.erb @@ -1,70 +1 @@ -
-
- <%# 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" %> -
- <%= 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 %> - - - - - - - - - - - - - - - - - <% @post_votes.each do |vote| %> - - - - - - - - - - - <% end %> -
IDPostVoterEmailSigned UpVoteCreatedUpdatedIP
<%= vote.id %><%= link_to vote.post_id, post_path(id: vote.post_id) %><%= mod_link_to_user vote.user, :negative %><%= vote.user.email %> - "><%= time_ago_in_words(vote.user.created_at) %> ago - - <% if vote.is_positive? %>Up - <% elsif vote.is_locked? %>Locked - <% else %>Down - <% end %>"><%= time_ago_in_words(vote.created_at) %> ago - "><%= time_ago_in_words(vote.updated_at) %> ago - <%= link_to_ip vote.user_ip_addr %>
-
- <%= tag.button "Select All", id: "select-all-votes" %>
- <%= tag.button "Lock Votes", id: "lock-votes" %> Set the votes to 0, preventing the user from - voting on the image again
- <%= tag.button "Delete Votes", id: "delete-votes" %> Remove the votes - - <%= javascript_tag nonce: true do -%> - new Danbooru.VoteManager('post'); - <% end -%> - -
- <%= numbered_paginator(@post_votes) %> -
-
-
- -<% content_for(:page_title) do %> - Post Votes -<% end %> +<%= render "user_votes/common_index", type: PostVote, votes: @post_votes %> diff --git a/app/views/user_votes/_common_index.html.erb b/app/views/user_votes/_common_index.html.erb new file mode 100644 index 000000000..0056e6f91 --- /dev/null +++ b/app/views/user_votes/_common_index.html.erb @@ -0,0 +1,73 @@ +
+
+ <%# 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" %> +
+ <%= 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 %> + + + + + + + + + + + + + + + + + + <% votes.each do |vote| %> + + + + + + + + + + + <% end %> + +
ID<%= type.model_type.capitalize %><%= type.model_type.capitalize %> CreatorVoterEmailSigned UpVoteCreatedUpdatedIP
<%= vote.id %><%= link_to vote.send("#{type.model_type}_id"), vote.send(type.model_type) %><%= mod_link_to_user vote.send(type.model_type).send(type.model_creator_column), :negative %><%= mod_link_to_user vote.user, :negative %><%= vote.user.email %> + "><%= time_ago_in_words(vote.user.created_at) %> ago + + <% if vote.is_positive? %>Up + <% elsif vote.is_locked? %>Locked + <% else %>Down + <% end %>"><%= time_ago_in_words(vote.created_at) %> ago + "><%= time_ago_in_words(vote.updated_at) %> ago + <%= link_to_ip vote.user_ip_addr %>
+
+ <%= tag.button "Select All", id: "select-all-votes" %>
+ <%= tag.button "Lock Votes", id: "lock-votes" %> Set the votes to 0, preventing the user + from voting on the <%= type.model_type %> again
+ <%= tag.button "Delete Votes", id: "delete-votes" %> Remove the votes + + <%= javascript_tag nonce: true do -%> + new Danbooru.VoteManager('<%= type.model_type %>'); + <% end -%> + +
+ <%= numbered_paginator(votes) %> +
+
+
+ +<% content_for(:page_title) do %> + <%= type.model_name.plural.titleize %> +<% end %>