diff --git a/app/controllers/simple_discussion/forum_threads_controller.rb b/app/controllers/simple_discussion/forum_threads_controller.rb index 048879c..7aa22ff 100644 --- a/app/controllers/simple_discussion/forum_threads_controller.rb +++ b/app/controllers/simple_discussion/forum_threads_controller.rb @@ -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) + .paginate(per_page: 10, page: page_number) + else + [] + end + render :index + end + private def set_forum_thread @@ -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 diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 80ccced..06847fa 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -1,4 +1,5 @@ require "language_filter" + class ForumPost < ApplicationRecord belongs_to :forum_thread, counter_cache: true, touch: true belongs_to :user @@ -25,6 +26,8 @@ def clean_body end end + private + def solve_forum_thread forum_thread.update(solved: true) end diff --git a/app/models/forum_thread.rb b/app/models/forum_thread.rb index 1bc79e7..61eb4d6 100644 --- a/app/models/forum_thread.rb +++ b/app/models/forum_thread.rb @@ -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 diff --git a/app/views/simple_discussion/forum_threads/index.html.erb b/app/views/simple_discussion/forum_threads/index.html.erb index c7db374..dd311da 100644 --- a/app/views/simple_discussion/forum_threads/index.html.erb +++ b/app/views/simple_discussion/forum_threads/index.html.erb @@ -1,17 +1,21 @@ - + + <% ForumCategory.sorted.each do |category| %> + + <% end %> + + <%= 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" %> + <% end %> - + <% if @forum_threads.none? %>
<%= t('search_not_found') %>. <%= t('check_out') %> <%= link_to t('latest_questions'), simple_discussion.forum_threads_path %> <%= t('instead') %>
<% else %> - <%= render partial: "simple_discussion/forum_threads/forum_thread", collection: @forum_threads %>
<%= will_paginate @forum_threads, url_builder: simple_discussion, renderer: SimpleDiscussion::BootstrapLinkRenderer %>
- <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index a6943e4..3a9b817 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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" diff --git a/config/locales/es.yml b/config/locales/es.yml index 804b5e7..3656c5f 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -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" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 31b362c..f138735 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -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" diff --git a/config/routes.rb b/config/routes.rb index 2345640..db92379 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ get :mine get :participating get :spam_reports + get :search get "category/:id", to: "forum_categories#index", as: :forum_category end