FIX: Advanced search category term should be case insensitive.

This commit is contained in:
Guo Xiang Tan 2019-03-12 14:11:21 +08:00
parent 95532814df
commit da941840d4
2 changed files with 20 additions and 8 deletions

View File

@ -399,8 +399,17 @@ class Search
if slug[1]
# sub category
parent_category_id = Category.where(slug: slug[0].downcase, parent_category_id: nil).pluck(:id).first
category_id = Category.where(slug: slug[1].downcase, parent_category_id: parent_category_id).pluck(:id).first
parent_category_id = Category
.where("lower(slug) = ? AND parent_category_id IS NULL", slug[0].downcase)
.pluck(:id)
.first
category_id = Category
.where("lower(slug) = ? AND parent_category_id = ?",
slug[1].downcase, parent_category_id
)
.pluck(:id)
.first
else
# main category
if slug[0][0] == "="
@ -409,7 +418,7 @@ class Search
exact = false
end
category_id = Category.where(slug: slug[0].downcase)
category_id = Category.where("lower(slug) = ?", slug[0].downcase)
.order('case when parent_category_id is null then 0 else 1 end')
.pluck(:id)
.first

View File

@ -873,25 +873,28 @@ describe Search do
it 'supports category slug and tags' do
# main category
category = Fabricate(:category, name: 'category 24', slug: 'category-24')
category = Fabricate(:category, name: 'category 24', slug: 'cateGory-24')
topic = Fabricate(:topic, created_at: 3.months.ago, category: category)
post = Fabricate(:post, raw: 'Sams first post', topic: topic)
expect(Search.execute('sams post #category-24').posts.length).to eq(1)
expect(Search.execute('sams post #categoRy-24').posts.length).to eq(1)
expect(Search.execute("sams post category:#{category.id}").posts.length).to eq(1)
expect(Search.execute('sams post #category-25').posts.length).to eq(0)
expect(Search.execute('sams post #categoRy-25').posts.length).to eq(0)
sub_category = Fabricate(:category, name: 'sub category', slug: 'sub-category', parent_category_id: category.id)
second_topic = Fabricate(:topic, created_at: 3.months.ago, category: sub_category)
Fabricate(:post, raw: 'sams second post', topic: second_topic)
expect(Search.execute("sams post category:category-24").posts.length).to eq(2)
expect(Search.execute("sams post category:=category-24").posts.length).to eq(1)
expect(Search.execute("sams post category:categoRY-24").posts.length).to eq(2)
expect(Search.execute("sams post category:=cAtegory-24").posts.length).to eq(1)
expect(Search.execute("sams post #category-24").posts.length).to eq(2)
expect(Search.execute("sams post #=category-24").posts.length).to eq(1)
expect(Search.execute("sams post #sub-category").posts.length).to eq(1)
expect(Search.execute("sams post #categoRY-24:SUB-category").posts.length)
.to eq(1)
# tags
topic.tags = [Fabricate(:tag, name: 'alpha'), Fabricate(:tag, name: 'привет'), Fabricate(:tag, name: 'HeLlO')]
expect(Search.execute('this is a test #alpha').posts.map(&:id)).to eq([post.id])