Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 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,18 @@ def destroy
redirect_to simple_discussion.forum_threads_path
end

def search
if params[:query].present?
@forum_threads = topic_search(params[:query])
.includes(:user, :forum_category)
.paginate(per_page: 10, page: page_number)
else
index
return
end
render :index
end

private

def set_forum_thread
Expand All @@ -80,4 +92,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
7 changes: 7 additions & 0 deletions app/helpers/simple_discussion/forum_threads_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ def parent_layout(layout)
output = render(template: "layouts/#{layout}")
self.output_buffer = ActionView::OutputBuffer.new(output)
end

# simple sql query implementation of basic_search, this helper method can be complex as it you wanted it to be
def topic_search(query)
ForumThread.joins(:forum_posts)
.where("forum_threads.title LIKE :query OR forum_posts.body LIKE :query", query: "%#{query}%")
.distinct
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
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: "Search for Forum Threads", autocomplete: "off", class: "form-control form-input" %>
<button class="btn primary-button" type="submit" name="button" value="Submit">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 %>
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