Created
April 9, 2012 15:05
-
-
Save krisf/2344104 to your computer and use it in GitHub Desktop.
lol im a baddie
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 ProfilesController < AuthorizedController | |
before_filter :extract_location, :only => :search | |
def search | |
search_terms = params[:profile].keep_if {|key, val| !val.blank? && val != '0'} | |
search_terms.each{|key, val| search_terms[key] = true if val == '1'} | |
if @location_params[:name] | |
@locations = Location.near(@location_params[:name], @location_params[:within], {:select => "locations.*, profiles.*", :units => :km}).joins(:profile).has_user_role.where(:profiles => search_terms) | |
current_hometown_filter_for_search(@locations) | |
profile_ids = @locations.map{|loc| loc.profile_id}.compact.uniq | |
@profiles = Profile.where('profiles.id IN (?)', profile_ids).page(params[:page]).includes(:user).has_user_role | |
format_locations_json(@locations) | |
else | |
@profiles = Profile.where(search_terms).page(params[:page]).includes(:user).has_user_role | |
@locations = Profile.where(search_terms).has_user_role.includes(:locations).map{|p| p.locations}.flatten | |
current_hometown_filter_for_search(@locations) | |
format_locations_json(@locations) | |
end | |
@search_terms = search_terms | |
respond_to do |format| | |
format.html # search.html.erb | |
format.json { render json: @profiles } | |
end | |
end | |
protected | |
def extract_location | |
@location_params = { :current => (params[:profile][:current] == '1' ? true : false), :hometown => (params[:profile][:hometown] == '1' ? true : false) } | |
unless params[:profile][:location].blank? | |
@location_params[:name] = params[:profile][:location] | |
@location_params[:within] = params[:profile][:within].to_i | |
end | |
params[:profile].delete(:location) | |
params[:profile].delete(:within) | |
params[:profile].delete(:current) | |
params[:profile].delete(:hometown) | |
end | |
def current_hometown_filter_for_search(locations) | |
if @location_params[:current] == false || @location_params[:hometown] == false | |
@locations = locations.map do |loc| | |
if loc.current == @location_params[:current] | |
loc | |
elsif loc.hometown == @location_params[:hometown] | |
loc | |
end | |
end | |
@locations.compact! | |
else | |
@locations = locations | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment