Skip to content

Commit

Permalink
[feature] add search on forum (#1572)
Browse files Browse the repository at this point in the history
- add simple form for search
- update of Index action to search the forum if search query present.

Co-authored-by: amu1996 <thisamu4u@gmail.com>
  • Loading branch information
donrestarone and amu1996 authored Jul 18, 2023
1 parent f8cd96b commit 66a63e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 13 additions & 1 deletion app/controllers/simple_discussion/forum_threads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ class SimpleDiscussion::ForumThreadsController < SimpleDiscussion::ApplicationCo
after_action :broadcast_to_mods, only: [:create]

def index
@forum_threads = ForumThread.pinned_first.sorted.includes(:user, :forum_category).paginate(page: page_number)
@forum_threads = ForumThread.pinned_first.sorted
.includes(:user, :forum_category)
.paginate(page: page_number)

if params[:search].present?
search_query = "%#{params[:search].downcase}%"

@forum_threads = @forum_threads.joins(forum_posts: :rich_text_body)
.where("LOWER(forum_threads.title) LIKE ? OR LOWER(action_text_rich_texts.body) LIKE ?", search_query, search_query)
.distinct
end

render "simple_discussion/forum_threads/index"
end

def answered
Expand Down
6 changes: 5 additions & 1 deletion app/views/layouts/simple_discussion.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@
<div class="col-md-9 mb-3">

<div class="card card-body">
<%= form_tag simple_discussion.forum_threads_path, method: :get do %>
<%= text_field_tag :search, params[:search], placeholder: "Search threads..." %>
<%= submit_tag "Search" %>
<% end %>
<%= yield %>
</div>

</div>
</div>

<% parent_layout("forum") %>
<%= cms_snippet_render('footer').html_safe unless mobile? %>
<%= cms_snippet_render('footer').html_safe unless mobile? %>

0 comments on commit 66a63e4

Please sign in to comment.