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 @@ -
ID | -Comment | -Comment Creator | -Voter | -Signed Up | -Vote | -Created | -Updated | -IP | -|
---|---|---|---|---|---|---|---|---|---|
<%= 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 %> | -
ID | -Post | -Voter | -Signed Up | -Vote | -Created | -Updated | -IP | -|
---|---|---|---|---|---|---|---|---|
<%= 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 %> | -
ID | +<%= type.model_type.capitalize %> | +<%= type.model_type.capitalize %> Creator | +Voter | +Signed Up | +Vote | +Created | +Updated | +IP | +|
---|---|---|---|---|---|---|---|---|---|
<%= 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 %> | +