From 3729b2b8d2a177b261b0f7be8663fea87f506d38 Mon Sep 17 00:00:00 2001 From: Arun V Date: Wed, 7 Sep 2011 17:12:46 +0530 Subject: [PATCH 1/3] Corrected spelling to autocomplete in autocomplete with scope spec --- integration/spec/acceptance/autocomplete_spec.rb | 2 +- integration/spec/acceptance/support/paths.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/integration/spec/acceptance/autocomplete_spec.rb b/integration/spec/acceptance/autocomplete_spec.rb index f5ce5cee..a0d69a64 100644 --- a/integration/spec/acceptance/autocomplete_spec.rb +++ b/integration/spec/acceptance/autocomplete_spec.rb @@ -72,7 +72,7 @@ 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") diff --git a/integration/spec/acceptance/support/paths.rb b/integration/spec/acceptance/support/paths.rb index aacd5423..47ba1ada 100644 --- a/integration/spec/acceptance/support/paths.rb +++ b/integration/spec/acceptance/support/paths.rb @@ -25,7 +25,7 @@ def new_simple_form_page "/simple_forms/new" end - def new_scoped_cutocomplete_page + def new_scoped_autocomplete_page "/scoped_autocompletes/new" end end From cb3a7bb060666fddd433dc6bf65f658adbaf1265 Mon Sep 17 00:00:00 2001 From: Arun V Date: Thu, 8 Sep 2011 00:50:06 +0530 Subject: [PATCH 2/3] introduced parametrized scopes --- .../param_scoped_autocompletes_controller.rb | 13 +++++++++++++ integration/app/models/brand.rb | 1 + .../views/param_scoped_autocompletes/new.html.haml | 9 +++++++++ integration/config/routes.rb | 4 ++++ integration/db/schema.rb | 1 + integration/spec/acceptance/autocomplete_spec.rb | 11 +++++++++++ integration/spec/acceptance/support/paths.rb | 5 +++++ lib/rails3-jquery-autocomplete/orm/active_record.rb | 6 ++++++ lib/rails3-jquery-autocomplete/orm/mongoid.rb | 6 ++++++ 9 files changed, 56 insertions(+) create mode 100644 integration/app/controllers/param_scoped_autocompletes_controller.rb create mode 100644 integration/app/views/param_scoped_autocompletes/new.html.haml 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 a0d69a64..424d2d4d 100644 --- a/integration/spec/acceptance/autocomplete_spec.rb +++ b/integration/spec/acceptance/autocomplete_spec.rb @@ -77,5 +77,16 @@ 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 47ba1ada..d1d030e2 100644 --- a/integration/spec/acceptance/support/paths.rb +++ b/integration/spec/acceptance/support/paths.rb @@ -28,6 +28,11 @@ def new_simple_form_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..f0763781 100644 --- a/lib/rails3-jquery-autocomplete/orm/mongoid.rb +++ b/lib/rails3-jquery-autocomplete/orm/mongoid.rb @@ -21,9 +21,15 @@ 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) + + param_scopes.each do |scope| + items = items.send(scope[:scope],method(scope[:param]).call) + end unless param_scopes.empty? + end end end From fa3882ac2e186e8209b226a3d5228b3568a82891 Mon Sep 17 00:00:00 2001 From: Arun V Date: Thu, 8 Sep 2011 14:13:37 +0530 Subject: [PATCH 3/3] Param Scopes in Mongoid --- lib/rails3-jquery-autocomplete/orm/mongoid.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/rails3-jquery-autocomplete/orm/mongoid.rb b/lib/rails3-jquery-autocomplete/orm/mongoid.rb index f0763781..05f2a55b 100644 --- a/lib/rails3-jquery-autocomplete/orm/mongoid.rb +++ b/lib/rails3-jquery-autocomplete/orm/mongoid.rb @@ -24,12 +24,15 @@ def get_autocomplete_items(parameters) 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