From abcc827626289b11a2f26a861ab299b21c4fd857 Mon Sep 17 00:00:00 2001 From: andrew Date: Sat, 17 Nov 2007 07:14:26 +0000 Subject: [PATCH] adding autocomplete controller and tests git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@36 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7 --- app/controllers/autocomplete_controller.rb | 27 +++++++++++++++++++ app/helpers/autocomplete_helper.rb | 2 ++ app/views/autocomplete/_results.rhtml | 7 +++++ .../autocomplete_controller_test.rb | 18 +++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 app/controllers/autocomplete_controller.rb create mode 100644 app/helpers/autocomplete_helper.rb create mode 100644 app/views/autocomplete/_results.rhtml create mode 100644 test/functional/autocomplete_controller_test.rb 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 @@ + diff --git a/test/functional/autocomplete_controller_test.rb b/test/functional/autocomplete_controller_test.rb new file mode 100644 index 0000000..29c570d --- /dev/null +++ b/test/functional/autocomplete_controller_test.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'autocomplete_controller' + +# Re-raise errors caught by the controller. +class AutocompleteController; def rescue_action(e) raise e end; end + +class AutocompleteControllerTest < Test::Unit::TestCase + def setup + @controller = AutocompleteController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + # Replace this with your real tests. + def test_truth + assert true + end +end