Skip to content

Minor spell correction #88

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions integration/app/models/brand.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions integration/config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions integration/db/schema.rb
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 12 additions & 1 deletion integration/spec/acceptance/autocomplete_spec.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion integration/spec/acceptance/support/paths.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions lib/rails3-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 10 additions & 1 deletion lib/rails3-jquery-autocomplete/orm/mongoid.rb
Original file line number Diff line number Diff line change
@@ -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