From 4d9eae0038098f3680ab4f76e53ac8b632d577f5 Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Sat, 28 Mar 2020 04:29:12 +0000 Subject: [PATCH] Add additional advanced search operators - Tag matches allows a user to search for a single tag -- Since testing for multiple tags would require converting the "tags" string to an array which would most likely fail even for single tags - Is new for quick searching of uploads or not uploads --- app/models/post_version.rb | 17 +++++++++++++++++ app/views/post_versions/search.html.erb | 2 ++ 2 files changed, 19 insertions(+) diff --git a/app/models/post_version.rb b/app/models/post_version.rb index 13fae86bf..701742d67 100644 --- a/app/models/post_version.rb +++ b/app/models/post_version.rb @@ -32,6 +32,13 @@ class PostVersion < ApplicationRecord end end + def tag_matches(string) + tag = string.split(/\S+/)[0] + return all if tag.nil? + tag = "*#{tag}*" unless tag =~ /\*/ + where_ilike(:tags, tag) + end + def search(params) q = super q = q.search_attributes(params, :updater_id, :post_id, :tags, :added_tags, :removed_tags, :rating, :rating_changed, :parent_id, :parent_changed, :source, :source_changed, :version) @@ -40,10 +47,20 @@ class PostVersion < ApplicationRecord q = q.changed_tags_include_all(params[:changed_tags].scan(/[^[:space:]]+/)) end + if params[:tag_matches] + q = q.tag_matches(params[:tag_matches]) + end + if params[:updater_name].present? q = q.where(updater_id: User.name_to_id(params[:updater_name])) end + if params[:is_new].to_s.truthy? + q = q.where(version: 1) + elsif params[:is_new].to_s.falsy? + q = q.where("version != 1") + end + q.apply_default_order(params) end end diff --git a/app/views/post_versions/search.html.erb b/app/views/post_versions/search.html.erb index 0413edc9b..926ed4ca9 100644 --- a/app/views/post_versions/search.html.erb +++ b/app/views/post_versions/search.html.erb @@ -12,7 +12,9 @@ <%= f.input :parent_id %> <%= f.input :rating %> <%= f.input :source_ilike, label: "Source", hint: "Use * for wildcard" %> + <%= f.input :tag_matches, hint: "Use * for wildcard" %> <%= f.input :version %> + <%= f.input :is_new, label: "Include uploads?", collection: [["Yes", nil], ["No", false], ["Only", true]], include_blank: false, hint: "I.e. the 1st versions of posts" %> <%= f.input :rating_changed, as: :select %> <%= f.input :parent_changed, as: :select %> <%= f.input :source_changed, as: :select %>