summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2016-03-01 12:51:01 +0100
committerRobert Speicher <rspeicher@gmail.com>2016-03-11 15:25:21 -0500
commit1f5284e5ddf2ce9b555799f43ca73be32d9bdf67 (patch)
treea5a9c8ed4f26d76887c17da4bbfa358763f893be /app/models
parentdb615d0a7992d5118c3e9e8914064eb26970666b (diff)
downloadgitlab-ce-1f5284e5ddf2ce9b555799f43ca73be32d9bdf67.tar.gz
Use ILIKE/LIKE for searching snippets
Previously this used a regular LIKE which is case-sensitive on PostgreSQL. This ensures that for both PostgreSQL and MySQL the searching is case-insensitive similar to searching for projects.
Diffstat (limited to 'app/models')
-rw-r--r--app/models/snippet.rb24
1 files changed, 22 insertions, 2 deletions
diff --git a/app/models/snippet.rb b/app/models/snippet.rb
index dd3925c7a7d..35d05af38bf 100644
--- a/app/models/snippet.rb
+++ b/app/models/snippet.rb
@@ -113,12 +113,32 @@ class Snippet < ActiveRecord::Base
end
class << self
+ # Searches for snippets with a matching title or file name.
+ #
+ # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
+ #
+ # query - The search query as a String.
+ #
+ # Returns an ActiveRecord::Relation.
def search(query)
- where('(title LIKE :query OR file_name LIKE :query)', query: "%#{query}%")
+ t = Snippet.arel_table
+ pattern = "%#{query}%"
+
+ where(t[:title].matches(pattern).or(t[:file_name].matches(pattern)))
end
+ # Searches for snippets with matching content.
+ #
+ # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
+ #
+ # query - The search query as a String.
+ #
+ # Returns an ActiveRecord::Relation.
def search_code(query)
- where('(content LIKE :query)', query: "%#{query}%")
+ table = Snippet.arel_table
+ pattern = "%#{query}%"
+
+ where(table[:content].matches(pattern))
end
def accessible_to(user)