Browse Source

Add concern for doctrine search

pull/17/head
Angel Aviel Domaoan 4 years ago committed by Angel Aviel Domaoan
parent
commit
abbd7400e2
  1. 24
      app/controllers/api/doctrines_controller.rb
  2. 23
      app/controllers/concerns/doctrine_search.rb
  3. 15
      app/controllers/doctrines_controller.rb

24
app/controllers/api/doctrines_controller.rb

@ -1,6 +1,8 @@
module Api module Api
class DoctrinesController < BaseController class DoctrinesController < BaseController
include DoctrineSearch
load_and_authorize_resource :doctrine, class: "Doctrine", only: %i[show] load_and_authorize_resource :doctrine, class: "Doctrine", only: %i[show]
authorize_resource :doctrine, class: "Doctrine", only: %i[index] authorize_resource :doctrine, class: "Doctrine", only: %i[index]
@ -18,28 +20,6 @@ module Api
private 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 def search_params
params.permit(:created_at, :q, :page, :per_page, params.permit(:created_at, :q, :page, :per_page,
exclude_ids: [], subject_ids: [], user_ids: []) exclude_ids: [], subject_ids: [], user_ids: [])

23
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

15
app/controllers/doctrines_controller.rb

@ -1,8 +1,10 @@
class DoctrinesController < ApplicationController class DoctrinesController < ApplicationController
include DoctrineSearch
load_and_authorize_resource :doctrine, class: "Doctrine" load_and_authorize_resource :doctrine, class: "Doctrine"
def index def index
@search = search_doctrines(search_params) @search = doctrine_search(search_params)
@doctrines = @search.results @doctrines = @search.results
respond_to do |format| respond_to do |format|
@ -22,16 +24,7 @@ class DoctrinesController < ApplicationController
def show; end def show; end
private 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 def search_params
params.permit(subject_ids: []) params.permit(subject_ids: [])
end end

Loading…
Cancel
Save