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

searching using active_record scope #235

Open
wants to merge 1 commit 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
19 changes: 15 additions & 4 deletions lib/rails3-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def get_autocomplete_items(parameters)
term = parameters[:term]
method = parameters[:method]
options = parameters[:options]
search_scope = options[:search_scope]
scopes = Array(options[:scopes])
where = options[:where]
limit = get_autocomplete_limit(options)
Expand All @@ -24,8 +25,14 @@ def get_autocomplete_items(parameters)
scopes.each { |scope| items = items.send(scope) } unless 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)

if search_scope.blank?
items = items.where(get_autocomplete_where_clause(model, term, method, options))
else
items = items.send(search_scope, get_autocomplete_term_for_like(term, options))
end

items = items.limit(limit).order(order)
items = items.where(where) unless where.blank?

items
Expand All @@ -38,9 +45,13 @@ def get_autocomplete_select_clause(model, method, options)

def get_autocomplete_where_clause(model, term, method, options)
table_name = model.table_name
is_full_search = options[:full]
like_clause = (postgres?(model) ? 'ILIKE' : 'LIKE')
["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
["LOWER(#{table_name}.#{method}) #{like_clause} ?", get_autocomplete_term_for_like(term, options)]
end

def get_autocomplete_term_for_like(term, options)
is_full_search = options[:full]
"#{(is_full_search ? '%' : '')}#{term.downcase}%"
end

def postgres?(model)
Expand Down
18 changes: 18 additions & 0 deletions test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ class Dog ; end
end
end

context '#get_autocomplete_term_for_like' do
setup do
@term = 'query'
@options = {}
end

should 'return term with % added' do
assert_equal "query%", get_autocomplete_term_for_like(@term, @options)
end

context 'full search' do
should 'return term sourrounded by %%' do
@options[:full] = true
assert_equal "%query%", get_autocomplete_term_for_like(@term, @options)
end
end
end

context '#get_autocomplete_where_clause' do
setup do
@model = Object.new
Expand Down