Update regex for views search filter.

This commit is contained in:
Arpit Jalan 2020-09-24 17:05:55 +05:30
parent 5b0ce25dd7
commit cdf45f4fe6
5 changed files with 38 additions and 38 deletions

View File

@ -12,8 +12,8 @@ const REGEXP_TAGS_PREFIX = /^(tags?:|#(?=[a-z0-9\-]+::tag))/gi;
const REGEXP_IN_PREFIX = /^(in|with):/gi;
const REGEXP_STATUS_PREFIX = /^status:/gi;
const REGEXP_MIN_POST_COUNT_PREFIX = /^min_post_count:/gi;
const REGEXP_MIN_VIEW_COUNT_PREFIX = /^min_view_count:/gi;
const REGEXP_MAX_VIEW_COUNT_PREFIX = /^max_view_count:/gi;
const REGEXP_MIN_VIEWS_PREFIX = /^min_views:/gi;
const REGEXP_MAX_VIEWS_PREFIX = /^max_views:/gi;
const REGEXP_POST_TIME_PREFIX = /^(before|after):/gi;
const REGEXP_TAGS_REPLACE = /(^(tags?:|#(?=[a-z0-9\-]+::tag))|::tag\s?$)/gi;
@ -95,8 +95,8 @@ export default Component.extend({
},
status: null,
min_post_count: null,
min_view_count: null,
max_view_count: null,
min_views: null,
max_views: null,
time: {
when: "before",
days: null,
@ -167,13 +167,13 @@ export default Component.extend({
);
this.setSearchedTermValue(
"searchedTerms.min_view_count",
REGEXP_MIN_VIEW_COUNT_PREFIX
"searchedTerms.min_views",
REGEXP_MIN_VIEWS_PREFIX
);
this.setSearchedTermValue(
"searchedTerms.max_view_count",
REGEXP_MAX_VIEW_COUNT_PREFIX
"searchedTerms.max_views",
REGEXP_MAX_VIEWS_PREFIX
);
},
@ -360,15 +360,15 @@ export default Component.extend({
},
@action
onChangeSearchTermMinViewCount(value) {
this.set("searchedTerms.min_view_count", value.length ? value : null);
this._updateSearchTermForMinViewCount();
onChangeSearchTermMinViews(value) {
this.set("searchedTerms.min_views", value.length ? value : null);
this._updateSearchTermForMinViews();
},
@action
onChangeSearchTermMaxViewCount(value) {
this.set("searchedTerms.max_view_count", value.length ? value : null);
this._updateSearchTermForMaxViewCount();
onChangeSearchTermMaxViews(value) {
this.set("searchedTerms.max_views", value.length ? value : null);
this._updateSearchTermForMaxViews();
},
@action
@ -653,19 +653,19 @@ export default Component.extend({
}
},
_updateSearchTermForMinViewCount() {
const match = this.filterBlocks(REGEXP_MIN_VIEW_COUNT_PREFIX);
const viewsCountFilter = this.get("searchedTerms.min_view_count");
_updateSearchTermForMinViews() {
const match = this.filterBlocks(REGEXP_MIN_VIEWS_PREFIX);
const viewsCountFilter = this.get("searchedTerms.min_views");
let searchTerm = this.searchTerm || "";
if (viewsCountFilter) {
if (match.length !== 0) {
searchTerm = searchTerm.replace(
match[0],
`min_view_count:${viewsCountFilter}`
`min_views:${viewsCountFilter}`
);
} else {
searchTerm += ` min_view_count:${viewsCountFilter}`;
searchTerm += ` min_views:${viewsCountFilter}`;
}
this._updateSearchTerm(searchTerm);
@ -675,19 +675,19 @@ export default Component.extend({
}
},
_updateSearchTermForMaxViewCount() {
const match = this.filterBlocks(REGEXP_MAX_VIEW_COUNT_PREFIX);
const viewsCountFilter = this.get("searchedTerms.max_view_count");
_updateSearchTermForMaxViews() {
const match = this.filterBlocks(REGEXP_MAX_VIEWS_PREFIX);
const viewsCountFilter = this.get("searchedTerms.max_views");
let searchTerm = this.searchTerm || "";
if (viewsCountFilter) {
if (match.length !== 0) {
searchTerm = searchTerm.replace(
match[0],
`max_view_count:${viewsCountFilter}`
`max_views:${viewsCountFilter}`
);
} else {
searchTerm += ` max_view_count:${viewsCountFilter}`;
searchTerm += ` max_views:${viewsCountFilter}`;
}
this._updateSearchTerm(searchTerm);

View File

@ -162,27 +162,27 @@
</div>
<div class="control-group pull-left">
<label class="control-label" for="search-min-view-count">{{i18n "search.advanced.min_view_count.label"}}</label>
<label class="control-label" for="search-min-views">{{i18n "search.advanced.min_views.label"}}</label>
<div class="controls">
{{input
type="number"
value=(readonly searchedTerms.min_view_count)
value=(readonly searchedTerms.min_views)
class="input-small"
id="search-min-view-count"
input=(action "onChangeSearchTermMinViewCount" value="target.value")
id="search-min-views"
input=(action "onChangeSearchTermMinViews" value="target.value")
}}
</div>
</div>
<div class="control-group pull-left">
<label class="control-label" for="search-max-view-count">{{i18n "search.advanced.max_view_count.label"}}</label>
<label class="control-label" for="search-max-views">{{i18n "search.advanced.max_views.label"}}</label>
<div class="controls">
{{input
type="number"
value=(readonly searchedTerms.max_view_count)
value=(readonly searchedTerms.max_views)
class="input-small"
id="search-max-view-count"
input=(action "onChangeSearchTermMaxViewCount" value="target.value")
id="search-max-views"
input=(action "onChangeSearchTermMaxViews" value="target.value")
}}
</div>
</div>

View File

@ -2124,9 +2124,9 @@ en:
label: Posted
before: before
after: after
min_view_count:
min_views:
label: Minimum Views
max_view_count:
max_views:
label: Maximum Views
hamburger_menu: "go to another topic list or category"

View File

@ -637,11 +637,11 @@ class Search
)", file_extensions: file_extensions)
end
advanced_filter(/^min_view_count:(\d+)$/i) do |posts, match|
advanced_filter(/^min_views:(\d+)$/i) do |posts, match|
posts.where("topics.views >= ?", match.to_i)
end
advanced_filter(/^max_view_count:(\d+)$/i) do |posts, match|
advanced_filter(/^max_views:(\d+)$/i) do |posts, match|
posts.where("topics.views <= ?", match.to_i)
end

View File

@ -1399,8 +1399,8 @@ describe Search do
post = Fabricate(:post, raw: 'Topic', topic: topic)
post2 = Fabricate(:post, raw: 'Topic', topic: topic2)
expect(Search.execute('Topic min_view_count:150').posts.map(&:id)).to eq([post2.id])
expect(Search.execute('Topic max_view_count:150').posts.map(&:id)).to eq([post.id])
expect(Search.execute('Topic min_views:150').posts.map(&:id)).to eq([post2.id])
expect(Search.execute('Topic max_views:150').posts.map(&:id)).to eq([post.id])
end
it 'can search for terms with dots' do