diff --git a/README.markdown b/README.markdown index 725e5418..2716d469 100644 --- a/README.markdown +++ b/README.markdown @@ -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 diff --git a/lib/rails3-jquery-autocomplete/autocomplete.rb b/lib/rails3-jquery-autocomplete/autocomplete.rb index fefc9146..68a80eba 100644 --- a/lib/rails3-jquery-autocomplete/autocomplete.rb +++ b/lib/rails3-jquery-autocomplete/autocomplete.rb @@ -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) @@ -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 diff --git a/rails3-jquery-autocomplete.gemspec b/rails3-jquery-autocomplete.gemspec index f27f9a84..9f9297c6 100644 --- a/rails3-jquery-autocomplete.gemspec +++ b/rails3-jquery-autocomplete.gemspec @@ -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"] diff --git a/test/lib/rails3-jquery-autocomplete_test.rb b/test/lib/rails3-jquery-autocomplete_test.rb index 4ca11a8d..edc7c505 100644 --- a/test/lib/rails3-jquery-autocomplete_test.rb +++ b/test/lib/rails3-jquery-autocomplete_test.rb @@ -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 @@ -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