From e6ce05eb2972c48964365fca02a0860b8ccca79b Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 20 Apr 2018 14:59:47 -0500 Subject: [PATCH] uploads: add more search options for /uploads listing (#3657). --- app/models/upload.rb | 52 ++++++++++++++++++++-- app/views/uploads/_search.html.erb | 8 ++++ app/views/uploads/index.html.erb | 2 + test/functional/uploads_controller_test.rb | 16 +++++-- 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 app/views/uploads/_search.html.erb diff --git a/app/models/upload.rb b/app/models/upload.rb index 756554415..6a4a9a18b 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -391,19 +391,65 @@ class Upload < ApplicationRecord where(:status => "pending") end + def post_tags_match(query) + PostQueryBuilder.new(query).build(self.joins(:post)).reorder("") + end + def search(params) q = super if params[:uploader_id].present? - q = q.uploaded_by(params[:uploader_id].to_i) + q = q.attribute_matches(:uploader_id, params[:uploader_id]) end if params[:uploader_name].present? - q = q.where("uploader_id = (select _.id from users _ where lower(_.name) = ?)", params[:uploader_name].mb_chars.downcase) + q = q.where(uploader_id: User.name_to_id(params[:uploader_name])) end if params[:source].present? - q = q.where("source = ?", params[:source]) + q = q.where(source: params[:source]) + end + + if params[:source_matches].present? + q = q.where("uploads.source LIKE ? ESCAPE E'\\\\'", params[:source_matches].to_escaped_for_sql_like) + end + + if params[:rating].present? + q = q.where(rating: params[:rating]) + end + + if params[:parent_id].present? + q = q.attribute_matches(:rating, params[:parent_id]) + end + + if params[:post_id].present? + q = q.attribute_matches(:post_id, params[:post_id]) + end + + if params[:has_post] == "yes" + q = q.where.not(post_id: nil) + elsif params[:has_post] == "no" + q = q.where(post_id: nil) + end + + if params[:post_tags_match].present? + q = q.post_tags_match(params[:post_tags_match]) + end + + if params[:status].present? + q = q.where("uploads.status LIKE ? ESCAPE E'\\\\'", params[:status].to_escaped_for_sql_like) + end + + if params[:backtrace].present? + q = q.where("uploads.backtrace LIKE ? ESCAPE E'\\\\'", params[:backtrace].to_escaped_for_sql_like) + end + + if params[:tag_string].present? + q = q.where("uploads.tag_string LIKE ? ESCAPE E'\\\\'", params[:tag_string].to_escaped_for_sql_like) + end + + if params[:server].present? + q = q.where(server: params[:server]) end q.apply_default_order(params) diff --git a/app/views/uploads/_search.html.erb b/app/views/uploads/_search.html.erb new file mode 100644 index 000000000..8e1a1cda6 --- /dev/null +++ b/app/views/uploads/_search.html.erb @@ -0,0 +1,8 @@ +<%= simple_form_for(:search, url: uploads_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %> + <%= f.input :uploader_name, label: "Uploader", input_html: { value: params[:search][:uploader_name] } %> + <%= f.input :post_tags_match, label: "Post Tags", input_html: { value: params[:search][:post_tags_match] } %> + <%= f.input :source_matches, label: "Source", input_html: { value: params[:search][:source_matches] } %> + <%= f.input :status, collection: [%w[Completed completed], %w[Processing processing], %w[Pending pending], %w[Duplicate *duplicate*], %w[Error error*]], include_blank: true, selected: params[:search][:status] %> + + <%= f.submit "Search" %> +<% end %> diff --git a/app/views/uploads/index.html.erb b/app/views/uploads/index.html.erb index 775efdf28..786ac64e0 100644 --- a/app/views/uploads/index.html.erb +++ b/app/views/uploads/index.html.erb @@ -1,5 +1,7 @@
+ <%= render "uploads/search" %> + diff --git a/test/functional/uploads_controller_test.rb b/test/functional/uploads_controller_test.rb index 80d2cf522..93502a8d2 100644 --- a/test/functional/uploads_controller_test.rb +++ b/test/functional/uploads_controller_test.rb @@ -63,18 +63,28 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest context "index action" do setup do as_user do - @upload = create(:source_upload) + @upload = create(:source_upload, tag_string: "foo bar") end end should "render" do - get_auth uploads_path, @user + get uploads_path assert_response :success end context "with search parameters" do should "render" do - get_auth uploads_path, @user, params: {:search => {:source => @upload.source}} + search_params = { + uploader_name: @upload.uploader_name, + source_matches: @upload.source, + rating: @upload.rating, + has_post: "yes", + post_tags_match: @upload.tag_string, + status: @upload.status, + server: @upload.server, + } + + get uploads_path, params: { search: search_params } assert_response :success end end