diff --git a/integration/app/controllers/param_scoped_autocompletes_controller.rb b/integration/app/controllers/param_scoped_autocompletes_controller.rb new file mode 100644 index 00000000..7ab34821 --- /dev/null +++ b/integration/app/controllers/param_scoped_autocompletes_controller.rb @@ -0,0 +1,13 @@ +class ParamScopedAutocompletesController < ApplicationController + + autocomplete :brand, :name, :param_scopes => [{:scope => :custom_state,:param => :state}] + + def new + @product = Product.new + end + + private + def state + false + end +end diff --git a/integration/app/models/brand.rb b/integration/app/models/brand.rb index 8872f3a4..505396ba 100644 --- a/integration/app/models/brand.rb +++ b/integration/app/models/brand.rb @@ -15,6 +15,7 @@ class Brand < ActiveRecord::Base scope :active, where(:state => true) scope :with_address, joins(:address) + scope :custom_state, ->(state) { where(:state => state)} belongs_to :address # embeds_one :address end diff --git a/integration/app/views/param_scoped_autocompletes/new.html.haml b/integration/app/views/param_scoped_autocompletes/new.html.haml new file mode 100644 index 00000000..cc336353 --- /dev/null +++ b/integration/app/views/param_scoped_autocompletes/new.html.haml @@ -0,0 +1,9 @@ +%h1 Scoped Autocomplete + += form_for @product do |form| + %p + = form.label :name + = form.text_field :name + %p + = form.label :brand_name + = form.autocomplete_field :brand_name, autocomplete_brand_name_param_scoped_autocompletes_path diff --git a/integration/config/routes.rb b/integration/config/routes.rb index 9e183922..da0b6f09 100644 --- a/integration/config/routes.rb +++ b/integration/config/routes.rb @@ -27,6 +27,10 @@ resources :scoped_autocompletes do get :autocomplete_brand_name, :on => :collection end + + resources :param_scoped_autocompletes do + get :autocomplete_brand_name, :on => :collection + end end #== Route Map # Generated on 25 Apr 2011 09:55 diff --git a/integration/db/schema.rb b/integration/db/schema.rb index cbdba74f..4c87422b 100644 --- a/integration/db/schema.rb +++ b/integration/db/schema.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. diff --git a/integration/spec/acceptance/autocomplete_spec.rb b/integration/spec/acceptance/autocomplete_spec.rb index f5ce5cee..424d2d4d 100644 --- a/integration/spec/acceptance/autocomplete_spec.rb +++ b/integration/spec/acceptance/autocomplete_spec.rb @@ -72,10 +72,21 @@ kappa_brand = Brand.find_by_name('Kappa') kappa_brand.address = Address.create! kappa_brand.save! - visit new_scoped_cutocomplete_page + visit new_scoped_autocomplete_page fill_in("Brand name", :with => "ka") choose_autocomplete_result "Kappa" find_field("Brand name").value.should include("Kappa") end + + scenario "Autocomplete with param scope" do + kappa_brand = Brand.find_by_name('Kappa') + kappa_brand.address = Address.create! + kappa_brand.save! + visit new_param_scoped_autocomplete_page + fill_in("Brand name", :with => "ka") + choose_autocomplete_result "Kappler" + find_field("Brand name").value.should include("Kappler") + end + end end diff --git a/integration/spec/acceptance/support/paths.rb b/integration/spec/acceptance/support/paths.rb index aacd5423..d1d030e2 100644 --- a/integration/spec/acceptance/support/paths.rb +++ b/integration/spec/acceptance/support/paths.rb @@ -25,9 +25,14 @@ def new_simple_form_page "/simple_forms/new" end - def new_scoped_cutocomplete_page + def new_scoped_autocomplete_page "/scoped_autocompletes/new" end + + def new_param_scoped_autocomplete_page + "/param_scoped_autocompletes/new" + end + end RSpec.configuration.include NavigationHelpers, :type => :acceptance diff --git a/lib/rails3-jquery-autocomplete/orm/active_record.rb b/lib/rails3-jquery-autocomplete/orm/active_record.rb index fe4292b1..f666f28b 100644 --- a/lib/rails3-jquery-autocomplete/orm/active_record.rb +++ b/lib/rails3-jquery-autocomplete/orm/active_record.rb @@ -14,6 +14,7 @@ def get_autocomplete_items(parameters) method = parameters[:method] options = parameters[:options] scopes = Array(options[:scopes]) + param_scopes = Array(options[:param_scopes]) limit = get_autocomplete_limit(options) order = get_autocomplete_order(method, options, model) @@ -22,6 +23,11 @@ def get_autocomplete_items(parameters) scopes.each { |scope| items = items.send(scope) } unless scopes.empty? + param_scopes.each do |scope| + items = items.send(scope[:scope],method(scope[:param]).call) + end unless param_scopes.empty? + + items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model] items = items.where(get_autocomplete_where_clause(model, term, method, options)). limit(limit).order(order) diff --git a/lib/rails3-jquery-autocomplete/orm/mongoid.rb b/lib/rails3-jquery-autocomplete/orm/mongoid.rb index f1e41609..05f2a55b 100644 --- a/lib/rails3-jquery-autocomplete/orm/mongoid.rb +++ b/lib/rails3-jquery-autocomplete/orm/mongoid.rb @@ -21,9 +21,18 @@ def get_autocomplete_items(parameters) term = parameters[:term] limit = get_autocomplete_limit(options) order = get_autocomplete_order(method, options) + param_scopes = Array(options[:param_scopes]) search = (is_full_search ? '.*' : '^') + term + '.*' - items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order) + + items = model.scoped + + param_scopes.each do |scope| + items = items.send(scope[:scope],method(scope[:param]).call) + end unless param_scopes.empty? + + items = items.where(method.to_sym => /#{search}/i).limit(limit).order_by(order) + end end end