From 13d20b087f261690553dff268ff39bb6893ddaa3 Mon Sep 17 00:00:00 2001 From: Greg Barnett Date: Tue, 7 Feb 2012 12:38:16 -0800 Subject: [PATCH] added cast_to_text option for non-string columns --- lib/rails3-jquery-autocomplete/orm/active_record.rb | 3 ++- .../rails3-jquery-autocomplete/orm/active_record_test.rb | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/rails3-jquery-autocomplete/orm/active_record.rb b/lib/rails3-jquery-autocomplete/orm/active_record.rb index 9606c0f2..fa6953fd 100644 --- a/lib/rails3-jquery-autocomplete/orm/active_record.rb +++ b/lib/rails3-jquery-autocomplete/orm/active_record.rb @@ -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) diff --git a/test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb b/test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb index e56dc705..56d0e3ce 100644 --- a/test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb +++ b/test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb @@ -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