adding autocomplete controller and tests

git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@36 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7
master
andrew 2007-11-17 07:14:26 +00:00
parent f3c9f85e1d
commit abcc827626
4 changed files with 54 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,2 @@
module AutocompleteHelper
end

View File

@ -0,0 +1,7 @@
<ul>
<% if @items.empty? -%>
<li><strong>Ohtehnoz! No maches found!</strong></li>
<% else -%>
<%= @items.collect { |x| "<li>#{x.send(@value)}</li>" } %>
<% end -%>
</ul>

View File

@ -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