Skip to content

Commit

Permalink
FEATURE: min_age and max_age search operators
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Jun 23, 2015
1 parent f101408 commit e85df6b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config/locales/server.en.yml
Expand Up @@ -2411,7 +2411,8 @@ en:
<tr><td><code>status:open</code></td><td><code>status:closed</code></td><td><code>status:archived</code></td><td><code>status:noreplies</code></td><td><code>status:singleuser</code></td></tr>
<tr><td><code>category:foo</code></td><td><code>user:foo</code></td><td colspan=3></td></tr>
<tr><td><code>in:likes</code></td><td><code>in:posted</code></td><td><code>in:watching</code></td><td><code>in:tracking</code></td><td><code>in:private</code></td></tr>
<tr><td><code>in:bookmarks</code></td><td colspan=4></td></tr>
<tr><td><code>in:bookmarks</code><td colspan=4></td></tr>
<tr><td><code>posts_count:num</code></td><td><code>min_age:days</code></td><td><code>max_age:days</code></td> <td colspan=2></td></tr>
</table>
</p>
<p>
Expand Down
10 changes: 10 additions & 0 deletions lib/search.rb
Expand Up @@ -219,6 +219,16 @@ def self.advanced_filters
posts.where("posts.user_id = #{user_id}")
end

advanced_filter(/min_age:(\d+)/) do |posts,match|
n = match.to_i
posts.where("topics.created_at > ?", n.days.ago)
end

advanced_filter(/max_age:(\d+)/) do |posts,match|
n = match.to_i
posts.where("topics.created_at < ?", n.days.ago)
end

private


Expand Down
12 changes: 12 additions & 0 deletions spec/components/search_spec.rb
Expand Up @@ -372,6 +372,18 @@ def search
end

describe 'Advanced search' do

it 'supports min_age and max_age' do
topic = Fabricate(:topic, created_at: 3.months.ago)
Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic)

expect(Search.execute('test min_age:100').posts.length).to eq(1)
expect(Search.execute('test min_age:10').posts.length).to eq(0)
expect(Search.execute('test max_age:10').posts.length).to eq(1)
expect(Search.execute('test max_age:100').posts.length).to eq(0)

end

it 'can find by status' do
post = Fabricate(:post, raw: 'hi this is a test 123 123')
topic = post.topic
Expand Down

0 comments on commit e85df6b

Please sign in to comment.