REFACTOR: Better variable name.

This commit is contained in:
Guo Xiang Tan 2019-03-13 15:23:01 +08:00
parent bd2edbb243
commit 684eef71c7
1 changed files with 11 additions and 9 deletions

View File

@ -394,31 +394,33 @@ class Search
exact = true
slug = match.to_s.split(":")
next if slug.empty?
category_slug, subcategory_slug = match.to_s.split(":")
next unless category_slug
if slug[1]
if subcategory_slug
# sub category
parent_category_id = Category
.where("lower(slug) = ? AND parent_category_id IS NULL", slug[0].downcase)
.where(
"lower(slug) = ? AND parent_category_id IS NULL", category_slug.downcase
)
.pluck(:id)
.first
category_id = Category
.where("lower(slug) = ? AND parent_category_id = ?",
slug[1].downcase, parent_category_id
subcategory_slug.downcase, parent_category_id
)
.pluck(:id)
.first
else
# main category
if slug[0][0] == "="
slug[0] = slug[0][1..-1]
if category_slug[0] == "="
category_slug = category_slug[1..-1]
else
exact = false
end
category_id = Category.where("lower(slug) = ?", slug[0].downcase)
category_id = Category.where("lower(slug) = ?", category_slug.downcase)
.order('case when parent_category_id is null then 0 else 1 end')
.pluck(:id)
.first
@ -434,7 +436,7 @@ class Search
posts.where("topics.category_id IN (?)", category_ids)
else
# try a possible tag match
tag_id = Tag.where_name(slug[0]).pluck(:id).first
tag_id = Tag.where_name(category_slug).pluck(:id).first
if (tag_id)
posts.where("topics.id IN (
SELECT DISTINCT(tt.topic_id)