From 1a52f9db2b20e74ae942608b3d9902d22605956a Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 23 May 2017 20:59:48 -0500 Subject: [PATCH] wiki pages: add wildcard support to "other names" search. --- app/logical/sources/site.rb | 2 +- app/models/wiki_page.rb | 16 ++++++++++++---- app/views/wiki_pages/search.html.erb | 2 +- test/unit/wiki_page_test.rb | 7 ++++++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/logical/sources/site.rb b/app/logical/sources/site.rb index 364c39380..be9c66592 100644 --- a/app/logical/sources/site.rb +++ b/app/logical/sources/site.rb @@ -54,7 +54,7 @@ module Sources tag end end - WikiPage.other_names_match(untranslated_tags).map{|wiki_page| [wiki_page.title, wiki_page.category_name]} + WikiPage.other_names_equal(untranslated_tags).map{|wiki_page| [wiki_page.title, wiki_page.category_name]} end def to_h diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index a838cf067..aefb64af1 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -40,12 +40,20 @@ class WikiPage < ActiveRecord::Base end end - def other_names_match(names) - names = names.map{|name| name.to_escaped_for_tsquery} - query_sql = names.join(" | ") + def other_names_equal(names) + query_sql = names.map(&:to_escaped_for_tsquery).join(" | ") where("other_names_index @@ to_tsquery('danbooru', E?)", query_sql) end + def other_names_match(name) + if name =~ /\*/ + subquery = WikiPage.from("unnest(string_to_array(other_names, ' ')) AS other_name").where("other_name ILIKE ?", name.to_escaped_for_sql_like) + where(id: subquery) + else + other_names_equal([name]) + end + end + def search(params = {}) q = where("true") params = {} if params.blank? @@ -63,7 +71,7 @@ class WikiPage < ActiveRecord::Base end if params[:other_names_match].present? - q = q.other_names_match(params[:other_names_match].split(" ")) + q = q.other_names_match(params[:other_names_match]) end if params[:creator_name].present? diff --git a/app/views/wiki_pages/search.html.erb b/app/views/wiki_pages/search.html.erb index 9e7555e85..fee3fcee7 100644 --- a/app/views/wiki_pages/search.html.erb +++ b/app/views/wiki_pages/search.html.erb @@ -4,7 +4,7 @@ <%= search_field "title", :hint => "Use * for wildcard searches" %> <%= search_field "creator_name" %> <%= search_field "body_matches", :label => "Body" %> - <%= search_field "other_names_match", :label => "Other names" %> + <%= search_field "other_names_match", :label => "Other names", :hint => "Use * for wildcard searches" %>
diff --git a/test/unit/wiki_page_test.rb b/test/unit/wiki_page_test.rb index 3c91e9410..cadcbe57b 100644 --- a/test/unit/wiki_page_test.rb +++ b/test/unit/wiki_page_test.rb @@ -47,7 +47,7 @@ class WikiPageTest < ActiveSupport::TestCase setup do @user = FactoryGirl.create(:user) CurrentUser.user = @user - @wiki_page = FactoryGirl.create(:wiki_page, :title => "HOT POTATO") + @wiki_page = FactoryGirl.create(:wiki_page, :title => "HOT POTATO", :other_names => "foo*bar baz") end should "not allow the is_locked attribute to be updated" do @@ -67,6 +67,11 @@ class WikiPageTest < ActiveSupport::TestCase assert_equal("hot_potato", matches.first.title) end + should "search other names with wildcards" do + matches = WikiPage.search(other_names_match: "fo*") + assert_equal([@wiki_page.id], matches.map(&:id)) + end + should "create versions" do assert_difference("WikiPageVersion.count") do @wiki_page = FactoryGirl.create(:wiki_page, :title => "xxx")