Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
ed412174df11
Commit
2cf7f3f4
authored
Mar 01, 2016
by
Yorick Peterse
Committed by
Robert Speicher
Mar 11, 2016
Browse files
Use ILIKE/LIKE for searching milestones
parent
db75ad81a350
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/models/milestone.rb
View file @
ed412174
...
...
@@ -58,9 +58,18 @@ class Milestone < ActiveRecord::Base
alias_attribute
:name
,
:title
class
<<
self
# Searches for milestones matching the given query.
#
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
#
# query - The search query as a String
#
# Returns an ActiveRecord::Relation.
def
search
(
query
)
query
=
"%
#{
query
}
%"
where
(
"title like ? or description like ?"
,
query
,
query
)
t
=
arel_table
pattern
=
"%
#{
query
}
%"
where
(
t
[
:title
].
matches
(
pattern
).
or
(
t
[
:description
].
matches
(
pattern
)))
end
end
...
...
spec/models/milestone_spec.rb
View file @
ed412174
...
...
@@ -181,4 +181,34 @@
expect
(
issue4
.
position
).
to
eq
(
42
)
end
end
describe
'.search'
do
let
(
:milestone
)
{
create
(
:milestone
,
title:
'foo'
,
description:
'bar'
)
}
it
'returns milestones with a matching title'
do
expect
(
described_class
.
search
(
milestone
.
title
)).
to
eq
([
milestone
])
end
it
'returns milestones with a partially matching title'
do
expect
(
described_class
.
search
(
milestone
.
title
[
0
..
2
])).
to
eq
([
milestone
])
end
it
'returns milestones with a matching title regardless of the casing'
do
expect
(
described_class
.
search
(
milestone
.
title
.
upcase
)).
to
eq
([
milestone
])
end
it
'returns milestones with a matching description'
do
expect
(
described_class
.
search
(
milestone
.
description
)).
to
eq
([
milestone
])
end
it
'returns milestones with a partially matching description'
do
expect
(
described_class
.
search
(
milestone
.
description
[
0
..
2
])).
to
eq
([
milestone
])
end
it
'returns milestones with a matching description regardless of the casing'
do
expect
(
described_class
.
search
(
milestone
.
description
.
upcase
)).
to
eq
([
milestone
])
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment