From 497c595704d21100064d91f159ff3000c9c29766 Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 15 Sep 2015 15:06:34 +0200 Subject: [PATCH 1/2] considers tags within a debate search --- app/models/debate.rb | 5 ++++- spec/features/debates_spec.rb | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/models/debate.rb b/app/models/debate.rb index d141e4330..6fec7cb54 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -114,7 +114,10 @@ class Debate < ActiveRecord::Base end def self.search(terms) - terms.present? ? where("title ILIKE ? OR description ILIKE ?", "%#{terms}%", "%#{terms}%") : none + return none unless terms.present? + + ids = where("debates.title ILIKE ? OR debates.description ILIKE ?", "%#{terms}%", "%#{terms}%").pluck(:id) | tagged_with(terms).pluck(:id) + where(id: ids) end def conflictive? diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 98be639e1..4c72ae538 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -482,15 +482,17 @@ feature 'Debates' do debate2 = create(:debate, title: "Get Schwifty") debate3 = create(:debate) debate4 = create(:debate, description: "Schwifty in here") + debate5 = create(:debate, tag_list: 'schwifty') visit debates_path fill_in "search", with: "Schwifty" click_button "Search" within("#debates") do - expect(page).to have_css('.debate', count: 2) + expect(page).to have_css('.debate', count: 3) expect(page).to have_content(debate2.title) expect(page).to have_content(debate4.title) + expect(page).to have_content(debate5.title) expect(page).to_not have_content(debate1.title) expect(page).to_not have_content(debate3.title) end From 0418872fffa3a940f3cd5d9b569cd5a240cd4bf7 Mon Sep 17 00:00:00 2001 From: David Gil Date: Thu, 17 Sep 2015 14:48:43 +0200 Subject: [PATCH 2/2] tidies up a bit and adds wild: true for tag-based debate searching --- app/models/debate.rb | 6 ++++-- spec/features/debates_spec.rb | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/models/debate.rb b/app/models/debate.rb index 6fec7cb54..a7b84f0ce 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -116,8 +116,10 @@ class Debate < ActiveRecord::Base def self.search(terms) return none unless terms.present? - ids = where("debates.title ILIKE ? OR debates.description ILIKE ?", "%#{terms}%", "%#{terms}%").pluck(:id) | tagged_with(terms).pluck(:id) - where(id: ids) + debate_ids = where("debates.title ILIKE ? OR debates.description ILIKE ?", + "%#{terms}%", "%#{terms}%").pluck(:id) + tag_ids = tagged_with(terms, wild: true, any: true).pluck(:id) + where(id: [debate_ids, tag_ids].flatten.compact) end def conflictive? diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 4c72ae538..5b1069a66 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -483,16 +483,18 @@ feature 'Debates' do debate3 = create(:debate) debate4 = create(:debate, description: "Schwifty in here") debate5 = create(:debate, tag_list: 'schwifty') + debate6 = create(:debate, tag_list: ['awesome foreign schwifty', 'major']) visit debates_path fill_in "search", with: "Schwifty" click_button "Search" within("#debates") do - expect(page).to have_css('.debate', count: 3) + expect(page).to have_css('.debate', count: 4) expect(page).to have_content(debate2.title) expect(page).to have_content(debate4.title) expect(page).to have_content(debate5.title) + expect(page).to have_content(debate6.title) expect(page).to_not have_content(debate1.title) expect(page).to_not have_content(debate3.title) end