Make LIKE searches case-insensitive also on PgSQL

This commit is contained in:
Jyri-Petteri Paloposki 2020-09-05 01:24:23 +03:00
parent e89511aec8
commit bbb9fc8809
7 changed files with 21 additions and 11 deletions

View file

@ -766,7 +766,7 @@ class TodosController < ApplicationController
def get_not_completed_for_predecessor(relation, todo_id=nil) def get_not_completed_for_predecessor(relation, todo_id=nil)
items = relation.todos.not_completed. 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 = items.where("AND NOT(todos.id=?)", todo_id) unless todo_id.nil?
items. items.

View file

@ -7,7 +7,7 @@ class Context < ApplicationRecord
scope :active, -> { where state: :active } scope :active, -> { where state: :active }
scope :hidden, -> { where state: :hidden } scope :hidden, -> { where state: :hidden }
scope :closed, -> { where state: :closed } 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 acts_as_list :scope => :user, :top_of_list => 0

View file

@ -2,5 +2,5 @@ class Note < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :project belongs_to :project
scope :with_body, lambda { |terms| where("body LIKE ?", terms) } scope :with_body, lambda { |terms| where("body " + Common.like_operator + " ?", terms) }
end end

View file

@ -11,8 +11,8 @@ class Project < ApplicationRecord
scope :completed, -> { where state: 'completed' } scope :completed, -> { where state: 'completed' }
scope :uncompleted, -> { where("NOT(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_name_or_description, lambda { |body| where("name " + Common.like_operator + " ? OR description " + Common.like_operator + " ?", body, body) }
scope :with_namepart, lambda { |body| where("name LIKE ?", body + '%') } scope :with_namepart, lambda { |body| where("name " + Common.like_operator + " ?", body + '%') }
before_create :set_last_reviewed_now before_create :set_last_reviewed_now

View file

@ -27,14 +27,14 @@ module Search
def incomplete_todos(terms) def incomplete_todos(terms)
@user.todos. @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). includes(Todo::DEFAULT_INCLUDES).
reorder(Arel.sql("todos.due IS NULL, todos.due ASC, todos.created_at ASC")) reorder(Arel.sql("todos.due IS NULL, todos.due ASC, todos.created_at ASC"))
end end
def complete_todos(terms) def complete_todos(terms)
@user.todos. @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). includes(Todo::DEFAULT_INCLUDES).
reorder("todos.completed_at DESC") reorder("todos.completed_at DESC")
end end
@ -46,7 +46,7 @@ module Search
"LEFT JOIN taggings ON tags.id = taggings.tag_id "+ "LEFT JOIN taggings ON tags.id = taggings.tag_id "+
"LEFT JOIN todos ON taggings.taggable_id = todos.id "+ "LEFT JOIN todos ON taggings.taggable_id = todos.id "+
"WHERE todos.user_id=? "+ "WHERE todos.user_id=? "+
"AND tags.name LIKE ? ", @user.id, terms]) "AND tags.name " + Common.like_operator + " ? ", @user.id, terms])
end end
end end

View file

@ -6,4 +6,14 @@ module Common
@user_theme = SITE_CONFIG['default_theme'] || 'light_blue' @user_theme = SITE_CONFIG['default_theme'] || 'light_blue'
end end
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 end

View file

@ -1,7 +1,7 @@
require 'test_helper' require 'test_helper'
class SearchControllerTest < ActionController::TestCase class SearchControllerTest < ActionController::TestCase
def setup def setup
end end
@ -10,12 +10,12 @@ class SearchControllerTest < ActionController::TestCase
get :index get :index
assert_response 200 assert_response 200
end end
def test_search_for_todo_with_tag def test_search_for_todo_with_tag
login_as :admin_user login_as :admin_user
post :results, params: { :search => "gates" } post :results, params: { :search => "gates" }
assert_response 200 assert_response 200
assert_equal 3, assigns['count'], "should have found 3 todos" assert_equal 3, assigns['count'], "should have found 3 todos"
end end
end end