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

Bug with LIKE and slashes #6

Open
wants to merge 1 commit 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
Fix: fix mysql LIKE and slashes bug
  • Loading branch information
petRUShka committed Mar 12, 2011

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
commit 9fe2160646902a8526efb0c4bf5921455418be01
2 changes: 1 addition & 1 deletion lib/auto_complete.rb
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ def auto_complete_for(object, method, options = {})
define_method("auto_complete_for_#{object}_#{method}") do
model = object.to_s.camelize.constantize
find_options = {
:conditions => [ "LOWER(#{model.quoted_table_name}.#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ],
:conditions => [ "LOWER(#{model.quoted_table_name}.#{method}) LIKE ?", '%' + params[object][method].downcase.gsub(/\\/,"\\\\\\\\") + '%' ],
:order => "#{model.quoted_table_name}.#{method} ASC",
:limit => 10 }.merge!(options)

7 changes: 7 additions & 0 deletions test/auto_complete_test.rb
Original file line number Diff line number Diff line change
@@ -101,6 +101,13 @@ def test_default_auto_complete_for
assert_equal 10, default_auto_complete_find_options[:limit]
assert_equal ["LOWER(\"some_models\".some_field) LIKE ?", "%some_value%"], default_auto_complete_find_options[:conditions]
end

# auto_complete_for :some_model, :some_field but value contains slashes
def test_default_auto_complete_for_value_with_slashes
get :auto_complete_for_some_model_some_field, :some_model => { :some_field => "some_value\\some_other_value" }
default_auto_complete_find_options = @controller.items.proxy_options
assert_equal ["LOWER(\"some_models\".some_field) LIKE ?", "%some_value\\\\some_other_value%"], default_auto_complete_find_options[:conditions]
end

# auto_complete_for :some_model, :some_other_field do |items, params|
# items.scoped( { :conditions => [ "a_third_field = ?", params['some_model']['a_third_field'] ] })