My autocomplet works, and it looks decent enough, but oh boy, the controller is a mess. The autocomplete searches across multiple models which does not help things, and totalling up the found sets ect - there are country flags for customers etc.
I'd like to extract this to a partial ideally or another way to make the code easier to deal with.
def autocomplete
customer_count = (Customer.select("company").where("LOWER(company) LIKE ? AND deleted != ?", "#{params[:term].downcase}%", true).count) + (Product.select("model_number,description").where("LOWER(description) LIKE ? OR LOWER(model_number) LIKE ? AND deleted != ?", "#{params[:term].downcase}%", "#{params[:term].downcase}%", true).count) + (Order.where("LOWER(order_number) LIKE ? AND deleted != true","#{params[:term].downcase}%").count)
@customers = [{:label => (customer_count > 1 ? "<div style='font-weight: bold; text-align: left;'>Show all #{customer_count} records</div" : customer_count > 0 ? '' : "<div style='font-weight: bold; text-align: left;'>No records found</div>"), :link => "/customers?company=#{params[:term].downcase}"}]
@customers.concat(Customer.select("id, company, country").where("LOWER(company) LIKE ? AND deleted != ?", "#{params[:term].downcase}%", true).limit(24).collect{|customer|{:label=>("<div style='float:left;margin-top:4px;margin-right:8px;'><img alt='Mini_usa' src='/images/#{customer.country_div}'/></div> #{customer.company}").html_safe,:link =>"/customers/#{customer.id}"}})
@customers.concat(Product.select("id, model_number, description").where("LOWER(description) LIKE ? OR LOWER(model_number) LIKE ? AND deleted != ?","#{params[:term].downcase}%","#{params[:term].downcase}%",true).limit(24).collect{|product|{:label => ("#{product.model_number.titlecase} #{product.description.titlecase}").html_safe,:link =>"/products/#{product.model_number}"}})
@customers.concat(Order.select("id, order_number").where("LOWER(order_number) LIKE ? AND deleted != true","#{params[:term].downcase}%").limit(24).collect{ |order| { :label => ("#{order.order_number}").html_safe, :link =>"/orders/#{order.id}" } })
end