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

View File

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

View File

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

View File

@ -637,11 +637,11 @@ class Search
)", file_extensions: file_extensions) )", file_extensions: file_extensions)
end 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) posts.where("topics.views >= ?", match.to_i)
end 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) posts.where("topics.views <= ?", match.to_i)
end end

View File

@ -1399,8 +1399,8 @@ describe Search do
post = Fabricate(:post, raw: 'Topic', topic: topic) post = Fabricate(:post, raw: 'Topic', topic: topic)
post2 = Fabricate(:post, raw: 'Topic', topic: topic2) 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 min_views: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 max_views:150').posts.map(&:id)).to eq([post.id])
end end
it 'can search for terms with dots' do it 'can search for terms with dots' do