Skip to content

Instantly share code, notes, and snippets.

@BunHouth
Created July 16, 2020 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BunHouth/8b1d2d43b7b859746ce19735957098d2 to your computer and use it in GitHub Desktop.
Save BunHouth/8b1d2d43b7b859746ce19735957098d2 to your computer and use it in GitHub Desktop.
class SearchController < FrontendController
def index
@locations = locations
@search = Search.new(params, current_guest)
result = @search.paginate
respond_to do |format|
@data = { data: @search.search_results, total_count: @search.paginate[:total_count], total_pages: @search.paginate[:total_pages], per_page: 20, page: params[:page]}
query_params = params.except(:controller, :action, :search, :query).as_json
format.html {
redirect_to(url_builder(params[:query].parameterize, query_params)) unless request.path.include?("/#{params[:query]}")
}
format.json { render json: @data, location: url_builder(params[:query].parameterize, query_params) }
end
end
private
def locations
data = Location.search "*", load: false
data.map { |result| { name: result.name, listings_count: result.listings_count } }
end
def url_builder(query, query_params)
query = params[:query].split('hotels-in-').last if params[:level] == 'true' && params[:query].include?('hotels-in-')
params[:level] == 'true' ? hotel_in_path("hotels-in-#{query}", query_params) : search_path(query, query_params)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment