Merge pull request #4783 from bekicot/allow-to-order-search-results-by-the-topic-creation-date
Allow to order search results by the topic creation date
This commit is contained in:
commit
16087009a9
|
@ -11,6 +11,8 @@ const SortOrders = [
|
|||
{name: I18n.t('search.latest_post'), id: 1, term: 'order:latest'},
|
||||
{name: I18n.t('search.most_liked'), id: 2, term: 'order:likes'},
|
||||
{name: I18n.t('search.most_viewed'), id: 3, term: 'order:views'},
|
||||
{name: I18n.t('search.latest_topic'), id: 4, term: 'order:latest_topic'},
|
||||
|
||||
];
|
||||
|
||||
export default Ember.Controller.extend({
|
||||
|
@ -73,14 +75,7 @@ export default Ember.Controller.extend({
|
|||
|
||||
@computed('q')
|
||||
noSortQ(q) {
|
||||
if (q) {
|
||||
SortOrders.forEach((order) => {
|
||||
if (q.indexOf(order.term) > -1){
|
||||
q = q.replace(order.term, "");
|
||||
q = q.trim();
|
||||
}
|
||||
});
|
||||
}
|
||||
q = this.cleanTerm(q);
|
||||
return escapeExpression(q);
|
||||
},
|
||||
|
||||
|
@ -88,17 +83,23 @@ export default Ember.Controller.extend({
|
|||
|
||||
setSearchTerm(term) {
|
||||
this._searchOnSortChange = false;
|
||||
term = this.cleanTerm(term);
|
||||
this._searchOnSortChange = true;
|
||||
this.set('searchTerm', term);
|
||||
},
|
||||
|
||||
cleanTerm(term) {
|
||||
if (term) {
|
||||
SortOrders.forEach(order => {
|
||||
if (term.indexOf(order.term) > -1){
|
||||
let matches = term.match(new RegExp(`${order.term}\\b`));
|
||||
if (matches) {
|
||||
this.set('sortOrder', order.id);
|
||||
term = term.replace(order.term, "");
|
||||
term = term.replace(new RegExp(`${order.term}\\b`, 'g'), "");
|
||||
term = term.trim();
|
||||
}
|
||||
});
|
||||
}
|
||||
this._searchOnSortChange = true;
|
||||
this.set('searchTerm', term);
|
||||
return term;
|
||||
},
|
||||
|
||||
@observes('sortOrder')
|
||||
|
|
|
@ -1276,6 +1276,7 @@ en:
|
|||
sort_by: "Sort by"
|
||||
relevance: "Relevance"
|
||||
latest_post: "Latest Post"
|
||||
latest_topic: "Latest Topic"
|
||||
most_viewed: "Most Viewed"
|
||||
most_liked: "Most Liked"
|
||||
select_all: "Select All"
|
||||
|
|
|
@ -477,6 +477,9 @@ class Search
|
|||
if word == 'order:latest'
|
||||
@order = :latest
|
||||
nil
|
||||
elsif word == 'order:latest_topic'
|
||||
@order = :latest_topic
|
||||
nil
|
||||
elsif word =~ /topic:(\d+)/
|
||||
topic_id = $1.to_i
|
||||
if topic_id > 1
|
||||
|
@ -680,6 +683,12 @@ class Search
|
|||
else
|
||||
posts = posts.order("posts.created_at DESC")
|
||||
end
|
||||
elsif @order == :latest_topic
|
||||
if opts[:aggregate_search]
|
||||
posts = posts.order("MAX(topics.created_at) DESC")
|
||||
else
|
||||
posts = posts.order("topics.created_at DESC")
|
||||
end
|
||||
elsif @order == :views
|
||||
if opts[:aggregate_search]
|
||||
posts = posts.order("MAX(topics.views) DESC")
|
||||
|
|
|
@ -613,7 +613,28 @@ describe Search do
|
|||
|
||||
expect(Search.execute('sam').posts.map(&:id)).to eq([post1.id, post2.id])
|
||||
expect(Search.execute('sam order:latest').posts.map(&:id)).to eq([post2.id, post1.id])
|
||||
end
|
||||
|
||||
it 'can order by topic creation' do
|
||||
today = Date.today
|
||||
yesterday = 1.day.ago
|
||||
two_days_ago = 2.days.ago
|
||||
|
||||
old_topic = Fabricate(:topic,
|
||||
title: 'First Topic, testing the created_at sort',
|
||||
created_at: two_days_ago)
|
||||
latest_topic = Fabricate(:topic,
|
||||
title: 'Second Topic, testing the created_at sort',
|
||||
created_at: yesterday)
|
||||
|
||||
old_relevant_topic_post = Fabricate(:post, topic: old_topic, created_at: yesterday, raw: 'Relevant Topic')
|
||||
latest_irelevant_topic_post = Fabricate(:post, topic: latest_topic, created_at: today, raw: 'Not Relevant')
|
||||
|
||||
# Expecting the default results
|
||||
expect(Search.execute('Topic').posts.map(&:id)).to eq([old_relevant_topic_post.id, latest_irelevant_topic_post.id])
|
||||
|
||||
# Expecting the ordered by topic creation results
|
||||
expect(Search.execute('Topic order:latest_topic').posts.map(&:id)).to eq([latest_irelevant_topic_post.id, old_relevant_topic_post.id])
|
||||
end
|
||||
|
||||
it 'can tokenize dots' do
|
||||
|
|
Loading…
Reference in New Issue