Created
July 16, 2020 08:46
-
-
Save BunHouth/8b1d2d43b7b859746ce19735957098d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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