Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gsoc'24): topic search implementation #28

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions app/controllers/simple_discussion/forum_threads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ def destroy
redirect_to simple_discussion.forum_threads_path
end

def search
@forum_threads = if params[:query].present?
ForumThread.search(params[:query])
.includes(:user, :forum_category)
Waishnav marked this conversation as resolved.
Show resolved Hide resolved
.paginate(per_page: 10, page: page_number)
else
[]
end
render :index
end

private

def set_forum_thread
Expand All @@ -80,4 +91,8 @@ def set_forum_thread
def forum_thread_params
params.require(:forum_thread).permit(:title, :forum_category_id, forum_posts_attributes: [:body])
end

def page_number
params[:page] || 1
end
end
3 changes: 3 additions & 0 deletions app/models/forum_post.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "language_filter"

class ForumPost < ApplicationRecord
belongs_to :forum_thread, counter_cache: true, touch: true
belongs_to :user
Expand All @@ -25,6 +26,8 @@ def clean_body
end
end

private

def solve_forum_thread
forum_thread.update(solved: true)
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/forum_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ def clean_title
end
end

# simple search implementation on all forum threads
def self.search(query)
joins(:forum_posts)
.where("LOWER(forum_threads.title) LIKE :query OR LOWER(forum_posts.body) LIKE :query", query: "%#{query.downcase}%")
.distinct
end

def subscribed_users
(users + optin_subscribers).uniq - optout_subscribers
end
Expand Down
18 changes: 11 additions & 7 deletions app/views/simple_discussion/forum_threads/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<select class="custom-select w-auto mr-auto mb-4" onchange="window.location.href=this.value" name="choose_category">
<option value="<%= simple_discussion.forum_threads_path %>" <% if request.path == simple_discussion.forum_threads_path %> selected <% end %>><%= t("all_categories") %>&nbsp;&nbsp;&nbsp;</option>
<% ForumCategory.sorted.each do |category| %>
<option value="<%= simple_discussion.forum_category_forum_threads_path(category) %>" <% if request.path == simple_discussion.forum_category_forum_threads_path(category) %> selected <% end %> ><%= category.name %>&nbsp;&nbsp;&nbsp;</option>
<div class="row m-2 align-items-center my-4">
<select class="custom-select w-auto mr-auto" onchange="window.location.href=this.value" name="choose_category">
<option value="<%= simple_discussion.forum_threads_path %>" <% if request.path == simple_discussion.forum_threads_path %> selected <% end %>><%= t("all_categories") %>&nbsp;&nbsp;&nbsp;</option>
<% ForumCategory.sorted.each do |category| %>
<option value="<%= simple_discussion.forum_category_forum_threads_path(category) %>" <% if request.path == simple_discussion.forum_category_forum_threads_path(category) %> selected <% end %> ><%= category.name %>&nbsp;&nbsp;&nbsp;</option>
<% end %>
</select>
<%= form_tag("/forum/threads/search", method: "get", id: "search-box", class: "form-inline") do %>
<%= text_field_tag :query, params[:query], placeholder: t('topic_search_input_placeholder'), autocomplete: "off", class: "form-control form-input" %>
<button class="btn primary-button" type="submit" name="button" value="Submit"><%= t('search') %></button>
<% end %>
</select>
</div>
<% if @forum_threads.none? %>
<div><%= t('search_not_found') %>. <%= t('check_out') %> <%= link_to t('latest_questions'), simple_discussion.forum_threads_path %> <%= t('instead') %> </div>
<% else %>

<%= render partial: "simple_discussion/forum_threads/forum_thread", collection: @forum_threads %>

<div class="forum-threads-nav text-center">
<%= will_paginate @forum_threads, url_builder: simple_discussion, renderer: SimpleDiscussion::BootstrapLinkRenderer %>
</div>

<% end %>
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ en:
unmark_as_solution: "Unmark as Solution"
mark_as_solution: "Mark as Solution"
report_post: "Report Post"
topic_search_input_placeholder: "Search for Forum Threads"
search: "Search"
2 changes: 2 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ es:
unmark_as_solution: "Desmarcar como solución"
mark_as_solution: "Marcar como solución"
report_post: "Reportar publicación"
topic_search_input_placeholder: "Buscar hilos de foros"
search: "Buscar"
2 changes: 2 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ fr:
unmark_as_solution: "Démarquer comme solution"
mark_as_solution: "Marquer comme solution"
report_post: "Signaler le post"
topic_search_input_placeholder: "Rechercher des fils de discussion dans les forums"
search: "Rechercher"
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
get :mine
get :participating
get :spam_reports
get :search
get "category/:id", to: "forum_categories#index", as: :forum_category
end

Expand Down