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

add custom dynamic filter support #91

Open
wants to merge 3 commits 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
7 changes: 4 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ Only the object's id and the column you are searching on will be returned in JSO
By default autocomplete uses method name as column name. Now it can be specified using column_name options
`:column_name => 'name'`

#### json encoder
Autocomplete uses Yajl as JSON encoder/decoder, but you can specify your own
#### dynamic filter
If you want to use dynamic filter(where clause, scope, ...), it can be specified using a block.

class ProductsController < Admin::BaseController
autocomplete :brand, :name do |items|
CustomJSON::Encoder.encode(items)
# This block is executed on controller's instance context.
items.where("user_id = :user_id", :user_id => current_user.id).a_scope(params[:foo])
end
end

Expand Down
11 changes: 6 additions & 5 deletions lib/rails3-jquery-autocomplete/autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ def self.included(target)
#
# f.text_field :brand_name, :autocomplete => autocomplete_brand_name_products_path
#
#
# Yajl is used by default to encode results, if you want to use a different encoder
# you can specify your custom encoder via block
# You can specify your custom filter via block.
# The block is executed in a controller instance context.
#
# class ProductsController < Admin::BaseController
# autocomplete :brand, :name do |items|
# CustomJSONEncoder.encode(items)
# items.where("name = :name", :name => params[:name])
# end
# end
#
module ClassMethods
def autocomplete(object, method, options = {})
def autocomplete(object, method, options = {}, &block)
define_method("autocomplete_#{object}_#{method}") do

method = options[:column_name] if options.has_key?(:column_name)
Expand All @@ -53,6 +52,8 @@ def autocomplete(object, method, options = {})
class_name = options[:class_name] || object
items = get_autocomplete_items(:model => get_object(class_name), \
:options => options, :term => term, :method => method)

items = self.instance_exec(items, &block) if block
else
items = {}
end
Expand Down
2 changes: 1 addition & 1 deletion rails3-jquery-autocomplete.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('rr')
s.add_development_dependency('rcov')

s.files = Dir['lib/**/*'] + %w{CHANGELOG.MD LICENSE README.markdown Rakefile}
s.files = Dir['lib/**/*'] + %w{CHANGELOG.md LICENSE README.markdown Rakefile}
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
Expand Down
15 changes: 13 additions & 2 deletions test/lib/rails3-jquery-autocomplete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

module Rails3JQueryAutocomplete
class Rails3JQueryAutocompleteTest < ActionController::TestCase
ActorsController = Class.new(ActionController::Base)
ActorsController.autocomplete(:movie, :name)

class ::User
def id ; 1 end
end
class ::Movie ; end

class ActorsController < ActionController::Base
def current_user
@user ||= User.new
end
autocomplete(:movie, :name, { :display_value => :name }) do |items|
items.where("user_id = :user_id", :user_id => current_user.id)
end
end

context '#autocomplete_object_method' do
setup do
@controller = ActorsController.new
Expand All @@ -24,6 +34,7 @@ class ::Movie ; end
}) { @items }

mock(@controller).json_for_autocomplete(@items, :name, nil)
mock(@items).where("user_id = :user_id", :user_id => @controller.current_user.id) { @items }
get :autocomplete_movie_name, :term => 'query'
end

Expand Down