diff --git a/app/controllers/autocomplete_controller.rb b/app/controllers/autocomplete_controller.rb new file mode 100644 index 0000000..9852436 --- /dev/null +++ b/app/controllers/autocomplete_controller.rb @@ -0,0 +1,27 @@ +class AutocompleteController < ApplicationController + append_before_filter :ensure_xhr + + ## + # Automatically finds and returns a nice list of things. It is stupid in that + # it only finds the first thing available, but it will find it all. + # + # It expects to have parameters passed in the form: + # + # 'brewery' => { 'name' => 'foo' } + # + # Only one of those sets. If this is done, then all will be peachy. + # + def index + key = params.keys.detect do |x| + x.to_s != 'action' and x.to_s != 'controller' + end + render :nothing => true, :status => 500 if key.nil? + @value = params[key].keys.first + class_name = key.camelize.constantize + render :nothing => true unless class_name.new.respond_to?(@value) + @items = class_name.find(:all, :order => "#{@value} ASC", :select => @value, + :conditions => [ "lower(#{@value}) LIKE ?", + "%#{params[key][@value].downcase}%" ]) + render :partial => 'autocomplete/results' + end +end diff --git a/app/helpers/autocomplete_helper.rb b/app/helpers/autocomplete_helper.rb new file mode 100644 index 0000000..1a77f08 --- /dev/null +++ b/app/helpers/autocomplete_helper.rb @@ -0,0 +1,2 @@ +module AutocompleteHelper +end diff --git a/app/views/autocomplete/_results.rhtml b/app/views/autocomplete/_results.rhtml new file mode 100644 index 0000000..4999c6c --- /dev/null +++ b/app/views/autocomplete/_results.rhtml @@ -0,0 +1,7 @@ +