Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a :term_filter option to allow for input term transformation before lookup #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ This wouldn't really make much sense unless you use it with the "id_element" att

Only the object's id and the column you are searching on will be returned in JSON, so if your display_value method requires another parameter, make sure to fetch it with the :extra_data option

#### :term_filter

If you want to transform incoming data before it's used to search, you can use the :display_value option.

This option takes a message name as a parameter, and uses that method each invocation to transform the input term.

class ProductsController < Admin::BaseController
def transform_like_its_1984(term)
"#tubular {term}
end

autocomplete :brand, :name, :term_filter => :transform_like_its_1984
end

#### :scopes
Added option to use scopes. Pass scopes in an array.
Expand Down
10 changes: 9 additions & 1 deletion lib/rails3-jquery-autocomplete/autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def autocomplete(object, method, options = {})

method = options[:column_name] if options.has_key?(:column_name)

term = params[:term]
term = filter_term(params[:term], options[:term_filter])

if term && !term.blank?
#allow specifying fully qualified class name for model object
Expand Down Expand Up @@ -91,6 +91,14 @@ def json_for_autocomplete(items, method, extra_data=[])
hash
end
end

#
# Filters the input term against a specified method.
#
# Calls filter_method on self with term and returns the result.
def filter_term(term, filter_method)
returned_term = filter_method.nil? ? term : self.send(filter_method, term)
end
end
end

14 changes: 14 additions & 0 deletions test/lib/rails3-jquery-autocomplete/autocomplete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,19 @@ class AutocompleteTest < Test::Unit::TestCase
end
end
end

context '#filter_term' do
should 'return the term if :term_filter_method is blank' do
term = "test_term"
assert_equal term, filter_term(term, nil)
end

should 'apply the passed method to term' do
test_term = "test_term"
returned_term = "returned_term"
mock(self).send(:test_method, test_term) {returned_term}
assert_equal returned_term, filter_term(test_term, :test_method)
end
end
end
end
21 changes: 21 additions & 0 deletions test/lib/rails3-jquery-autocomplete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Rails3JQueryAutocomplete
class Rails3JQueryAutocompleteTest < ActionController::TestCase
ActorsController = Class.new(ActionController::Base)
ActorsController.autocomplete(:movie, :name)
DirectorsController = Class.new(ActionController::Base)
DirectorsController.autocomplete(:movie, :name, :term_filter => :test_term, :display_value => :name)

class ::Movie ; end

Expand Down Expand Up @@ -34,5 +36,24 @@ class ::Movie ; end
end
end
end

context '#autocomplete_object_method_filtered' do
setup do
@controller = DirectorsController.new
@items = {}
@options = { :display_value => :name, :term_filter => :test_term }
end

context 'term_filter is set' do
should "render the JSON items" do
mock(@controller).test_term("query") {"querty"}
mock(@controller).get_autocomplete_items({
:model => Movie, :method => :name, :options => @options, :term => "querty"
}) { @items }
mock(@controller).json_for_autocomplete(@items, :name, nil)
get :autocomplete_movie_name, :term => 'query'
end
end
end
end
end