From dff27e3a3ae02e84c7c942f68a978e362e7b9a13 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Sep 2022 20:56:10 -0500 Subject: [PATCH] comments: put search form on same page as search results. * Remove the /comment/search page. Instead put the comment search form on the same page as the search results, so you don't have to go back and forth to update your search. * Add an "Edited?" search option, for finding comments that have been edited. * Add options for sorting by oldest comments and lowest scoring comments. --- app/controllers/comments_controller.rb | 3 --- app/models/comment.rb | 18 ++++++++++++++++- app/views/comments/_index_by_comment.html.erb | 14 +++++++++++++ app/views/comments/_secondary_links.html.erb | 2 +- app/views/comments/search.html.erb | 20 ------------------- app/views/static/site_map.html.erb | 2 +- config/routes.rb | 2 +- test/functional/comments_controller_test.rb | 11 +++++++--- 8 files changed, 42 insertions(+), 30 deletions(-) delete mode 100644 app/views/comments/search.html.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 6b0525c2f..e91765323 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -22,9 +22,6 @@ class CommentsController < ApplicationController end end - def search - end - def new if params[:id] quoted_comment = Comment.find(params[:id]) diff --git a/app/models/comment.rb b/app/models/comment.rb index 666a46ebc..f2f3e84ea 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -35,13 +35,29 @@ class Comment < ApplicationRecord def search(params, current_user) q = search_attributes(params, [:id, :created_at, :updated_at, :is_deleted, :is_sticky, :do_not_bump_post, :body, :score, :post, :creator, :updater], current_user: current_user) + if params[:is_edited].to_s.truthy? + q = q.where("comments.updated_at - comments.created_at > ?", 5.minutes.iso8601) + elsif params[:is_edited].to_s.falsy? + q = q.where("comments.updated_at - comments.created_at <= ?", 5.minutes.iso8601) + end + case params[:order] + when "id_asc" + q = q.order("comments.id ASC") + when "created_at", "created_at_desc" + q = q.order("comments.created_at DESC, comments.id DESC") + when "created_at_asc" + q = q.order("comments.created_at ASC, comments.id ASC") when "post_id", "post_id_desc" q = q.order("comments.post_id DESC, comments.id DESC") when "score", "score_desc" q = q.order("comments.score DESC, comments.id DESC") + when "score_asc" + q = q.order("comments.score ASC, comments.id ASC") when "updated_at", "updated_at_desc" - q = q.order("comments.updated_at DESC") + q = q.order("comments.updated_at DESC, comments.id DESC") + when "updated_at_asc" + q = q.order("comments.updated_at ASC, comments.id ASC") else q = q.apply_default_order(params) end diff --git a/app/views/comments/_index_by_comment.html.erb b/app/views/comments/_index_by_comment.html.erb index 8e58bae78..fe4263818 100644 --- a/app/views/comments/_index_by_comment.html.erb +++ b/app/views/comments/_index_by_comment.html.erb @@ -1,5 +1,19 @@
+ <%= search_form_for(comments_path) do |f| %> + <%= hidden_field_tag "group_by", "comment", :id => "group_by_full" %> + <%= f.input :creator_name, label: "Commenter", input_html: { value: params[:search][:creator_name], "data-autocomplete": "user" } %> + <%= f.input :body_matches, label: "Text", input_html: { value: params[:search][:body_matches] } %> + <%= f.input :post_tags_match, label: "Tags", input_html: { value: params[:search][:post_tags_match], "data-autocomplete": "tag-query" } %> + <%= f.input :score, input_html: { value: params[:search][:score] } %> + <%= f.input :is_edited, label: "Edited?", collection: %w[Yes No], include_blank: true, selected: params[:search][:is_edited] %> + <%= f.input :is_deleted, label: "Deleted?", collection: %w[Yes No], include_blank: true, selected: params[:search][:is_deleted] %> + <%= f.input :is_sticky, label: "Stickied?", collection: %w[Yes No], include_blank: true, selected: params[:search][:is_sticky] %> + <%= f.input :do_not_bump_post, label: "Bumped?", collection: [["Yes", false], ["No", true]], include_blank: true, selected: params[:search][:do_not_bump_post] %> + <%= f.input :order, collection: [["Newest", "id_desc"], ["Oldest", "id_asc"], ["Updated", "updated_at_desc"], ["Score (highest)", "score_desc"], ["Score (lowest)", "score_asc"]], include_blank: true, selected: params[:search][:order] %> + <%= f.submit "Search" %> + <% end %> + <% dtext_data = DText.preprocess(@comments.map(&:body)) %> <% @comments.each do |comment| %> diff --git a/app/views/comments/_secondary_links.html.erb b/app/views/comments/_secondary_links.html.erb index 00ff842b0..b76fdc5a9 100644 --- a/app/views/comments/_secondary_links.html.erb +++ b/app/views/comments/_secondary_links.html.erb @@ -1,7 +1,7 @@ <% content_for(:secondary_links) do %> <%= quick_search_form_for(:body_matches, comments_path, "comments") %> <%= subnav_link_to "Listing", comments_path(:group_by => "post") %> - <%= subnav_link_to "Search", search_comments_path %> + <%= subnav_link_to "Search", comments_path(group_by: "comment") %> <% if policy(CommentVote).can_see_votes? %> <%= subnav_link_to "Votes", comment_votes_path %> <% end %> diff --git a/app/views/comments/search.html.erb b/app/views/comments/search.html.erb deleted file mode 100644 index 0be5c5315..000000000 --- a/app/views/comments/search.html.erb +++ /dev/null @@ -1,20 +0,0 @@ -
- -
- -<%= render "secondary_links" %> diff --git a/app/views/static/site_map.html.erb b/app/views/static/site_map.html.erb index 8eaeb37c1..6e091726e 100644 --- a/app/views/static/site_map.html.erb +++ b/app/views/static/site_map.html.erb @@ -83,7 +83,7 @@
  • Comments

  • <%= link_to("Comments", comments_path) %>
  • -
  • <%= link_to("Search", search_comments_path) %>
  • +
  • <%= link_to("Search", comments_path(group_by: "comment")) %>
  • <%= link_to("Votes", comment_votes_path) %>
  • <%= link_to("RSS", comments_path(:atom)) %>
  • <%= link_to_wiki "Help", "help:comments" %>
  • diff --git a/config/routes.rb b/config/routes.rb index 70582d7d9..0d530a955 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -85,7 +85,7 @@ Rails.application.routes.draw do get "/", action: :index end collection do - get :search + get :search, to: redirect("/comments?group_by=comment") end member do post :undelete diff --git a/test/functional/comments_controller_test.rb b/test/functional/comments_controller_test.rb index f25776309..6d26f2123 100644 --- a/test/functional/comments_controller_test.rb +++ b/test/functional/comments_controller_test.rb @@ -62,7 +62,7 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest context "searching" do setup do - @user_comment = create(:comment, post: @post, score: 10, do_not_bump_post: true, creator: @user) + @user_comment = create(:comment, created_at: 10.minutes.ago, updated_at: 3.minutes.ago, post: @post, score: 10, do_not_bump_post: true, creator: @user) @mod_comment = create(:comment, post: build(:post, tag_string: "touhou"), body: "blah", is_sticky: true, creator: @mod) @deleted_comment = create(:comment, creator: create(:user, name: "deleted"), is_deleted: true, is_sticky: true, do_not_bump_post: true, score: 10, body: "blah") end @@ -76,6 +76,8 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest should respond_to_search(is_sticky: "true").with { @mod_comment } should respond_to_search(is_deleted: "true").with { @deleted_comment } should respond_to_search(do_not_bump_post: "true").with { @user_comment } + should respond_to_search(is_edited: "true").with { @user_comment } + should respond_to_search(is_edited: "false").with { [@deleted_comment, @mod_comment] } should respond_to_search(creator_name: "deleted").with { [] } end @@ -88,6 +90,8 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest should respond_to_search(is_sticky: "true").with { @mod_comment } should respond_to_search(is_deleted: "true").with { @deleted_comment } should respond_to_search(do_not_bump_post: "true").with { @user_comment } + should respond_to_search(is_edited: "true").with { @user_comment } + should respond_to_search(is_edited: "false").with { [@deleted_comment, @mod_comment] } should respond_to_search(creator_name: "deleted").with { @deleted_comment } end @@ -100,6 +104,8 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest should respond_to_search(is_sticky: "true").with { [@deleted_comment, @mod_comment] } should respond_to_search(is_deleted: "true").with { @deleted_comment } should respond_to_search(do_not_bump_post: "true").with { [@deleted_comment, @user_comment] } + should respond_to_search(is_edited: "true").with { @user_comment } + should respond_to_search(is_edited: "false").with { [@deleted_comment, @mod_comment] } should respond_to_search(creator_name: "deleted").with { @deleted_comment } end @@ -131,9 +137,8 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest context "search action" do should "render" do - @comment = create(:comment, post: @post) get search_comments_path - assert_response :success + assert_redirected_to comments_path(group_by: "comment") end end