Permalink
Browse files

Tests are green but generator is failing

  • Loading branch information...
1 parent 0a6ccfb commit e26551460afe4be573ef57b974b0d03f6027ad68 Emmanuel Delgado committed Oct 20, 2010
@@ -1,66 +1,7 @@
-require 'form_helper'
-require 'rails3-jquery-autocomplete-helpers'
-
-module Rails3JQueryAutocomplete
- def self.included(base)
- base.extend(ClassMethods)
- end
-
-
- # Inspired on DHH's autocomplete plugin
- #
- # Usage:
- #
- # class ProductsController < Admin::BaseController
- # autocomplete :brand, :name
- # end
- #
- # This will magically generate an action autocomplete_brand_name, so,
- # don't forget to add it on your routes file
- #
- # resources :products do
- # get :autocomplete_brand_name, :on => :collection
- # end
- #
- # Now, on your view, all you have to do is have a text field like:
- #
- # f.text_field :brand_name, :autocomplete => autocomplete_brand_name_products_path
- #
- #
- module ClassMethods
- def autocomplete(object, method, options = {})
-
- define_method("autocomplete_#{object}_#{method}") do
-
- object = Helpers.get_object(object)
- implementation = Helpers.get_implementation(object)
-
- if implementation && params[:term] && !params[:term].empty?
-
- order = Helpers.get_order(implementation, method, options)
- limit = Helpers.get_limit(options)
-
- items = case implementation
- when :mongoid
- search = (options[:full] ? '.*' : '^') + params[:term] + '.*'
- items = object.where(method.to_sym => /#{search}/i) \
- .limit(limit).order_by(order)
- when :activerecord
- items = object.where(["LOWER(#{method}) LIKE ?", "#{(options[:full] ? '%' : '')}#{params[:term].downcase}%"]) \
- .limit(limit).order(order)
- end
-
- else
- items = {}
- end
-
- render :json => Helpers.json_for_autocomplete(items, (options[:display_value] ? options[:display_value] : method))
- end
- end
- end
-
-end
+require 'rails3-jquery-autocomplete/form_helper'
+require 'rails3-jquery-autocomplete/helpers'
+require 'rails3-jquery-autocomplete/autocomplete'
class ActionController::Base
- include Rails3JQueryAutocomplete
+ extend Rails3JQueryAutocomplete::Autocomplete
end
@@ -0,0 +1,55 @@
+module Rails3JQueryAutocomplete
+
+ # Inspired on DHH's autocomplete plugin
+ #
+ # Usage:
+ #
+ # class ProductsController < Admin::BaseController
+ # autocomplete :brand, :name
+ # end
+ #
+ # This will magically generate an action autocomplete_brand_name, so,
+ # don't forget to add it on your routes file
+ #
+ # resources :products do
+ # get :autocomplete_brand_name, :on => :collection
+ # end
+ #
+ # Now, on your view, all you have to do is have a text field like:
+ #
+ # f.text_field :brand_name, :autocomplete => autocomplete_brand_name_products_path
+ #
+ #
+ module Autocomplete
+ def autocomplete(object, method, options = {})
+
+ define_method("autocomplete_#{object}_#{method}") do
+
+ object = Helpers.get_object(object)
+ implementation = Helpers.get_implementation(object)
+
+ if implementation && params[:term] && !params[:term].empty?
+
+ order = Helpers.get_order(implementation, method, options)
+ limit = Helpers.get_limit(options)
+
+ items = case implementation
+ when :mongoid
+ search = (options[:full] ? '.*' : '^') + params[:term] + '.*'
+ items = object.where(method.to_sym => /#{search}/i) \
+ .limit(limit).order_by(order)
+ when :activerecord
+ items = object.where(["LOWER(#{method}) LIKE ?", "#{(options[:full] ? '%' : '')}#{params[:term].downcase}%"]) \
+ .limit(limit).order(order)
+ end
+
+ else
+ items = {}
+ end
+
+ render :json => Helpers.json_for_autocomplete(items, (options[:display_value] ? options[:display_value] : method))
+ end
+ end
+ end
+
+end
@@ -53,4 +53,4 @@ class ActionView::Helpers::FormBuilder #:nodoc:
def autocomplete_field(method, source, options = {})
@template.autocomplete_field(@object_name, method, source, options)
end
-end
+end
@@ -1,8 +1,5 @@
-require 'form_helper'
-
module Rails3JQueryAutocomplete
-
- class Helpers
+ module Helpers
def self.json_for_autocomplete(items, method)
items.collect {|item| {"id" => item.id, "label" => item.send(method), "value" => item.send(method)}}
@@ -37,5 +34,4 @@ def self.get_limit(options)
end
end
-
end
@@ -1,88 +1,100 @@
require "test_helper"
require 'support/active_record'
-class ActorsControllerTest < ActionController::TestCase
+module Rails3JQueryAutocomplete
+ module Test
+ module ActiveRecord
+
+ class ActorsControllerTest < ActionController::TestCase
+
+ include Rails3JQueryAutocomplete::Test::ActiveRecord
+
+ setup do
+ @controller = ActorsController.new
+ end
+
+ context "the autocomplete gem" do
+
+ should "be able to access the autocomplete action regardless of the quality of param[:term]" do
+ get :autocomplete_movie_name
+ assert_response :success
+
+ get :autocomplete_movie_name, :term => ''
+ assert_response :success
+
+ get :autocomplete_movie_name, :term => nil
+ assert_response :success
+
+ get :autocomplete_movie_name, :term => 'Al'
+ assert_response :success
+ end
+
+ should "respond with expected json" do
+ get :autocomplete_movie_name, :term => 'Al'
+ json_response = JSON.parse(@response.body)
+ assert_equal(json_response.first["label"], @movie.name)
+ assert_equal(json_response.first["value"], @movie.name)
+ assert_equal(json_response.first["id"], @movie.id)
+ end
+
+ should "return results in alphabetical order by default" do
+ get :autocomplete_movie_name, :term => 'Al'
+ json_response = JSON.parse(@response.body)
+ assert_equal(json_response.first["label"], "Alpha")
+ assert_equal(json_response.last["label"], "Alzpha")
+ end
+
+ should "be able to sort in other ways if desired" do
+ ActorsController.send(:autocomplete, :movie, :name, {:order => "name DESC"})
+
+ get :autocomplete_movie_name, :term => 'Al'
+ json_response = JSON.parse(@response.body)
+ assert_equal(json_response.first["label"], "Alzpha")
+ assert_equal(json_response.last["label"], "Alpha")
+ end
+
+ should "be able to limit the results" do
+ ActorsController.send(:autocomplete, :movie, :name, {:limit => 1})
+
+ get :autocomplete_movie_name, :term => 'Al'
+ json_response = JSON.parse(@response.body)
+ assert_equal(json_response.length, 1)
+ end
+
+ should "ignore case of search term and results" do
+ @movie = Movie.create(:name => 'aLpHa')
+
+ ActorsController.send(:autocomplete, :movie, :name)
+
+ get :autocomplete_movie_name, :term => 'Al'
+ json_response = JSON.parse(@response.body)
+ assert_equal(json_response.length, Movie.count)
+ assert_equal(json_response.last["label"], 'aLpHa')
+ end
+
+ should "match term to letters in middle of words when full-text search is on" do
+ ActorsController.send(:autocomplete, :movie, :name, {:full => true})
+
+ get :autocomplete_movie_name, :term => 'ph'
+ json_response = JSON.parse(@response.body)
+ assert_equal(json_response.length, Movie.count)
+ assert_equal(json_response.first["label"], @movie.name)
+ end
+
+ should "be able to customize what is displayed" do
+ ActorsController.send(:autocomplete, :movie, :name, {:display_value => :display_name})
+
+ get :autocomplete_movie_name, :term => 'Al'
+
+ json_response = JSON.parse(@response.body)
+
+ assert_equal(@movie.display_name, json_response.first["label"])
+ assert_equal(@movie.display_name, json_response.first["value"])
+ assert_equal(@movie.id, json_response.first["id"])
+ end
+ end
+ end
- include Rails3JQueryAutocomplete::ActiveRecord::Test
-
- context "the autocomplete gem" do
-
- should "be able to access the autocomplete action regardless of the quality of param[:term]" do
- get :autocomplete_movie_name
- assert_response :success
-
- get :autocomplete_movie_name, :term => ''
- assert_response :success
-
- get :autocomplete_movie_name, :term => nil
- assert_response :success
-
- get :autocomplete_movie_name, :term => 'Al'
- assert_response :success
- end
-
- should "respond with expected json" do
- get :autocomplete_movie_name, :term => 'Al'
- json_response = JSON.parse(@response.body)
- assert_equal(json_response.first["label"], @movie.name)
- assert_equal(json_response.first["value"], @movie.name)
- assert_equal(json_response.first["id"], @movie.id)
- end
-
- should "return results in alphabetical order by default" do
- get :autocomplete_movie_name, :term => 'Al'
- json_response = JSON.parse(@response.body)
- assert_equal(json_response.first["label"], "Alpha")
- assert_equal(json_response.last["label"], "Alzpha")
- end
-
- should "be able to sort in other ways if desired" do
- ActorsController.send(:autocomplete, :movie, :name, {:order => "name DESC"})
-
- get :autocomplete_movie_name, :term => 'Al'
- json_response = JSON.parse(@response.body)
- assert_equal(json_response.first["label"], "Alzpha")
- assert_equal(json_response.last["label"], "Alpha")
- end
-
- should "be able to limit the results" do
- ActorsController.send(:autocomplete, :movie, :name, {:limit => 1})
-
- get :autocomplete_movie_name, :term => 'Al'
- json_response = JSON.parse(@response.body)
- assert_equal(json_response.length, 1)
- end
-
- should "ignore case of search term and results" do
- @movie = Movie.create(:name => 'aLpHa')
-
- ActorsController.send(:autocomplete, :movie, :name)
-
- get :autocomplete_movie_name, :term => 'Al'
- json_response = JSON.parse(@response.body)
- assert_equal(json_response.length, Movie.count)
- assert_equal(json_response.last["label"], 'aLpHa')
- end
-
- should "match term to letters in middle of words when full-text search is on" do
- ActorsController.send(:autocomplete, :movie, :name, {:full => true})
-
- get :autocomplete_movie_name, :term => 'ph'
- json_response = JSON.parse(@response.body)
- assert_equal(json_response.length, Movie.count)
- assert_equal(json_response.first["label"], @movie.name)
- end
-
- should "be able to customize what is displayed" do
- ActorsController.send(:autocomplete, :movie, :name, {:display_value => :display_name})
-
- get :autocomplete_movie_name, :term => 'Al'
-
- json_response = JSON.parse(@response.body)
-
- assert_equal(@movie.display_name, json_response.first["label"])
- assert_equal(@movie.display_name, json_response.first["value"])
- assert_equal(@movie.id, json_response.first["id"])
end
end
end
@@ -1,5 +1,5 @@
require 'test_helper'
-require 'generators/autocomplete_generator'
+require 'rails3-jquery-autocomplete/generators/autocomplete_generator'
class AutocompleteGeneratorTest < Test::Unit::TestCase
def setup
Oops, something went wrong.

0 comments on commit e265514

Please sign in to comment.