pools: switch from search[name_matches] to search[name_contains].

The previous commit changed it so that `/pools?search[name_matches]`
does a full-text search. So for example, `search[name_matches]=smiling`
will now match pool names containing any of the words "smiling",
"smile", "smiles", or "smiled".

This commit adds a `/pools?search[name_contains]` param that does what
`name_matches` did before, and switches to it in search forms. So for
example, `search[name_contains]=smiling` will only match pool names
containing the exact substring "smiling".

This change is so that `<field>_matches` works consistently across the
site, and so that it's possible to search pool names by either an exact
substring match, or by a looser natural language match.

This is a minor breaking API change. API users can replace
`/pools?search[name_matches]` with `/pools?search[name_contains]` to get
the same behavior as before. The same applies to /favorite_groups.
This commit is contained in:
evazion
2022-09-21 22:52:30 -05:00
parent 3114ef3daf
commit 29a4ca0818
15 changed files with 40 additions and 28 deletions

View File

@@ -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]

View File

@@ -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?

View File

@@ -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?

View File

@@ -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