Skip to content

Commit bdcd6e8

Browse files
committed
controllers/search: Add support for search query filter processing
1 parent 6302100 commit bdcd6e8

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

app/controllers/search.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { restartableTask } from 'ember-concurrency';
77
import { bool, reads } from 'macro-decorators';
88

99
import { pagination } from '../utils/pagination';
10+
import { processSearchQuery } from '../utils/search';
1011

1112
export default class SearchController extends Controller {
1213
@service store;
@@ -61,6 +62,10 @@ export default class SearchController extends Controller {
6162
q = q.trim();
6263
}
6364

64-
return yield this.store.query('crate', { all_keywords, page, per_page, q, sort });
65+
let searchOptions = all_keywords
66+
? { page, per_page, sort, q, all_keywords }
67+
: { page, per_page, sort, ...processSearchQuery(q) };
68+
69+
return yield this.store.query('crate', searchOptions);
6570
}
6671
}

tests/acceptance/search-test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,42 @@ module('Acceptance | search', function (hooks) {
191191
await visit('/search?q=rust&page=3&per_page=15&sort=new&all_keywords=fire ball');
192192
assert.verifySteps(['/api/v1/crates']);
193193
});
194+
195+
test('supports `keyword:bla` filters', async function (assert) {
196+
this.server.get('/api/v1/crates', function (schema, request) {
197+
assert.step('/api/v1/crates');
198+
199+
assert.deepEqual(request.queryParams, {
200+
all_keywords: 'fire ball',
201+
page: '3',
202+
per_page: '15',
203+
q: 'rust',
204+
sort: 'new',
205+
});
206+
207+
return { crates: [], meta: { total: 0 } };
208+
});
209+
210+
await visit('/search?q=rust keywords:fire,ball&page=3&per_page=15&sort=new');
211+
assert.verifySteps(['/api/v1/crates']);
212+
});
213+
214+
test('`all_keywords` query parameter takes precedence over `keyword` filters', async function (assert) {
215+
this.server.get('/api/v1/crates', function (schema, request) {
216+
assert.step('/api/v1/crates');
217+
218+
assert.deepEqual(request.queryParams, {
219+
all_keywords: 'fire ball',
220+
page: '3',
221+
per_page: '15',
222+
q: 'rust keywords:foo',
223+
sort: 'new',
224+
});
225+
226+
return { crates: [], meta: { total: 0 } };
227+
});
228+
229+
await visit('/search?q=rust keywords:foo&page=3&per_page=15&sort=new&all_keywords=fire ball');
230+
assert.verifySteps(['/api/v1/crates']);
231+
});
194232
});

0 commit comments

Comments
 (0)