Skip to content
This repository has been archived by the owner on Dec 22, 2018. It is now read-only.

Commit

Permalink
Properly escape \, % and _ in query.
Browse files Browse the repository at this point in the history
Based on fix proposed in original bug report. Fixes #158.
  • Loading branch information
Frank Groeneveld committed Feb 12, 2015
1 parent a3f8aed commit fbb1bea
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ gem 'jquery-rails'
# foundation form errors
gem 'foundation_rails_helper'

group :development do
# to use debugger
gem 'byebug'
# to use debugger
gem 'byebug', group: [:development, :test]

group :development do
# sqlite database during development
gem 'sqlite3'

Expand Down
7 changes: 4 additions & 3 deletions app/models/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ def self.active_labels(status)

scope :search, ->(term) {
if !term.nil?
term = '%' + term.downcase + '%'
where('LOWER(subject) LIKE ? OR LOWER(content) LIKE ?',
term, term)
term.gsub!(/[\\%_]/) { |m| "!#{m}" }
term = "%#{term.downcase}%"
where('LOWER(subject) LIKE ? ESCAPE ? OR LOWER(content) LIKE ? ESCAPE ?',
term, '!', term, '!')
end
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/tickets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ multiple:
message_id: 'test124@test124'

daves_problem:
subject: Dave has a problem
subject: Dave has a problem %@#_
content: Dave has a problem with his computer
user: dave
message_id: 'test125@test125'
5 changes: 5 additions & 0 deletions test/models/ticket_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ class TicketTest < ActiveSupport::TestCase
assert_equal last.created_at, last.updated_at
end

test 'should escape special char search' do
assert_equal 1, Ticket.search('%').count
assert_equal 1, Ticket.search('_').count
end

end

0 comments on commit fbb1bea

Please sign in to comment.