Skip to content

Instantly share code, notes, and snippets.

@jwo
Created December 3, 2014 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwo/a1c041ba8a0ed5fe3272 to your computer and use it in GitHub Desktop.
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
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
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