docbrown/app/controllers/search_controller.rb
Alex 832f59ff6d
Search ClassifiedListings frontend code (#6372) [deploy]
* Add /search/classified_listings route

* Add classified_listings search controller action

* Add spec for /search/classified_listings request

* Begin implementing frontend code

* Add debounce to classified_listings search

* before_action to format page & per_page as Integer

* Use new fetchSearch helper to fix codeclimate

* Refactor to reduce listingSearch function length

* Refactor into separate updateListings function

* Move updateListings function outside class

* Move page size to constant

* Add brackets to conditionals

* Add comment for tags param

* Add JS doc

* Move params logic/cleanup to search_controller

* Move listingIDs to const

* Rename remove_blank_params & move to before_action
2020-03-03 14:13:40 -08:00

68 lines
1.6 KiB
Ruby

class SearchController < ApplicationController
before_action :authenticate_user!
before_action :format_integer_params
before_action :sanitize_params, only: %i[classified_listings]
CLASSIFIED_LISTINGS_PARAMS = %i[
category
classified_listing_search
page
per_page
tags
].freeze
def tags
tag_docs = Search::Tag.search_documents("name:#{params[:name]}* AND supported:true")
render json: { result: tag_docs }
rescue Search::Errors::Transport::BadRequest
render json: { result: [] }
end
def chat_channels
ccm_docs = Search::ChatChannelMembership.search_documents(
params: chat_channel_params.to_h, user_id: current_user.id,
)
render json: { result: ccm_docs }
end
def classified_listings
cl_docs = Search::ClassifiedListing.search_documents(
params: classified_listing_params.to_h,
)
render json: { result: cl_docs }
end
private
def chat_channel_params
accessible = %i[
per_page
page
channel_text
channel_type
channel_status
status
]
params.permit(accessible)
end
def classified_listing_params
params.permit(CLASSIFIED_LISTINGS_PARAMS)
end
def format_integer_params
params[:page] = params[:page].to_i if params[:page].present?
params[:per_page] = params[:per_page].to_i if params[:per_page].present?
end
# Some Elasticsearches/QueryBuilders treat values such as empty Strings and
# nil differently. This is a helper method to remove any params that are
# blank before passing it to Elasticsearch.
def sanitize_params
params.delete_if { |_k, v| v.blank? }
end
end