feat: add fulltext search with ?q= parameter
Filter posts by case-insensitive match in title and body. Extracted filter_by_lang and search_posts helpers to keep complexity in check.
This commit is contained in:
+19
-4
@@ -102,14 +102,29 @@ module Volumen
|
||||
|
||||
def filtered_posts
|
||||
posts = published_posts
|
||||
if params["lang"]
|
||||
lang = params["lang"]
|
||||
posts = posts.select { |post| post.lang == lang || post.all_langs? }
|
||||
end
|
||||
posts = filter_by_lang(posts)
|
||||
posts = posts.select { |post| post.tags.include?(params["tag"]) } if params["tag"]
|
||||
posts = search_posts(posts) if params["q"]
|
||||
posts
|
||||
end
|
||||
|
||||
def filter_by_lang(posts)
|
||||
return posts unless params["lang"]
|
||||
|
||||
lang = params["lang"]
|
||||
posts.select { |post| post.lang == lang || post.all_langs? }
|
||||
end
|
||||
|
||||
def search_posts(posts)
|
||||
query = params["q"].to_s.strip.downcase
|
||||
return posts if query.empty?
|
||||
|
||||
posts.select do |post|
|
||||
post.title.to_s.downcase.include?(query) ||
|
||||
post.body.downcase.include?(query)
|
||||
end
|
||||
end
|
||||
|
||||
def tag_counts
|
||||
counts = Hash.new(0)
|
||||
published_posts.each { |post| post.tags.each { |tag| counts[tag] += 1 } }
|
||||
|
||||
Reference in New Issue
Block a user