From 6653bbf2e9f24b34a0ff37dfb02568bfd97c379d Mon Sep 17 00:00:00 2001 From: Alexander-Dubrawski Date: Fri, 22 Jan 2021 18:53:17 +0100 Subject: [PATCH 01/39] Add workaround for flash message issue --- app/views/layouts/_flash_messages.html.erb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/views/layouts/_flash_messages.html.erb b/app/views/layouts/_flash_messages.html.erb index 03ab968..dec5660 100644 --- a/app/views/layouts/_flash_messages.html.erb +++ b/app/views/layouts/_flash_messages.html.erb @@ -1,12 +1,17 @@ <% flash.each do |level, log| %>
- <% if !log[:heading].nil? %> -

<%= log[:heading] %>

+ <% if log.is_a? String %> +
  • <%= log %>
  • <% end %> - <% log[:messages].each do |message| %> - + <% if !log.is_a? String %> + <% if !log[:heading].nil? %> +

    <%= log[:heading] %>

    + <% end %> + <% log[:messages].each do |message| %> + + <% end %> <% end %>
    <% end %> From 0f0c4369a514806aaa8c92ed9837a2fc76c87a2f Mon Sep 17 00:00:00 2001 From: Alexander-Dubrawski Date: Fri, 22 Jan 2021 19:37:41 +0100 Subject: [PATCH 02/39] First draft search engine --- app/controllers/users_controller.rb | 25 +++++++++++++++++++++---- config/routes.rb | 1 + 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index dd557b1..a714499 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,3 +1,5 @@ +require 'set' + class UsersController < ApplicationController include SocialAccountsHelper before_action :authorize, except: %i[show index search] @@ -42,10 +44,25 @@ def add_contact end def search - @users = User.search(params[:search]).where.not(id: current_user.id) - @users_to_add = @users.reject do |user| - current_user.sent_contact_request?(user) - end + use_wildcards = true + @users_to_add = Set[] + @contacts = Set[] + if current_user and params[:search] and use_wildcards + patterns = params[:search].split() + @user = User.find(current_user.id) + for pattern in patterns do + @contacts.merge(@user.contacts.where('firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?', "%#{pattern}%", "%#{pattern}%", "%#{pattern}%", "%#{pattern}%").to_set) + @users_to_add.merge(User.where('firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?', "%#{pattern}%", "%#{pattern}%", "%#{pattern}%", "%#{pattern}%").where.not(id: current_user.id).to_set) + @users_to_add - @contacts + if current_user and params[:search] and !use_wildcards + patterns = params[:search].split() + @user = User.find(current_user.id) + for pattern in patterns do + @contacts = @user.contacts.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}").to_set + @users_to_add = User.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}").where.not(id: current_user.id).to_set + @users_to_add - @contacts + if !current_user and params[:search] + @users_to_add = User.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}") end private diff --git a/config/routes.rb b/config/routes.rb index 9c33f84..36829a1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ devise_for :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root to: 'home#dashboard' + get 'search', to: 'users#search' resources :users, only: %i[show edit update index] do patch 'status', to: 'users#update_status', as: 'update_status', on: :member From c10eb208faa78c102cc5354357637fd029cf18ab Mon Sep 17 00:00:00 2001 From: Alexander-Dubrawski Date: Fri, 22 Jan 2021 19:50:22 +0100 Subject: [PATCH 03/39] Adjust code --- app/controllers/users_controller.rb | 19 ++++++++++++------- app/views/users/search.html.erb | 13 ++++++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a714499..861ed02 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -45,24 +45,29 @@ def add_contact def search use_wildcards = true - @users_to_add = Set[] + @users = Set[] @contacts = Set[] - if current_user and params[:search] and use_wildcards + if current_user and params[:search] and use_wildcards patterns = params[:search].split() @user = User.find(current_user.id) for pattern in patterns do @contacts.merge(@user.contacts.where('firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?', "%#{pattern}%", "%#{pattern}%", "%#{pattern}%", "%#{pattern}%").to_set) - @users_to_add.merge(User.where('firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?', "%#{pattern}%", "%#{pattern}%", "%#{pattern}%", "%#{pattern}%").where.not(id: current_user.id).to_set) - @users_to_add - @contacts + @users.merge(User.where('firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?', "%#{pattern}%", "%#{pattern}%", "%#{pattern}%", "%#{pattern}%").where.not(id: current_user.id).to_set) + end + @users - @contacts + end if current_user and params[:search] and !use_wildcards patterns = params[:search].split() @user = User.find(current_user.id) for pattern in patterns do @contacts = @user.contacts.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}").to_set - @users_to_add = User.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}").where.not(id: current_user.id).to_set - @users_to_add - @contacts + @users = User.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}").where.not(id: current_user.id).to_set + end + @users - @contacts + end if !current_user and params[:search] - @users_to_add = User.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}") + @users = User.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}") + end end private diff --git a/app/views/users/search.html.erb b/app/views/users/search.html.erb index 8a618e0..abf20c4 100644 --- a/app/views/users/search.html.erb +++ b/app/views/users/search.html.erb @@ -25,15 +25,22 @@ + <% @contacts.each do |user| %> + + <%= user.username%> + <%= user.firstname%> + <%= user.lastname%> + <%= user.email %> + + <% end %> + <% @users.each do |user| %> <%= user.username%> <%= user.firstname%> <%= user.lastname%> <%= user.email %> - <% if @users_to_add.include? user %> - <%= button_to '+', user_contact_requests_path(user), method: :post, id: user.id %> - <% end %> + <%= button_to '+', user_contact_requests_path(user), method: :post, id: user.id %> <% end %> From 64ce24c4a03732ea37d3b4cbf175b97c248f9e10 Mon Sep 17 00:00:00 2001 From: Alexander-Dubrawski Date: Fri, 22 Jan 2021 19:54:05 +0100 Subject: [PATCH 04/39] Use wildcards in pulic search --- app/controllers/users_controller.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 861ed02..0c80de8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -44,7 +44,7 @@ def add_contact end def search - use_wildcards = true + use_wildcards = false @users = Set[] @contacts = Set[] if current_user and params[:search] and use_wildcards @@ -65,7 +65,10 @@ def search end @users - @contacts end - if !current_user and params[:search] + if !current_user and params[:search] and use_wildcards + @users = User.where('firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}") + end + if !current_user and params[:search] and !use_wildcards @users = User.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}") end end From 9b8fed810d7bd3bd09aff27c0cffbebda55fa65f Mon Sep 17 00:00:00 2001 From: Alexander-Dubrawski Date: Fri, 22 Jan 2021 21:48:37 +0100 Subject: [PATCH 05/39] Refactor code --- app/controllers/users_controller.rb | 23 +++++++++-------------- app/helpers/users_helper.rb | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0c80de8..6e784a4 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,8 +2,9 @@ class UsersController < ApplicationController include SocialAccountsHelper + include UsersHelper before_action :authorize, except: %i[show index search] - helper_method :generate_link, :supported_social_networks + helper_method :generate_link, :supported_social_networks, :search_record def show @user = User.find(params[:id]) @@ -47,29 +48,23 @@ def search use_wildcards = false @users = Set[] @contacts = Set[] - if current_user and params[:search] and use_wildcards - patterns = params[:search].split() + if current_user and params[:search] and use_wildcards @user = User.find(current_user.id) - for pattern in patterns do - @contacts.merge(@user.contacts.where('firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?', "%#{pattern}%", "%#{pattern}%", "%#{pattern}%", "%#{pattern}%").to_set) - @users.merge(User.where('firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?', "%#{pattern}%", "%#{pattern}%", "%#{pattern}%", "%#{pattern}%").where.not(id: current_user.id).to_set) - end + @users = search_record(params[:search], User) + @contacts = search_record(params[:search], @user.contacts) @users - @contacts end if current_user and params[:search] and !use_wildcards - patterns = params[:search].split() @user = User.find(current_user.id) - for pattern in patterns do - @contacts = @user.contacts.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}").to_set - @users = User.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}").where.not(id: current_user.id).to_set - end + @users = search_record(params[:search], User, false) + @contacts = search_record(params[:search], @user.contacts, false) @users - @contacts end if !current_user and params[:search] and use_wildcards - @users = User.where('firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}") + @users = search_record(params[:search], User) end if !current_user and params[:search] and !use_wildcards - @users = User.where('firstname = ? OR lastname = ? OR username = ? OR email = ?', "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}") + @users = search_record(params[:search], User, false) end end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 2310a24..809f3ca 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -1,2 +1,20 @@ +require 'set' + module UsersHelper + def search_record(pattern, record, use_wildcard = true) + patterns = params[:search].split() + matches = Set[] + if use_wildcard + query = 'firstname = ? OR lastname = ? OR username = ? OR email = ?' + for pattern in patterns do + matches.merge(record.where(query, "#{pattern}", "#{pattern}", "#{pattern}", "#{pattern}").to_set) + end + return matches + end + query = 'firstname LIKE ? OR lastname LIKE ? OR username LIKE ? OR email LIKE ?' + for pattern in patterns do + matches.merge(record.where(query, "%#{pattern}%", "%#{pattern}%", "%#{pattern}%", "%#{pattern}%").to_set) + end + return matches + end end From 6fec237c6e3f56fbef21b457c5bf8d83e774bdc1 Mon Sep 17 00:00:00 2001 From: Alexander-Dubrawski Date: Fri, 22 Jan 2021 21:57:35 +0100 Subject: [PATCH 06/39] Add search button --- app/views/layouts/_navbar.html.erb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb index a937130..5bf1992 100644 --- a/app/views/layouts/_navbar.html.erb +++ b/app/views/layouts/_navbar.html.erb @@ -18,7 +18,11 @@ <% if user_signed_in? %> - <% end %> + <% end %> + +