From bbb9fc8809eb86311beeda3112f175eca7cb2b6d Mon Sep 17 00:00:00 2001 From: Jyri-Petteri Paloposki Date: Sat, 5 Sep 2020 01:24:23 +0300 Subject: [PATCH] Make LIKE searches case-insensitive also on PgSQL --- app/controllers/todos_controller.rb | 2 +- app/models/context.rb | 2 +- app/models/note.rb | 2 +- app/models/project.rb | 4 ++-- app/models/search/search_results.rb | 6 +++--- lib/common.rb | 10 ++++++++++ test/controllers/search_controller_test.rb | 6 +++--- 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index 82437157..99789289 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -766,7 +766,7 @@ class TodosController < ApplicationController def get_not_completed_for_predecessor(relation, todo_id=nil) items = relation.todos.not_completed. - where('(LOWER(todos.description) LIKE ?)', "%#{params[:term].downcase}%") + where('(LOWER(todos.description) ' + Common.like_operator + '?)', "%#{params[:term].downcase}%") items = items.where("AND NOT(todos.id=?)", todo_id) unless todo_id.nil? items. diff --git a/app/models/context.rb b/app/models/context.rb index 2eaad6fb..7461e730 100644 --- a/app/models/context.rb +++ b/app/models/context.rb @@ -7,7 +7,7 @@ class Context < ApplicationRecord scope :active, -> { where state: :active } scope :hidden, -> { where state: :hidden } scope :closed, -> { where state: :closed } - scope :with_name, lambda { |name| where("name LIKE ?", name) } + scope :with_name, lambda { |name| where("name " + Common.like_operator + " ?", name) } acts_as_list :scope => :user, :top_of_list => 0 diff --git a/app/models/note.rb b/app/models/note.rb index 5ba0750a..dc23a535 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -2,5 +2,5 @@ class Note < ApplicationRecord belongs_to :user belongs_to :project - scope :with_body, lambda { |terms| where("body LIKE ?", terms) } + scope :with_body, lambda { |terms| where("body " + Common.like_operator + " ?", terms) } end diff --git a/app/models/project.rb b/app/models/project.rb index c7e62a85..675cbb45 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -11,8 +11,8 @@ class Project < ApplicationRecord scope :completed, -> { where state: 'completed' } scope :uncompleted, -> { where("NOT(state = ?)", 'completed') } - scope :with_name_or_description, lambda { |body| where("name LIKE ? OR description LIKE ?", body, body) } - scope :with_namepart, lambda { |body| where("name LIKE ?", body + '%') } + scope :with_name_or_description, lambda { |body| where("name " + Common.like_operator + " ? OR description " + Common.like_operator + " ?", body, body) } + scope :with_namepart, lambda { |body| where("name " + Common.like_operator + " ?", body + '%') } before_create :set_last_reviewed_now diff --git a/app/models/search/search_results.rb b/app/models/search/search_results.rb index 36130db3..73463656 100644 --- a/app/models/search/search_results.rb +++ b/app/models/search/search_results.rb @@ -27,14 +27,14 @@ module Search def incomplete_todos(terms) @user.todos. - where("(todos.description LIKE ? OR todos.notes LIKE ?) AND todos.completed_at IS NULL", terms, terms). + where("(todos.description " + Common.like_operator + " ? OR todos.notes " + Common.like_operator + " ?) AND todos.completed_at IS NULL", terms, terms). includes(Todo::DEFAULT_INCLUDES). reorder(Arel.sql("todos.due IS NULL, todos.due ASC, todos.created_at ASC")) end def complete_todos(terms) @user.todos. - where("(todos.description LIKE ? OR todos.notes LIKE ?) AND NOT (todos.completed_at IS NULL)", terms, terms). + where("(todos.description " + Common.like_operator + " ? OR todos.notes " + Common.like_operator + " ?) AND NOT (todos.completed_at IS NULL)", terms, terms). includes(Todo::DEFAULT_INCLUDES). reorder("todos.completed_at DESC") end @@ -46,7 +46,7 @@ module Search "LEFT JOIN taggings ON tags.id = taggings.tag_id "+ "LEFT JOIN todos ON taggings.taggable_id = todos.id "+ "WHERE todos.user_id=? "+ - "AND tags.name LIKE ? ", @user.id, terms]) + "AND tags.name " + Common.like_operator + " ? ", @user.id, terms]) end end diff --git a/lib/common.rb b/lib/common.rb index 240fa8f0..83105ed6 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -6,4 +6,14 @@ module Common @user_theme = SITE_CONFIG['default_theme'] || 'light_blue' end end + + def self.like_operator + # This is something of a hack to use the correct operator for Pg + if ActiveRecord::Base.connection.adapter_name.downcase.to_sym == :postgresql + like = 'ILIKE' + else + like = 'LIKE' + end + return like + end end diff --git a/test/controllers/search_controller_test.rb b/test/controllers/search_controller_test.rb index 06a0d8ed..e67eaa2c 100644 --- a/test/controllers/search_controller_test.rb +++ b/test/controllers/search_controller_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class SearchControllerTest < ActionController::TestCase - + def setup end @@ -10,12 +10,12 @@ class SearchControllerTest < ActionController::TestCase get :index assert_response 200 end - + def test_search_for_todo_with_tag login_as :admin_user post :results, params: { :search => "gates" } assert_response 200 assert_equal 3, assigns['count'], "should have found 3 todos" end - + end