Created
December 3, 2014 21:47
-
-
Save jwo/a1c041ba8a0ed5fe3272 to your computer and use it in GitHub Desktop.
Search by company and first/last name. But: be able to choose if its first/last AND company vs first/last OR company
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 ContactSearch | |
attr_reader :params | |
def initialize(params) | |
@params = params | |
@all_filters = params[:all_filters].to_s == "true" | |
end | |
def search | |
t = Contact.arel_table | |
queries = [] | |
if params[:company].present? | |
queries << t[:company].matches("%#{params[:company]}%").to_sql | |
end | |
if params[:name].present? | |
queries << t[:first_name].matches("%#{params[:name]}%").or(t[:last_name].matches("%#{params[:name]}%")).to_sql | |
end | |
if @all_filters | |
sql = queries.join(" AND ") | |
else | |
sql = queries.join(" OR ") | |
end | |
@contacts = Contact.where(sql) | |
end | |
end |
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
App.PlacesController = Ember.Controller.extend({ | |
allFilters: false, | |
nameFilter: "", | |
companyFilter: "", | |
filteredPlaces: function() { | |
var allFilters = this.get('allFilters'); | |
var nameFilter = this.get('nameFilter'); | |
var companyFilter = this.get('companyFilter'); | |
return this.store.find('contact', {"all_filters":allFilters, | |
"company_name": companyFilter, | |
"name": nameFilter | |
}); | |
}.property('allFilters', 'nameFilter', 'companyFilter'), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment