Created
July 24, 2018 15:12
-
-
Save coryetzkorn/c138849c75c6370838fb51f5ad504bfb to your computer and use it in GitHub Desktop.
Hendrix Profile Query
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
@profiles = Profile | |
.where(visible: true) | |
.includes(:profile_type) | |
.joins(:user) | |
.where(:users => {:state => User.states[:active]}) | |
sort_by = params[:sort] ? params[:sort] : "recently-active" | |
case sort_by | |
when "recently-active" | |
@profiles = @profiles.order('users.last_sign_in_at DESC') | |
when "recently-updated" | |
@profiles = @profiles.order('profiles.updated_at DESC') | |
when "created-descending" | |
@profiles = @profiles.order('profiles.created_at DESC') | |
when "created-ascending" | |
@profiles = @profiles.order('profiles.created_at ASC') | |
else | |
# last-updated | |
@profiles = @profiles.order(updated_at: :asc) | |
end | |
if params[:zipcode] | |
if params[:radius].to_i > 0 | |
@profiles = @profiles.near(params[:zipcode], params[:radius].to_i) | |
end | |
end | |
if params[:keyword] | |
unless params[:keyword].blank? | |
keyword = params[:keyword].downcase | |
@profiles = @profiles.where("lower(profiles.name) LIKE ? OR lower(profiles.details) LIKE ?", "%#{keyword}%", "%#{keyword}%") | |
end | |
end | |
if params[:available] == "true" | |
@profiles = @profiles.where(available: true) | |
end | |
if !params[:types].blank? | |
@profiles = @profiles.where(profile_type: params[:types]) | |
end | |
if !params[:instruments].blank? | |
@profiles = @profiles.joins(:instruments).distinct.where(instruments: { id: params[:instruments] }) | |
end | |
if !params[:genres].blank? | |
@profiles = @profiles.joins(:genres).distinct.where(genres: { id: params[:genres] }) | |
end | |
if params[:image] == "true" | |
@profiles = @profiles.joins(:image) | |
end | |
if params[:songs] == "true" | |
@profiles = @profiles.joins(:songs) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment