From abbd7400e26d5eed4c5dcf08c516b62ac92a26b2 Mon Sep 17 00:00:00 2001 From: Angel Aviel Domaoan Date: Fri, 4 Feb 2022 09:30:47 +0000 Subject: [PATCH] Add concern for doctrine search --- app/controllers/api/doctrines_controller.rb | 24 ++------------------- app/controllers/concerns/doctrine_search.rb | 23 ++++++++++++++++++++ app/controllers/doctrines_controller.rb | 15 ++++--------- 3 files changed, 29 insertions(+), 33 deletions(-) create mode 100644 app/controllers/concerns/doctrine_search.rb diff --git a/app/controllers/api/doctrines_controller.rb b/app/controllers/api/doctrines_controller.rb index 7f53c92..016aa9b 100644 --- a/app/controllers/api/doctrines_controller.rb +++ b/app/controllers/api/doctrines_controller.rb @@ -1,6 +1,8 @@ module Api class DoctrinesController < BaseController + include DoctrineSearch + load_and_authorize_resource :doctrine, class: "Doctrine", only: %i[show] authorize_resource :doctrine, class: "Doctrine", only: %i[index] @@ -18,28 +20,6 @@ module Api private - def doctrine_search(search_params) - fulltext_fields = %i[content].freeze - - search = ::Doctrine.search do - fulltext search_params[:q], fields: fulltext_fields if search_params[:q].present? - - with(:subject_ids).any_of(search_params[:subject_ids]) if search_params[:subject_ids].present? - - with(:user_ids).any_of(search_params[:user_ids]) if search_params[:user_ids].present? - - if search_params[:created_at].present? - with(:created_at).between(Date.parse(search_params[:created_at])..Time.zone.today.to_date) - end - - without(:id).any_of(search_params[:exclude_ids]) if search_params[:exclude_ids].present? - - paginate page: search_params[:page] || 1, per_page: search_params[:per_page] || 20 - end - - search - end - def search_params params.permit(:created_at, :q, :page, :per_page, exclude_ids: [], subject_ids: [], user_ids: []) diff --git a/app/controllers/concerns/doctrine_search.rb b/app/controllers/concerns/doctrine_search.rb new file mode 100644 index 0000000..ae10a13 --- /dev/null +++ b/app/controllers/concerns/doctrine_search.rb @@ -0,0 +1,23 @@ +module DoctrineSearch + def doctrine_search(search_params) + fulltext_fields = %i[content].freeze + + search = ::Doctrine.search do + fulltext search_params[:q], fields: fulltext_fields if search_params[:q].present? + + with(:subject_ids).any_of(search_params[:subject_ids]) if search_params[:subject_ids].present? + + with(:user_ids).any_of(search_params[:user_ids]) if search_params[:user_ids].present? + + if search_params[:created_at].present? + with(:created_at).between(Date.parse(search_params[:created_at])..Time.zone.today.to_date) + end + + without(:id).any_of(search_params[:exclude_ids]) if search_params[:exclude_ids].present? + + paginate page: search_params[:page] || 1, per_page: search_params[:per_page] || 20 + end + + search + end +end diff --git a/app/controllers/doctrines_controller.rb b/app/controllers/doctrines_controller.rb index e595378..11c9cc3 100644 --- a/app/controllers/doctrines_controller.rb +++ b/app/controllers/doctrines_controller.rb @@ -1,8 +1,10 @@ class DoctrinesController < ApplicationController + include DoctrineSearch + load_and_authorize_resource :doctrine, class: "Doctrine" def index - @search = search_doctrines(search_params) + @search = doctrine_search(search_params) @doctrines = @search.results respond_to do |format| @@ -22,16 +24,7 @@ class DoctrinesController < ApplicationController def show; end private - def search_doctrines(args) - Doctrine.search do - if args[:subject_ids].present? - with(:subject_ids).any_of(args[:subject_ids].map(&:to_i)) - end - - paginate page: args[:page] || 1, per_page: args[:per_page] || 100 - end - end - + def search_params params.permit(subject_ids: []) end