From 6496895096dad7d282fc4318ffb737eba2ee7ac3 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 15 Sep 2015 17:39:14 +1000 Subject: [PATCH] FEATURE: search for all posts by a group --- lib/search.rb | 9 +++++++++ spec/components/search_spec.rb | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/search.rb b/lib/search.rb index adb82a5383e..2dccf451456 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -233,6 +233,15 @@ class Search end end + advanced_filter(/group:(.+)/) do |posts,match| + group_id = Group.where('name ilike ? OR (id = ? AND id > 0)', match, match.to_i).pluck(:id).first + if group_id + posts.where("posts.user_id IN (select gu.user_id from group_users gu where gu.group_id = ?)", group_id) + else + posts.where("1 = 0") + end + end + advanced_filter(/user:(.+)/) do |posts,match| user_id = User.where('username_lower = ? OR id = ?', match.downcase, match.to_i).pluck(:id).first if user_id diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 335e538b0e0..219ca398ac9 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -391,6 +391,17 @@ describe Search do expect(Search.execute("user:#{_post.user.username}").posts.length).to eq(1) end + it 'supports group' do + topic = Fabricate(:topic, created_at: 3.months.ago) + post = Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic) + + group = Group.create!(name: "Like_a_Boss") + GroupUser.create!(user_id: post.user_id, group_id: group.id) + + expect(Search.execute('group:like_a_boss').posts.length).to eq(1) + expect(Search.execute('group:"like a brick"').posts.length).to eq(0) + end + it 'supports with_badge' do topic = Fabricate(:topic, created_at: 3.months.ago)