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

Backport: fixed integer value auto completer #121 #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/scoped_search/auto_complete_builder.rb
Expand Up @@ -197,7 +197,7 @@ def complete_value
return complete_date_value if field.temporal?
return complete_key_value(field, token, val) if field.key_field

opts = value_conditions(field.quoted_field, val)
opts = value_conditions(field, val)
opts.merge!(:limit => 20, :select => "DISTINCT #{field.quoted_field}")

return completer_scope(field).all(opts).map(&field.field).compact.map{|v| v.to_s =~ /\s+/ ? "\"#{v}\"" : v}
Expand Down Expand Up @@ -233,7 +233,7 @@ def complete_date_value
# complete values in a key-value schema
def complete_key_value(field, token, val)
key_name = token.sub(/^.*\./,"")
key_opts = value_conditions(field.quoted_field,val).merge(:conditions => {field.key_field => key_name})
key_opts = value_conditions(field,val).merge(:conditions => {field.key_field => key_name})
key_klass = field.key_klass.first(key_opts)
raise ScopedSearch::QueryNotSupported, "Field '#{key_name}' not recognized for searching!" if key_klass.nil?

Expand All @@ -250,7 +250,7 @@ def complete_key_value(field, token, val)

#this method returns conditions for selecting completion from partial value
def value_conditions(field_name, val)
return val.blank? ? {} : {:conditions => "#{field_name} LIKE '#{val.gsub("'","''")}%'".tr_s('%*', '%')}
return val.blank? ? {} : {:conditions => "CAST(#{field_name.quoted_field} AS CHAR(50)) LIKE '#{val.gsub("'","''")}%'".tr_s('%*', '%')}
end

# This method complete infix operators by field type
Expand Down
9 changes: 8 additions & 1 deletion spec/integration/auto_complete_spec.rb
Expand Up @@ -35,7 +35,8 @@ class ::Foo < ActiveRecord::Base
has_many :bars
default_scope { order(:string) }

scoped_search :on => [:string, :int, :date]
scoped_search :on => [:string, :date]
scoped_search :on => [:int], :complete_value => true
scoped_search :on => :another, :default_operator => :eq, :alias => :alias
scoped_search :on => :explicit, :only_explicit => true, :complete_value => true
scoped_search :on => :deprecated, :complete_enabled => false
Expand Down Expand Up @@ -213,5 +214,11 @@ class ::Infoo < ::Foo
end

end

context 'autocompleting integer comparisons' do
it 'should autocomplete numerical fields' do
Foo.complete_for('int > 2').first.should match(/9/)
end
end
end
end