FIX: Allow finding non-lowercase tag groups (#12787)

FIX: Allow finding non-lowercase tag groups (#12787)

diff --git a/app/controllers/tag_groups_controller.rb b/app/controllers/tag_groups_controller.rb
index 76cecc0..9921d7a 100644
--- a/app/controllers/tag_groups_controller.rb
+++ b/app/controllers/tag_groups_controller.rb
@@ -68,7 +68,7 @@ class TagGroupsController < ApplicationController
     end
 
     if params[:names].present?
-      matches = matches.where(name: params[:names].map(&:downcase))
+      matches = matches.where('lower(NAME) in (?)', params[:names].map(&:downcase))
     end
 
     matches = matches.order('name').limit(params[:limit] || 5)
diff --git a/spec/requests/tag_groups_controller_spec.rb b/spec/requests/tag_groups_controller_spec.rb
index 6fcbf9a..885bf5d 100644
--- a/spec/requests/tag_groups_controller_spec.rb
+++ b/spec/requests/tag_groups_controller_spec.rb
@@ -55,20 +55,22 @@ RSpec.describe TagGroupsController do
     context 'for anons' do
       it 'returns the tag group with the associated tag names' do
         tag_group = tag_group_with_permission(everyone, readonly)
+        tag_group_with_permission(everyone, readonly)
 
-        get '/tag_groups/filter/search.json', params: { ids: [tag_group.id] }
+        get '/tag_groups/filter/search.json'
         expect(response.status).to eq(200)
 
         results = JSON.parse(response.body, symbolize_names: true).fetch(:results)
 
+        expect(results.count).to eq(2)
         expect(results.first[:name]).to eq(tag_group.name)
         expect(results.first[:tag_names]).to contain_exactly(tag.name)
       end
 
       it 'returns an empty array if the tag group is private' do
-        tag_group = tag_group_with_permission(staff, full)
+        tag_group_with_permission(staff, full)
 
-        get '/tag_groups/filter/search.json', params: { ids: [tag_group.id] }
+        get '/tag_groups/filter/search.json'
         expect(response.status).to eq(200)
 
         results = JSON.parse(response.body, symbolize_names: true).fetch(:results)
@@ -83,7 +85,7 @@ RSpec.describe TagGroupsController do
       it 'returns the tag group with the associated tag names' do
         tag_group = tag_group_with_permission(everyone, readonly)
 
-        get '/tag_groups/filter/search.json', params: { names: [tag_group.name] }
+        get '/tag_groups/filter/search.json'
         expect(response.status).to eq(200)
 
         results = JSON.parse(response.body, symbolize_names: true).fetch(:results)
@@ -93,19 +95,51 @@ RSpec.describe TagGroupsController do
       end
 
       it 'returns an empty array if the tag group is private' do
-        tag_group = tag_group_with_permission(staff, full)
+        tag_group_with_permission(staff, full)
 
-        get '/tag_groups/filter/search.json', params: { names: [tag_group.name] }
+        get '/tag_groups/filter/search.json'
         expect(response.status).to eq(200)
 
         results = JSON.parse(response.body, symbolize_names: true).fetch(:results)
 
         expect(results).to be_empty
       end
+
+      it 'finds exact case-insensitive matches using the `names` param' do
+        tag_group_with_permission(everyone, readonly, name: "Whee")
+        tag_group_with_permission(everyone, readonly, name: "Whee Two")
+
+        get '/tag_groups/filter/search.json', params: { names: ["WHEE"] }
+        expect(response.status).to eq(200)
+
+        results = JSON.parse(response.body, symbolize_names: true).fetch(:results)
+
+        expect(results.count).to eq(1)
+        expect(results.first[:name]).to eq("Whee")
+      end
+
+      it 'finds partial matches using the `q` param' do
+        tag_group_with_permission(everyone, readonly, name: "Whee")
+        tag_group_with_permission(everyone, readonly, name: "Woop")
+        tag_group_with_permission(everyone, readonly, name: "Hoop")
+
+        get '/tag_groups/filter/search.json', params: { q: "oop" }
+        expect(response.status).to eq(200)
+
+        results = JSON.parse(response.body, symbolize_names: true).fetch(:results)
+
+        expect(results.count).to eq(2)
+        expect(results.first[:name]).to eq("Hoop")
+        expect(results.last[:name]).to eq("Woop")
+      end
+
     end
 
-    def tag_group_with_permission(auto_group, permission_type)
-      Fabricate(:tag_group, tags: [tag]).tap do |tag_group|
+    def tag_group_with_permission(auto_group, permission_type, name: nil)
+      options = { tags: [tag] }
+      options.merge!({ name: name }) if name
+
+      Fabricate(:tag_group, options).tap do |tag_group|
         tag_group.permissions = [[auto_group, permission_type]]
         tag_group.save!
       end

GitHub sha: a172a6cd

1 Like

This commit appears in #12787 which was approved by romanrizzi. It was merged by CvX.