From 7de0f54dab9dc11ec912808835d465bd0e3d38d4 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sun, 28 Jul 2024 17:28:11 +0530 Subject: [PATCH 01/10] feat: basic topic search implementation --- .../forum_threads_controller.rb | 16 +++++++ app/models/forum_post.rb | 13 ++++++ app/models/forum_thread.rb | 43 +++++++++++++++++++ .../forum_threads/index.html.erb | 18 +++++--- config/routes.rb | 1 + ..._add_searchable_column_to_forum_threads.rb | 24 +++++++++++ simple_discussion.gemspec | 2 +- 7 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20240728092034_add_searchable_column_to_forum_threads.rb diff --git a/app/controllers/simple_discussion/forum_threads_controller.rb b/app/controllers/simple_discussion/forum_threads_controller.rb index 048879c..03bf02b 100644 --- a/app/controllers/simple_discussion/forum_threads_controller.rb +++ b/app/controllers/simple_discussion/forum_threads_controller.rb @@ -71,6 +71,18 @@ def destroy redirect_to simple_discussion.forum_threads_path end + def search + if params[:query].present? + @forum_threads = ForumThread.basic_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 @@ -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 diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 80ccced..aa269e5 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 @@ -10,6 +11,8 @@ class ForumPost < ApplicationRecord scope :sorted, -> { order(:created_at) } after_update :solve_forum_thread, if: :solved? + after_update :update_thread_searchable, if: :is_first_post? + after_destroy :update_thread_searchable, if: :is_first_post? def clean_body filters = [:profanity, :sex, :violence, :hate] @@ -25,6 +28,16 @@ def clean_body end end + private + + def is_first_post? + forum_thread.forum_posts.order(:created_at).first == self + end + + def update_thread_searchable + forum_thread.update_searchable + end + 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..e0357ea 100644 --- a/app/models/forum_thread.rb +++ b/app/models/forum_thread.rb @@ -25,6 +25,49 @@ class ForumThread < ApplicationRecord scope :unpinned, -> { where.not(pinned: true) } scope :unsolved, -> { where.not(solved: true) } + if ActiveRecord::Base.connection.adapter_name.downcase.start_with?('postgresql') + include PgSearch::Model + pg_search_scope :basic_search, + against: %i[title], + using: { + tsearch: { + dictionary: 'english', + tsvector_column: 'searchable_data' + } + } + + after_save :generate_searchable + + def generate_searchable + require 'redcarpet' + require 'redcarpet/render_strip' + + markdown_renderer = Redcarpet::Render::StripDown.new + plain_body = Redcarpet::Markdown.new(markdown_renderer).render(forum_posts.first&.body.to_s) + + searchable_content = "#{title} #{plain_body}" + + tsvector_query = ActiveRecord::Base.send(:sanitize_sql_array, + ["SELECT to_tsvector('english', ?)", searchable_content]) + self.searchable_data = ActiveRecord::Base.connection.execute(tsvector_query).getvalue(0, 0) + # Save the changes without triggering callbacks to avoid infinite loop + update_column(:searchable_data, self.searchable_data) + end + else + def self.basic_search(query) + joins(:forum_posts) + .where("forum_threads.title LIKE :query OR forum_posts.body LIKE :query", query: "%#{query}%") + .distinct + end + end + + def update_searchable + if ActiveRecord::Base.connection.adapter_name.downcase.start_with?('postgresql') + generate_searchable + save + end + end + def clean_title filters = [:profanity, :sex, :violence, :hate] diff --git a/app/views/simple_discussion/forum_threads/index.html.erb b/app/views/simple_discussion/forum_threads/index.html.erb index c7db374..da092ab 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: "Search for Forum Threads", 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/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 diff --git a/db/migrate/20240728092034_add_searchable_column_to_forum_threads.rb b/db/migrate/20240728092034_add_searchable_column_to_forum_threads.rb new file mode 100644 index 0000000..1820771 --- /dev/null +++ b/db/migrate/20240728092034_add_searchable_column_to_forum_threads.rb @@ -0,0 +1,24 @@ +class AddSearchableColumnToForumThreads < ActiveRecord::Migration[7.0] + disable_ddl_transaction! + + def up + if ActiveRecord::Base.connection.adapter_name.downcase.start_with?('postgresql') + add_column :forum_threads, :searchable_data, :tsvector + + # Initialize the tsvector column + ForumThread.find_each do |thread| + thread.update_searchable + end + + # index concurrently + add_index :forum_threads, :searchable_data, using: :gin, algorithm: :concurrently + end + end + + def down + if ActiveRecord::Base.connection.adapter_name.downcase.start_with?('postgresql') + remove_index :forum_threads, :searchable_data + remove_column :forum_threads, :searchable_data + end + end +end diff --git a/simple_discussion.gemspec b/simple_discussion.gemspec index 3bcc7c4..22dcb74 100644 --- a/simple_discussion.gemspec +++ b/simple_discussion.gemspec @@ -27,6 +27,6 @@ Gem::Specification.new do |spec| spec.add_dependency "rails", ">= 4.2" spec.add_dependency "will_paginate", ">= 3.1.0" spec.add_dependency "language_filter", ">= 0.3.01" - + spec.add_dependency "pg_search", ">= 2.3.0" spec.metadata["rubygems_mfa_required"] = "true" end From 05a1123c508d106925df1862569d461c2996f678 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Thu, 15 Aug 2024 20:19:58 +0530 Subject: [PATCH 02/10] feat: removing implementation for topic search and exposing this implementation through helper method --- .../forum_threads_controller.rb | 2 +- app/models/forum_post.rb | 10 ----- app/models/forum_thread.rb | 43 ------------------- ..._add_searchable_column_to_forum_threads.rb | 24 ----------- simple_discussion.gemspec | 1 - test/dummy/app/helpers/application_helper.rb | 6 +++ 6 files changed, 7 insertions(+), 79 deletions(-) delete mode 100644 db/migrate/20240728092034_add_searchable_column_to_forum_threads.rb diff --git a/app/controllers/simple_discussion/forum_threads_controller.rb b/app/controllers/simple_discussion/forum_threads_controller.rb index 03bf02b..560a0df 100644 --- a/app/controllers/simple_discussion/forum_threads_controller.rb +++ b/app/controllers/simple_discussion/forum_threads_controller.rb @@ -73,7 +73,7 @@ def destroy def search if params[:query].present? - @forum_threads = ForumThread.basic_search(params[:query]) + @forum_threads = topic_search(params[:query]) .includes(:user, :forum_category) .paginate(per_page: 10, page: page_number) else diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index aa269e5..06847fa 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -11,8 +11,6 @@ class ForumPost < ApplicationRecord scope :sorted, -> { order(:created_at) } after_update :solve_forum_thread, if: :solved? - after_update :update_thread_searchable, if: :is_first_post? - after_destroy :update_thread_searchable, if: :is_first_post? def clean_body filters = [:profanity, :sex, :violence, :hate] @@ -30,14 +28,6 @@ def clean_body private - def is_first_post? - forum_thread.forum_posts.order(:created_at).first == self - end - - def update_thread_searchable - forum_thread.update_searchable - end - 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 e0357ea..1bc79e7 100644 --- a/app/models/forum_thread.rb +++ b/app/models/forum_thread.rb @@ -25,49 +25,6 @@ class ForumThread < ApplicationRecord scope :unpinned, -> { where.not(pinned: true) } scope :unsolved, -> { where.not(solved: true) } - if ActiveRecord::Base.connection.adapter_name.downcase.start_with?('postgresql') - include PgSearch::Model - pg_search_scope :basic_search, - against: %i[title], - using: { - tsearch: { - dictionary: 'english', - tsvector_column: 'searchable_data' - } - } - - after_save :generate_searchable - - def generate_searchable - require 'redcarpet' - require 'redcarpet/render_strip' - - markdown_renderer = Redcarpet::Render::StripDown.new - plain_body = Redcarpet::Markdown.new(markdown_renderer).render(forum_posts.first&.body.to_s) - - searchable_content = "#{title} #{plain_body}" - - tsvector_query = ActiveRecord::Base.send(:sanitize_sql_array, - ["SELECT to_tsvector('english', ?)", searchable_content]) - self.searchable_data = ActiveRecord::Base.connection.execute(tsvector_query).getvalue(0, 0) - # Save the changes without triggering callbacks to avoid infinite loop - update_column(:searchable_data, self.searchable_data) - end - else - def self.basic_search(query) - joins(:forum_posts) - .where("forum_threads.title LIKE :query OR forum_posts.body LIKE :query", query: "%#{query}%") - .distinct - end - end - - def update_searchable - if ActiveRecord::Base.connection.adapter_name.downcase.start_with?('postgresql') - generate_searchable - save - end - end - def clean_title filters = [:profanity, :sex, :violence, :hate] diff --git a/db/migrate/20240728092034_add_searchable_column_to_forum_threads.rb b/db/migrate/20240728092034_add_searchable_column_to_forum_threads.rb deleted file mode 100644 index 1820771..0000000 --- a/db/migrate/20240728092034_add_searchable_column_to_forum_threads.rb +++ /dev/null @@ -1,24 +0,0 @@ -class AddSearchableColumnToForumThreads < ActiveRecord::Migration[7.0] - disable_ddl_transaction! - - def up - if ActiveRecord::Base.connection.adapter_name.downcase.start_with?('postgresql') - add_column :forum_threads, :searchable_data, :tsvector - - # Initialize the tsvector column - ForumThread.find_each do |thread| - thread.update_searchable - end - - # index concurrently - add_index :forum_threads, :searchable_data, using: :gin, algorithm: :concurrently - end - end - - def down - if ActiveRecord::Base.connection.adapter_name.downcase.start_with?('postgresql') - remove_index :forum_threads, :searchable_data - remove_column :forum_threads, :searchable_data - end - end -end diff --git a/simple_discussion.gemspec b/simple_discussion.gemspec index 22dcb74..d7da08a 100644 --- a/simple_discussion.gemspec +++ b/simple_discussion.gemspec @@ -27,6 +27,5 @@ Gem::Specification.new do |spec| spec.add_dependency "rails", ">= 4.2" spec.add_dependency "will_paginate", ">= 3.1.0" spec.add_dependency "language_filter", ">= 0.3.01" - spec.add_dependency "pg_search", ">= 2.3.0" spec.metadata["rubygems_mfa_required"] = "true" end diff --git a/test/dummy/app/helpers/application_helper.rb b/test/dummy/app/helpers/application_helper.rb index de6be79..a190c28 100644 --- a/test/dummy/app/helpers/application_helper.rb +++ b/test/dummy/app/helpers/application_helper.rb @@ -1,2 +1,8 @@ module ApplicationHelper + # 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 From c36c1606a82615941ce6d97d76cbf5978b25c849 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Thu, 15 Aug 2024 20:23:33 +0530 Subject: [PATCH 03/10] fix ci standardrb issue --- app/controllers/simple_discussion/forum_threads_controller.rb | 4 ++-- test/dummy/app/helpers/application_helper.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/simple_discussion/forum_threads_controller.rb b/app/controllers/simple_discussion/forum_threads_controller.rb index 560a0df..cc2f669 100644 --- a/app/controllers/simple_discussion/forum_threads_controller.rb +++ b/app/controllers/simple_discussion/forum_threads_controller.rb @@ -74,8 +74,8 @@ def destroy def search if params[:query].present? @forum_threads = topic_search(params[:query]) - .includes(:user, :forum_category) - .paginate(per_page: 10, page: page_number) + .includes(:user, :forum_category) + .paginate(per_page: 10, page: page_number) else index return diff --git a/test/dummy/app/helpers/application_helper.rb b/test/dummy/app/helpers/application_helper.rb index a190c28..249c1ee 100644 --- a/test/dummy/app/helpers/application_helper.rb +++ b/test/dummy/app/helpers/application_helper.rb @@ -2,7 +2,7 @@ module ApplicationHelper # 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 + .where("forum_threads.title LIKE :query OR forum_posts.body LIKE :query", query: "%#{query}%") + .distinct end end From 53b3f4417b900e008c39941d2228b389b05fedb6 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Fri, 23 Aug 2024 20:46:50 +0530 Subject: [PATCH 04/10] fix: added default implementation of topic_search helper --- app/helpers/simple_discussion/forum_threads_helper.rb | 7 +++++++ simple_discussion.gemspec | 1 + test/dummy/app/helpers/application_helper.rb | 6 ------ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/helpers/simple_discussion/forum_threads_helper.rb b/app/helpers/simple_discussion/forum_threads_helper.rb index dfb7344..ec0a7e2 100644 --- a/app/helpers/simple_discussion/forum_threads_helper.rb +++ b/app/helpers/simple_discussion/forum_threads_helper.rb @@ -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 diff --git a/simple_discussion.gemspec b/simple_discussion.gemspec index d7da08a..7cb72ef 100644 --- a/simple_discussion.gemspec +++ b/simple_discussion.gemspec @@ -29,3 +29,4 @@ Gem::Specification.new do |spec| spec.add_dependency "language_filter", ">= 0.3.01" spec.metadata["rubygems_mfa_required"] = "true" end + diff --git a/test/dummy/app/helpers/application_helper.rb b/test/dummy/app/helpers/application_helper.rb index 249c1ee..de6be79 100644 --- a/test/dummy/app/helpers/application_helper.rb +++ b/test/dummy/app/helpers/application_helper.rb @@ -1,8 +1,2 @@ module ApplicationHelper - # 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 From e81ce9dca2d057d67b96803227a45214b69b3afb Mon Sep 17 00:00:00 2001 From: Waishnav Date: Fri, 23 Aug 2024 20:48:24 +0530 Subject: [PATCH 05/10] fix: removed unnecessary changes --- simple_discussion.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simple_discussion.gemspec b/simple_discussion.gemspec index 7cb72ef..3bcc7c4 100644 --- a/simple_discussion.gemspec +++ b/simple_discussion.gemspec @@ -27,6 +27,6 @@ Gem::Specification.new do |spec| spec.add_dependency "rails", ">= 4.2" spec.add_dependency "will_paginate", ">= 3.1.0" spec.add_dependency "language_filter", ">= 0.3.01" + spec.metadata["rubygems_mfa_required"] = "true" end - From 75955b629fb097dd2db6fa977dcfb77ffa3cebcb Mon Sep 17 00:00:00 2001 From: Waishnav Date: Fri, 23 Aug 2024 20:50:17 +0530 Subject: [PATCH 06/10] fix: standardrb --- app/helpers/simple_discussion/forum_threads_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/simple_discussion/forum_threads_helper.rb b/app/helpers/simple_discussion/forum_threads_helper.rb index ec0a7e2..d62fc00 100644 --- a/app/helpers/simple_discussion/forum_threads_helper.rb +++ b/app/helpers/simple_discussion/forum_threads_helper.rb @@ -26,7 +26,7 @@ def parent_layout(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 + # 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}%") From e13f38bb234301faa79b18fbdb3519c9fd953f15 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Fri, 23 Aug 2024 23:42:01 +0530 Subject: [PATCH 07/10] fix: i18 added --- app/views/simple_discussion/forum_threads/index.html.erb | 2 +- config/locales/en.yml | 1 + config/locales/es.yml | 1 + config/locales/fr.yml | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/views/simple_discussion/forum_threads/index.html.erb b/app/views/simple_discussion/forum_threads/index.html.erb index da092ab..21672aa 100644 --- a/app/views/simple_discussion/forum_threads/index.html.erb +++ b/app/views/simple_discussion/forum_threads/index.html.erb @@ -6,7 +6,7 @@ <% end %> <%= 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" %> + <%= text_field_tag :query, params[:query], placeholder: t('topic_search_input_placeholder'), autocomplete: "off", class: "form-control form-input" %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index a6943e4..f74c4ad 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -60,3 +60,4 @@ 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" diff --git a/config/locales/es.yml b/config/locales/es.yml index 804b5e7..01929a7 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -59,3 +59,4 @@ 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" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 31b362c..6910295 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -61,3 +61,4 @@ 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" From a6a9c8f7eaad817b2166cf52b4e14bb5c18351a0 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sat, 24 Aug 2024 00:16:43 +0530 Subject: [PATCH 08/10] fix: added button text i18n --- app/views/simple_discussion/forum_threads/index.html.erb | 2 +- config/locales/en.yml | 1 + config/locales/es.yml | 1 + config/locales/fr.yml | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/views/simple_discussion/forum_threads/index.html.erb b/app/views/simple_discussion/forum_threads/index.html.erb index 21672aa..dd311da 100644 --- a/app/views/simple_discussion/forum_threads/index.html.erb +++ b/app/views/simple_discussion/forum_threads/index.html.erb @@ -7,7 +7,7 @@ <%= 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? %> diff --git a/config/locales/en.yml b/config/locales/en.yml index f74c4ad..3a9b817 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -61,3 +61,4 @@ en: 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 01929a7..3656c5f 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -60,3 +60,4 @@ es: 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 6910295..f138735 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -62,3 +62,4 @@ fr: 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" From 21c2a78c4ba58cab77fd98b00ec5d5a1a95befd1 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sat, 24 Aug 2024 11:06:34 +0530 Subject: [PATCH 09/10] fix: moved the search logic to model and it will be overidable through overide model file --- .../simple_discussion/forum_threads_controller.rb | 5 ++--- app/helpers/simple_discussion/forum_threads_helper.rb | 7 ------- app/models/forum_thread.rb | 7 +++++++ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/controllers/simple_discussion/forum_threads_controller.rb b/app/controllers/simple_discussion/forum_threads_controller.rb index cc2f669..a6fa912 100644 --- a/app/controllers/simple_discussion/forum_threads_controller.rb +++ b/app/controllers/simple_discussion/forum_threads_controller.rb @@ -73,12 +73,11 @@ def destroy def search if params[:query].present? - @forum_threads = topic_search(params[:query]) + @forum_threads = ForumThread.search(params[:query]) .includes(:user, :forum_category) .paginate(per_page: 10, page: page_number) else - index - return + @forum_threads = [] end render :index end diff --git a/app/helpers/simple_discussion/forum_threads_helper.rb b/app/helpers/simple_discussion/forum_threads_helper.rb index d62fc00..dfb7344 100644 --- a/app/helpers/simple_discussion/forum_threads_helper.rb +++ b/app/helpers/simple_discussion/forum_threads_helper.rb @@ -25,11 +25,4 @@ 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 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 From 74ac3d6bc41aa9eb3f266fc0d9edc63b7d98a999 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sat, 24 Aug 2024 11:07:57 +0530 Subject: [PATCH 10/10] fix: standardrb --- .../simple_discussion/forum_threads_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/simple_discussion/forum_threads_controller.rb b/app/controllers/simple_discussion/forum_threads_controller.rb index a6fa912..7aa22ff 100644 --- a/app/controllers/simple_discussion/forum_threads_controller.rb +++ b/app/controllers/simple_discussion/forum_threads_controller.rb @@ -72,12 +72,12 @@ def destroy end def search - if params[:query].present? - @forum_threads = ForumThread.search(params[:query]) + @forum_threads = if params[:query].present? + ForumThread.search(params[:query]) .includes(:user, :forum_category) .paginate(per_page: 10, page: page_number) else - @forum_threads = [] + [] end render :index end