From 4a5a2d869e5bfd84cf015e3134d02a9c5ecd6f4f Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 10 Jul 2015 16:31:28 +1000 Subject: [PATCH] FEATURE: search by user id or category id FIX: more search results work if you open in new tab FIX: carry context to full page search --- .../discourse/controllers/search.js.es6 | 41 +++++++++++++++---- .../discourse/lib/search-for-term.js.es6 | 11 ++++- .../discourse/templates/search.hbs | 3 ++ .../javascripts/discourse/views/search.js.es6 | 2 +- lib/search.rb | 4 +- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/search.js.es6 b/app/assets/javascripts/discourse/controllers/search.js.es6 index 2bf81830419..9c44f0b2a9b 100644 --- a/app/assets/javascripts/discourse/controllers/search.js.es6 +++ b/app/assets/javascripts/discourse/controllers/search.js.es6 @@ -23,6 +23,30 @@ export default Em.Controller.extend(Presence, { } }.observes('searchContext'), + fullSearchUrlRelative: function(){ + + if (this.get('searchContextEnabled') && this.get('searchContext.type') === 'topic') { + return null; + } + + var url = '/search?q=' + encodeURIComponent(this.get('term')); + var searchContext = this.get('searchContext'); + + if (this.get('searchContextEnabled') && searchContext) { + url += encodeURIComponent(" " + searchContext.type + ":" + searchContext.id); + } + + return url; + + }.property('searchContext','term','searchContextEnabled'), + + fullSearchUrl: function(){ + var url = this.get('fullSearchUrlRelative'); + if (url) { + return Discourse.getURL(url); + } + }.property('fullSearchUrlRelative'), + searchContextDescription: function(){ var ctx = this.get('searchContext'); if (ctx) { @@ -68,7 +92,8 @@ export default Em.Controller.extend(Presence, { searchForTerm(term, { typeFilter: typeFilter, - searchContext: context + searchContext: context, + fullSearchUrl: this.get('fullSearchUrl') }).then(function(results) { self.setProperties({ noResults: !results, content: results }); self.set('loading', false); @@ -87,15 +112,15 @@ export default Em.Controller.extend(Presence, { }.observes('term'), actions: { - moreOfType: function(type) { - if (type === 'topic' && (!this.get('searchContextEnabled') || this.get('searchContext.type') !== 'topic')) { - var term = this.get('term'); - // TODO in topic and in category special handling - Discourse.URL.routeTo("/search?q=" + encodeURIComponent(term)); - } else { - this.set('typeFilter', type); + fullSearch: function() { + var url = this.get('fullSearchUrlRelative'); + if (url) { + Discourse.URL.routeTo(url); } }, + moreOfType: function(type) { + this.set('typeFilter', type); + }, cancelType: function() { this.cancelTypeFilter(); diff --git a/app/assets/javascripts/discourse/lib/search-for-term.js.es6 b/app/assets/javascripts/discourse/lib/search-for-term.js.es6 index 478b1bc6e99..a63eacad71b 100644 --- a/app/assets/javascripts/discourse/lib/search-for-term.js.es6 +++ b/app/assets/javascripts/discourse/lib/search-for-term.js.es6 @@ -51,12 +51,19 @@ function searchForTerm(term, opts) { [['topic','posts'],['user','users'],['category','categories']].forEach(function(pair){ const type = pair[0], name = pair[1]; if (results[name].length > 0) { - results.resultTypes.push({ + var result = { results: results[name], componentName: "search-result-" + ((opts.searchContext && opts.searchContext.type === 'topic' && type === 'topic') ? 'post' : type), type, more: r['more_' + name] - }); + }; + + if (result.more && name === "posts" && opts.fullSearchUrl) { + result.more = false; + result.moreUrl = opts.fullSearchUrl; + } + + results.resultTypes.push(result); } }); diff --git a/app/assets/javascripts/discourse/templates/search.hbs b/app/assets/javascripts/discourse/templates/search.hbs index a3c58a3addd..fb062f02596 100644 --- a/app/assets/javascripts/discourse/templates/search.hbs +++ b/app/assets/javascripts/discourse/templates/search.hbs @@ -24,6 +24,9 @@ {{component resultType.componentName results=resultType.results term=term}}
+ {{#if resultType.moreUrl}} + {{i18n "show_more"}} {{fa-icon "chevron-down"}} + {{/if}} {{#if resultType.more}} {{i18n "show_more"}} {{fa-icon "chevron-down"}} {{/if}} diff --git a/app/assets/javascripts/discourse/views/search.js.es6 b/app/assets/javascripts/discourse/views/search.js.es6 index 88ae67861ca..a240a0dec9d 100644 --- a/app/assets/javascripts/discourse/views/search.js.es6 +++ b/app/assets/javascripts/discourse/views/search.js.es6 @@ -6,7 +6,7 @@ export default Discourse.View.extend({ keyDown: function(e){ var term = this.get('controller.term'); if (e.which === 13 && term && term.length > 2) { - this.get('controller').send('moreOfType', 'topic'); + this.get('controller').send('fullSearch'); } } }); diff --git a/lib/search.rb b/lib/search.rb index 1395cb7fd34..5eb8a722b3d 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -214,12 +214,12 @@ class Search end advanced_filter(/category:(.+)/) do |posts,match| - category_id = Category.find_by('name ilike ?', match).try(:id) + category_id = Category.find_by('name ilike ? OR id = ?', match, match.to_i).try(:id) posts.where("topics.category_id = ?", category_id) end advanced_filter(/user:(.+)/) do |posts,match| - user_id = User.find_by('username_lower = ?', match.downcase).try(:id) + user_id = User.find_by('username_lower = ? OR id = ?', match.downcase, match.to_i).try(:id) posts.where("posts.user_id = #{user_id}") end