From be0aa4960a0ff9bed9607a661abb43c55376e8c9 Mon Sep 17 00:00:00 2001 From: Earlopain Date: Thu, 18 May 2023 20:48:01 +0200 Subject: [PATCH] [Users] Add about section search --- app/controllers/users_controller.rb | 2 +- app/models/user.rb | 9 +++--- app/views/users/_search.html.erb | 1 + .../20230518182034_add_user_about_indicies.rb | 8 +++++ db/structure.sql | 31 ++++++++++++++++++- 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20230518182034_add_user_about_indicies.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c96bf21b9..d22bebfb0 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -136,7 +136,7 @@ class UsersController < ApplicationController end def search_params - permitted_params = %i[name_matches level min_level max_level can_upload_free can_approve_posts order] + permitted_params = %i[name_matches about_me level min_level max_level can_upload_free can_approve_posts order] permitted_params += %i[ip_addr email_matches] if CurrentUser.is_admin? permit_search_params permitted_params end diff --git a/app/models/user.rb b/app/models/user.rb index d9c2f3622..db46f6b11 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -798,11 +798,10 @@ class User < ApplicationRecord q = q.search_text_attribute(:name, params) q = q.attribute_matches(:level, params[:level]) - # TODO: Doesn't support relation filtering using this method. - # q = q.attribute_matches(:post_upload_count, params[:post_upload_count]) - # q = q.attribute_matches(:post_update_count, params[:post_update_count]) - # q = q.attribute_matches(:note_update_count, params[:note_update_count]) - # q = q.attribute_matches(:favorite_count, params[:favorite_count]) + + if params[:about_me].present? + q = q.attribute_matches(:profile_about, params[:about_me]).or(attribute_matches(:profile_artinfo, params[:about_me])) + end if params[:email_matches].present? q = q.where_ilike(:email, params[:email_matches]) diff --git a/app/views/users/_search.html.erb b/app/views/users/_search.html.erb index 3fee341a5..c6f303ae5 100644 --- a/app/views/users/_search.html.erb +++ b/app/views/users/_search.html.erb @@ -1,5 +1,6 @@ <%= form_search(path: users_path) do |f| %> <%= f.input :name_matches, label: "Name", hint: "Use * for wildcard", autocomplete: "user" %> + <%= f.input :about_me, label: "About", hint: "Use * for wildcard" %> <% if CurrentUser.is_admin? %> <%= f.input :email_matches, label: "Email", hint: "Use * for wildcard" %> diff --git a/db/migrate/20230518182034_add_user_about_indicies.rb b/db/migrate/20230518182034_add_user_about_indicies.rb new file mode 100644 index 000000000..1b65cdfdd --- /dev/null +++ b/db/migrate/20230518182034_add_user_about_indicies.rb @@ -0,0 +1,8 @@ +class AddUserAboutIndicies < ActiveRecord::Migration[7.0] + def up + add_index :users, "(to_tsvector('english', profile_about))", using: :gin + add_index :users, "(to_tsvector('english', profile_artinfo))", using: :gin + execute("CREATE INDEX index_users_on_lower_profile_about_trgm ON users USING gin ((lower(profile_about)) gin_trgm_ops)") + execute("CREATE INDEX index_users_on_lower_profile_artinfo_trgm ON users USING gin ((lower(profile_artinfo)) gin_trgm_ops)") + end +end diff --git a/db/structure.sql b/db/structure.sql index 9b02dc20c..480c9129a 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -4300,6 +4300,20 @@ CREATE INDEX index_users_on_email ON public.users USING btree (email); CREATE INDEX index_users_on_last_ip_addr ON public.users USING btree (last_ip_addr) WHERE (last_ip_addr IS NOT NULL); +-- +-- Name: index_users_on_lower_profile_about_trgm; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_users_on_lower_profile_about_trgm ON public.users USING gin (lower(profile_about) public.gin_trgm_ops); + + +-- +-- Name: index_users_on_lower_profile_artinfo_trgm; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_users_on_lower_profile_artinfo_trgm ON public.users USING gin (lower(profile_artinfo) public.gin_trgm_ops); + + -- -- Name: index_users_on_name; Type: INDEX; Schema: public; Owner: - -- @@ -4307,6 +4321,20 @@ CREATE INDEX index_users_on_last_ip_addr ON public.users USING btree (last_ip_ad CREATE UNIQUE INDEX index_users_on_name ON public.users USING btree (lower((name)::text)); +-- +-- Name: index_users_on_to_tsvector_english_profile_about; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_users_on_to_tsvector_english_profile_about ON public.users USING gin (to_tsvector('english'::regconfig, profile_about)); + + +-- +-- Name: index_users_on_to_tsvector_english_profile_artinfo; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_users_on_to_tsvector_english_profile_artinfo ON public.users USING gin (to_tsvector('english'::regconfig, profile_artinfo)); + + -- -- Name: index_wiki_page_versions_on_created_at; Type: INDEX; Schema: public; Owner: - -- @@ -4704,6 +4732,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20230316084945'), ('20230506161827'), ('20230513074838'), -('20230517155547'); +('20230517155547'), +('20230518182034');