diff --git a/app/assets/javascripts/discourse/controllers/search.js.es6 b/app/assets/javascripts/discourse/controllers/search.js.es6
index 317106eaaf5..28a316c6583 100644
--- a/app/assets/javascripts/discourse/controllers/search.js.es6
+++ b/app/assets/javascripts/discourse/controllers/search.js.es6
@@ -9,9 +9,32 @@
export default Em.ArrayController.extend(Discourse.Presence, {
contextChanged: function(){
- this.setProperties({ term: "", content: [], resultCount: 0, urls: [] });
+ if(this.get('searchContextEnabled')){
+ this._dontSearch = true;
+ this.set('searchContextEnabled', false);
+ this._dontSearch = false;
+ }
}.observes("searchContext"),
+ searchContextDescription: function(){
+ var ctx = this.get('searchContext');
+ if (ctx) {
+ switch(Em.get(ctx, 'type')) {
+ case 'topic':
+ return I18n.t('search.context.topic');
+ case 'user':
+ return I18n.t('search.context.user', {username: Em.get(ctx, 'user.username')});
+ case 'category':
+ return I18n.t('search.context.category', {category: Em.get(ctx, 'category.name')});
+ }
+ }
+ }.property('searchContext'),
+
+ searchContextEnabledChanged: function(){
+ if(this._dontSearch){ return; }
+ this.newSearchNeeded();
+ }.observes('searchContextEnabled'),
+
// If we need to perform another search
newSearchNeeded: function() {
this.set('noResults', false);
@@ -29,9 +52,14 @@ export default Em.ArrayController.extend(Discourse.Presence, {
var self = this;
this.setProperties({ resultCount: 0, urls: [] });
+ var context;
+ if(this.get('searchContextEnabled')){
+ context = this.get('searchContext');
+ }
+
return Discourse.Search.forTerm(term, {
typeFilter: typeFilter,
- searchContext: this.get('searchContext')
+ searchContext: context
}).then(function(results) {
var urls = [];
if (results) {
diff --git a/app/assets/javascripts/discourse/templates/search.js.handlebars b/app/assets/javascripts/discourse/templates/search.js.handlebars
index 68f7a971b97..2b1fbb1e86a 100644
--- a/app/assets/javascripts/discourse/templates/search.js.handlebars
+++ b/app/assets/javascripts/discourse/templates/search.js.handlebars
@@ -1,5 +1,14 @@
-{{view 'search-text-field' value=term searchContext=searchContext id="search-term"}}
-{{#unless loading}}
+{{view 'search-text-field' value=term searchContextEnabled=searchContextEnabled searchContext=searchContext id="search-term"}}
+{{#if searchContext}}
+
+
+
+{{/if}}
+{{#if loading}}
+
+{{else}}
{{#unless noResults}}
{{#each resultType in content}}
@@ -21,6 +30,4 @@
{{i18n search.no_results}}
{{/unless}}
-{{else}}
-
-{{/unless}}
+{{/if}}
diff --git a/app/assets/javascripts/discourse/views/search-text-field.js.es6 b/app/assets/javascripts/discourse/views/search-text-field.js.es6
index c7aeb13b079..1195af24ba3 100644
--- a/app/assets/javascripts/discourse/views/search-text-field.js.es6
+++ b/app/assets/javascripts/discourse/views/search-text-field.js.es6
@@ -18,16 +18,10 @@ export default TextField.extend({
**/
placeholder: function() {
- var ctx = this.get('searchContext');
- if (ctx) {
- switch(Em.get(ctx, 'type')) {
- case 'user':
- return I18n.t('search.prefer.user', {username: Em.get(ctx, 'user.username')});
- case 'category':
- return I18n.t('search.prefer.category', {category: Em.get(ctx, 'category.name')});
- }
+ if(this.get('searchContextEnabled')){
+ return "";
}
return I18n.t('search.title');
- }.property('searchContext')
+ }.property('searchContextEnabled')
});
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 177e33235f9..e76677ad775 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -649,9 +649,10 @@ en:
no_results: "No results found."
searching: "Searching ..."
- prefer:
- user: "search will prefer results by @{{username}}"
- category: "search will prefer results in {{category}}"
+ context:
+ user: "Search posts by @{{username}}"
+ category: "Search the \"{{category}}\" category"
+ topic: "Search this topic"
site_map: "go to another topic list or category"
go_back: 'go back'
diff --git a/lib/search.rb b/lib/search.rb
index 55c7c1ab936..afb1a54b4a7 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -76,8 +76,10 @@ class Search
send("#{@results.type_filter}_search")
else
@limit = Search.per_facet + 1
- user_search
- category_search
+ unless @search_context
+ user_search
+ category_search
+ end
topic_search
end
@@ -161,14 +163,12 @@ class Search
if @search_context.present?
if @search_context.is_a?(User)
- # If the context is a user, prioritize that user's posts
- posts = posts.order("CASE WHEN posts.user_id = #{@search_context.id} THEN 0 ELSE 1 END")
+ posts = posts.where("posts.user_id = #{@search_context.id}")
elsif @search_context.is_a?(Category)
- # If the context is a category, restrict posts to that category
- posts = posts.order("CASE WHEN topics.category_id = #{@search_context.id} THEN 0 ELSE 1 END")
+ posts = posts.where("topics.category_id = #{@search_context.id}")
elsif @search_context.is_a?(Topic)
- posts = posts.order("CASE WHEN topics.id = #{@search_context.id} THEN 0 ELSE 1 END,
- CASE WHEN topics.id = #{@search_context.id} THEN posts.post_number ELSE 999999 END")
+ posts = posts.where("topics.id = #{@search_context.id}")
+ .order("posts.post_number")
end
end
diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb
index ff5eb62561b..cf25bd0cecf 100644
--- a/spec/components/search_spec.rb
+++ b/spec/components/search_spec.rb
@@ -162,8 +162,7 @@ describe Search do
post1.topic_id,
"_#{post2.id}",
"_#{post3.id}",
- "_#{post4.id}",
- topic2.id]
+ "_#{post4.id}"]
end
end
@@ -312,8 +311,6 @@ describe Search do
# should find topic created by searched user first
Then { first_of_type(search_user, 'topic')[:id].should == post.topic_id }
- # results should also include topic by coding_horror
- And { result_ids_for_type(search_user, 'topic').should include coding_horror_post.topic_id }
end
context 'category as a search context' do
@@ -326,9 +323,6 @@ describe Search do
When(:search_cat) { Search.new('hello', search_context: category).execute }
# should find topic in searched category first
Then { first_of_type(search_cat, 'topic')[:id].should == topic.id }
- # results should also include topic without category
- And { result_ids_for_type(search_cat, 'topic').should include topic_no_cat.id }
-
end
end
diff --git a/test/javascripts/controllers/search_controller_test.js b/test/javascripts/controllers/search_controller_test.js
index 2d843d9b999..2dfbc5db8f1 100644
--- a/test/javascripts/controllers/search_controller_test.js
+++ b/test/javascripts/controllers/search_controller_test.js
@@ -235,6 +235,7 @@ test("selecting a highlighted item", function() {
test("search query / the flow of the search", function() {
Ember.run(function() {
controller.set("searchContext", "context");
+ controller.set("searchContextEnabled", true);
controller.set("term", "ab");
});
ok(Discourse.Search.forTerm.calledWithExactly(
diff --git a/test/javascripts/views/search_text_field_test.js b/test/javascripts/views/search_text_field_test.js
deleted file mode 100644
index 05bdd4be52e..00000000000
--- a/test/javascripts/views/search_text_field_test.js
+++ /dev/null
@@ -1,45 +0,0 @@
-var view;
-
-var placeholderUsesKeyAndContext = function(key, context) {
- var placeholder = view.get("placeholder");
- equal(placeholder.key, key, "placeholder contains correct message");
- deepEqual(placeholder.context, context, "correct parameters are passed to the message");
-};
-
-module("view:search-text-field", {
- setup: function() {
- sinon.stub(I18n, "t", function(key, context) {
- return {key: key, context: context};
- });
-
- view = viewClassFor('search-text-field').create();
- },
-
- teardown: function() {
- I18n.t.restore();
- }
-});
-
-test("formats placeholder correctly when no searchContext is provided", function() {
- placeholderUsesKeyAndContext("search.title", undefined);
-});
-
-test("formats placeholder correctly when user searchContext is provided", function() {
- view.set("searchContext", {
- type: "user",
- user: {
- username: "userName"
- }
- });
- placeholderUsesKeyAndContext("search.prefer.user", {username: "userName"});
-});
-
-test("formats placeholder correctly when category searchContext is provided", function() {
- view.set("searchContext", {
- type: "category",
- category: {
- name: "categoryName"
- }
- });
- placeholderUsesKeyAndContext("search.prefer.category", {category: "categoryName"});
-});