diff --git a/app/controllers/pools_controller.rb b/app/controllers/pools_controller.rb index a48cb5963..8e36babcb 100644 --- a/app/controllers/pools_controller.rb +++ b/app/controllers/pools_controller.rb @@ -84,8 +84,8 @@ class PoolsController < ApplicationController private def item_matches_params(pool) - if params[:search][:name_matches] - Pool.normalize_name_for_search(pool.name) == Pool.normalize_name_for_search(params[:search][:name_matches]) + if params[:search][:name_contains] + Pool.normalize_name_for_search(pool.name) == Pool.normalize_name_for_search(params[:search][:name_contains]) else true end diff --git a/app/logical/autocomplete_service.rb b/app/logical/autocomplete_service.rb index 0b0fa4d48..d86edba71 100644 --- a/app/logical/autocomplete_service.rb +++ b/app/logical/autocomplete_service.rb @@ -296,8 +296,7 @@ class AutocompleteService # @param string [String] the name of the pool # @return [Array] the autocomplete results def autocomplete_pool(string) - string = "*" + string + "*" unless string.include?("*") - pools = Pool.undeleted.name_matches(string).search(order: "post_count").limit(limit) + pools = Pool.undeleted.name_contains(string).search(order: "post_count").limit(limit) pools.map do |pool| { type: "pool", label: pool.pretty_name, value: pool.name, id: pool.id, post_count: pool.post_count, category: pool.category } @@ -308,8 +307,7 @@ class AutocompleteService # @param string [String] the name of the favgroup # @return [Array] the autocomplete results def autocomplete_favorite_group(string) - string = "*" + string + "*" unless string.include?("*") - favgroups = FavoriteGroup.visible(current_user).where(creator: current_user).name_matches(string).search(order: "post_count").limit(limit) + favgroups = FavoriteGroup.visible(current_user).where(creator: current_user).name_contains(string).search(order: "post_count").limit(limit) favgroups.map do |favgroup| { label: favgroup.pretty_name, value: favgroup.name, post_count: favgroup.post_count } diff --git a/app/models/favorite_group.rb b/app/models/favorite_group.rb index 63ef60cc0..af4cf28b8 100644 --- a/app/models/favorite_group.rb +++ b/app/models/favorite_group.rb @@ -25,7 +25,7 @@ class FavoriteGroup < ApplicationRecord where_array_includes_any(:post_ids, [post_id]) end - def name_matches(name) + def name_contains(name) name = normalize_name(name) name = "*#{name}*" unless name =~ /\*/ where_ilike(:name, name) @@ -44,8 +44,8 @@ class FavoriteGroup < ApplicationRecord def search(params) q = search_attributes(params, :id, :created_at, :updated_at, :name, :is_public, :post_ids, :creator) - if params[:name_matches].present? - q = q.name_matches(params[:name_matches]) + if params[:name_contains].present? + q = q.name_contains(params[:name_contains]) end case params[:order] diff --git a/app/models/pool.rb b/app/models/pool.rb index 43b885b78..b788897b9 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -21,7 +21,7 @@ class Pool < ApplicationRecord scope :collection, -> { where(category: "collection") } module SearchMethods - def name_matches(name) + def name_contains(name) name = normalize_name_for_search(name) name = "*#{name}*" unless name =~ /\*/ where_ilike(:name, name) @@ -44,8 +44,8 @@ class Pool < ApplicationRecord q = q.post_tags_match(params[:post_tags_match]) end - if params[:name_matches].present? - q = q.name_matches(params[:name_matches]) + if params[:name_contains].present? + q = q.name_contains(params[:name_contains]) end if params[:linked_to].present? diff --git a/app/models/pool_version.rb b/app/models/pool_version.rb index bed95ffe4..ecb263404 100644 --- a/app/models/pool_version.rb +++ b/app/models/pool_version.rb @@ -27,7 +27,7 @@ class PoolVersion < ApplicationRecord where_array_includes_any(:added_post_ids, [post_id]).or(where_array_includes_any(:removed_post_ids, [post_id])) end - def name_matches(name) + def name_contains(name) name = normalize_name_for_search(name) name = "*#{name}*" unless name =~ /\*/ where_ilike(:name, name) @@ -40,8 +40,8 @@ class PoolVersion < ApplicationRecord q = q.for_post_id(params[:post_id].to_i) end - if params[:name_matches].present? - q = q.name_matches(params[:name_matches]) + if params[:name_contains].present? + q = q.name_contains(params[:name_contains]) end if params[:updater_name].present? diff --git a/app/models/post.rb b/app/models/post.rb index 00e8bb69d..fd402b73e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1241,7 +1241,7 @@ class Post < ApplicationRecord when "collection" where(id: Pool.collection.select("unnest(post_ids)")) when /\*/ - where(id: Pool.name_matches(pool_name).select("unnest(post_ids)")) + where(id: Pool.name_contains(pool_name).select("unnest(post_ids)")) else where(id: Pool.named(pool_name).select("unnest(post_ids)")) end diff --git a/app/views/favorite_groups/index.html.erb b/app/views/favorite_groups/index.html.erb index 686b3a0ae..ebf894b08 100644 --- a/app/views/favorite_groups/index.html.erb +++ b/app/views/favorite_groups/index.html.erb @@ -1,7 +1,7 @@
<%= search_form_for(favorite_groups_path) do |f| %> - <%= f.input :name_matches, label: "Name", input_html: { value: params.dig(:search, :name_matches), "data-autocomplete": "favorite-group" } %> + <%= f.input :name_contains, label: "Name", input_html: { value: params.dig(:search, :name_contains), "data-autocomplete": "favorite-group" } %> <%= f.input :creator_name, label: "Creator", input_html: { value: params.dig(:search, :creator_name), "data-autocomplete": "user" } %> <%= f.input :order, collection: [%w[Created created_at], %w[Updated updated_at], %w[Name name], %w[Post\ count post_count]], include_blank: true, selected: params.dig(:search, :order) %> <%= f.submit "Search" %> diff --git a/app/views/pool_versions/_secondary_links.html.erb b/app/views/pool_versions/_secondary_links.html.erb index baecd6434..79c55a1e2 100644 --- a/app/views/pool_versions/_secondary_links.html.erb +++ b/app/views/pool_versions/_secondary_links.html.erb @@ -1,5 +1,5 @@ <% content_for(:secondary_links) do %> - <%= quick_search_form_for(:name_matches, pool_versions_path, "pools", autocomplete: "pool") %> + <%= quick_search_form_for(:name_contains, pool_versions_path, "pools", autocomplete: "pool") %> <%= subnav_link_to "Listing", pool_versions_path %> <%= subnav_link_to "Search", search_pool_versions_path %> <% end %> diff --git a/app/views/pool_versions/search.html.erb b/app/views/pool_versions/search.html.erb index eadb6fe90..4db90bf5b 100644 --- a/app/views/pool_versions/search.html.erb +++ b/app/views/pool_versions/search.html.erb @@ -4,7 +4,7 @@ <%= search_form_for(pool_versions_path) do |f| %> <%= f.input :updater_name, label: "Updater", input_html: { value: params.dig(:search, :updater_name), "data-autocomplete": "user" } %> - <%= f.input :name_matches, label: "Pool", input_html: { value: params.dig(:search, :name_matches), "data-autocomplete": "pool" } %> + <%= f.input :name_contains, label: "Pool", input_html: { value: params.dig(:search, :name_contains), "data-autocomplete": "pool" } %> <%= f.input :category, label: "Category", collection: [["Series", "series"], ["Collection", "collection"]], include_blank: true %> <%= f.input :is_new, label: "New?", collection: [["Yes", true], ["No", false]], include_blank: true %> <%= f.input :name_changed, label: "Name changed?", collection: [["Yes", true], ["No", false]], include_blank: true %> diff --git a/app/views/pools/_search.html.erb b/app/views/pools/_search.html.erb index 211f681ec..88f0eb42a 100644 --- a/app/views/pools/_search.html.erb +++ b/app/views/pools/_search.html.erb @@ -1,5 +1,5 @@ <%= search_form_for(path) do |f| %> - <%= f.input :name_matches, label: "Name", input_html: { value: params.dig(:search, :name_matches), "data-autocomplete": "pool" } %> + <%= f.input :name_contains, label: "Name", input_html: { value: params.dig(:search, :name_contains), "data-autocomplete": "pool" } %> <%= f.input :description_matches, label: "Description", input_html: { value: params.dig(:search, :description_matches) } %> <%= f.input :post_tags_match, label: "Post tags", input_html: { value: params.dig(:search, :post_tags_match), "data-autocomplete": "tag-query" } %> <%= f.input :is_deleted, label: "Deleted?", as: :select, include_blank: true, selected: params[:search][:is_deleted] %> diff --git a/app/views/pools/_secondary_links.html.erb b/app/views/pools/_secondary_links.html.erb index d2b1ca5c9..09767c6cf 100644 --- a/app/views/pools/_secondary_links.html.erb +++ b/app/views/pools/_secondary_links.html.erb @@ -1,5 +1,5 @@ <% content_for(:secondary_links) do %> - <%= quick_search_form_for(:name_matches, pools_path, "pools", autocomplete: "pool", redirect: true) %> + <%= quick_search_form_for(:name_contains, pools_path, "pools", autocomplete: "pool", redirect: true) %> <%= subnav_link_to "Gallery", gallery_pools_path %> <%= subnav_link_to "Listing", pools_path %> <% if policy(Pool).create? %> diff --git a/test/functional/artist_versions_controller_test.rb b/test/functional/artist_versions_controller_test.rb index beabace8c..8221d93b1 100644 --- a/test/functional/artist_versions_controller_test.rb +++ b/test/functional/artist_versions_controller_test.rb @@ -22,8 +22,7 @@ class ArtistVersionsControllerTest < ActionDispatch::IntegrationTest should respond_to_search({}).with { @versions.reverse } should respond_to_search(name: "masao").with { [@versions[2], @versions[0]] } - should respond_to_search(name_matches: "(deleted)").with { @versions[1] } - should respond_to_search(group_name_matches: "the_best").with { @versions[2] } + should respond_to_search(group_name: "the_best").with { @versions[2] } should respond_to_search(urls_include_any: "https://www.deviantart.com/masao").with { [@versions[2], @versions[1], @versions[0]] } should respond_to_search(is_deleted: "true").with { @versions[1] } diff --git a/test/functional/favorite_groups_controller_test.rb b/test/functional/favorite_groups_controller_test.rb index 6e9c22194..456b99137 100644 --- a/test/functional/favorite_groups_controller_test.rb +++ b/test/functional/favorite_groups_controller_test.rb @@ -9,7 +9,7 @@ class FavoriteGroupsControllerTest < ActionDispatch::IntegrationTest context "index action" do setup do - @mod_favgroup = create(:favorite_group, name: "monochrome", creator: build(:moderator_user, name: "fumimi")) + @mod_favgroup = create(:favorite_group, name: "Beautiful Smile", creator: build(:moderator_user, name: "fumimi")) @private_favgroup = create(:private_favorite_group) end @@ -19,7 +19,12 @@ class FavoriteGroupsControllerTest < ActionDispatch::IntegrationTest end should respond_to_search({}).with { [@mod_favgroup, @favgroup] } - should respond_to_search(name: "monochrome").with { @mod_favgroup } + should respond_to_search(name_contains: "eautiful").with { @mod_favgroup } + should respond_to_search(name_contains: "beautiful smile").with { @mod_favgroup } + should respond_to_search(name_contains: "smiling beauty").with { [] } + should respond_to_search(name_matches: "eautiful").with { [] } + should respond_to_search(name_matches: "beautiful smile").with { @mod_favgroup } + should respond_to_search(name_matches: "smiling beauty").with { @mod_favgroup } context "using includes" do should respond_to_search(creator_name: "fumimi").with { @mod_favgroup } diff --git a/test/functional/pools_controller_test.rb b/test/functional/pools_controller_test.rb index aee1a9d9a..212858203 100644 --- a/test/functional/pools_controller_test.rb +++ b/test/functional/pools_controller_test.rb @@ -9,7 +9,7 @@ class PoolsControllerTest < ActionDispatch::IntegrationTest end as(@user) do @post = create(:post) - @pool = create(:pool, name: "pool", description: "[[touhou]]") + @pool = create(:pool, name: "Beautiful Smile", description: "[[touhou]]") end end @@ -25,7 +25,12 @@ class PoolsControllerTest < ActionDispatch::IntegrationTest assert_equal(Pool.count, response.parsed_body.css("urlset url loc").size) end - should respond_to_search(name_matches: "pool").with { @pool } + should respond_to_search(name_contains: "eautiful").with { @pool } + should respond_to_search(name_contains: "beautiful smile").with { @pool } + should respond_to_search(name_contains: "smiling beauty").with { [] } + should respond_to_search(name_matches: "eautiful").with { [] } + should respond_to_search(name_matches: "beautiful smile").with { @pool } + should respond_to_search(name_matches: "smiling beauty").with { @pool } should respond_to_search(linked_to: "touhou").with { @pool } should respond_to_search(not_linked_to: "touhou").with { [] } end diff --git a/test/unit/pool_test.rb b/test/unit/pool_test.rb index a0f1e3315..a3ac59fe5 100644 --- a/test/unit/pool_test.rb +++ b/test/unit/pool_test.rb @@ -17,7 +17,12 @@ class PoolTest < ActiveSupport::TestCase @pool = FactoryBot.create(:pool, name: "Test Pool") assert_equal(@pool.id, Pool.find_by_name("test pool").id) - assert_equal(@pool.id, Pool.search(name_matches: "test pool").first.id) + + assert_equal([@pool.id], Pool.search(name_contains: "test pool").map(&:id)) + assert_equal([@pool.id], Pool.search(name_contains: "tes").map(&:id)) + assert_equal([@pool.id], Pool.search(name_matches: "test pool").map(&:id)) + assert_equal([@pool.id], Pool.search(name_matches: "testing pool").map(&:id)) + assert_equal([], Pool.search(name_matches: "tes").map(&:id)) end should "find pools by post id" do