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

Added cast_to_text option for non string fields #138

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
3 changes: 2 additions & 1 deletion lib/rails3-jquery-autocomplete/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ 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}%"]
cast_to_text = options[:cast_to_text].present? ? ['CAST(', ' AS TEXT)'] : ['','']
["LOWER(#{cast_to_text[0]}#{table_name}.#{method}#{cast_to_text[1]}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]
end

def postgres?(model)
Expand Down
6 changes: 6 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 @@ -103,6 +103,12 @@ class Dog ; end
mock(self).postgres?(@model) { true }
assert_equal ["LOWER(table_name.method) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
end

should 'return options for where with CAST as TEXT when field is not string or text' do
mock(self).postgres?(@model) { true }
@options[:cast_to_text] = true
assert_equal ["LOWER(CAST(table_name.method AS TEXT)) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
end
end

context 'full search' do
Expand Down