Skip to content

Commit 105a12b

Browse files
authored
Only display issues from active projects in searchbar (#203)
* Add filter to only display Tickets from active projects * Check status when searching for project name * Refactor to use on_active_project scope * Add test for IssueSearcher
1 parent 608a946 commit 105a12b

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

app/controllers/completion_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ def search_term
1717
end
1818

1919
def scoped_issues
20-
scope = Issue.cross_project_scope(@project, params[:scope]).visible
21-
scope.includes(:project, :tracker)
20+
Issue
21+
.cross_project_scope(@project, params[:scope])
22+
.visible
23+
.includes(:project, :tracker)
2224
end
2325

2426
def format_issues_json(issues)

app/services/issue_searcher.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class IssueSearcher
66
TICKET_OPEN_STATUS = true
77

88
def call(search_term, issues)
9+
issues = issues.on_active_project
10+
911
issues = if search_term.present?
1012
search_by_term(search_term, issues)
1113
else
@@ -32,9 +34,10 @@ def find_by_id(search_term, issues)
3234

3335
def hits_by_project(search_term)
3436
project_arel = Project.arel_table
35-
Issue.left_joins(:project, :time_entries)
37+
Issue.left_joins(:time_entries)
3638
.select('issues.*, COUNT(time_entries.id) as time_entries_count')
3739
.group('issues.id')
40+
.on_active_project
3841
.where(project_arel[:name].lower.matches("%#{search_term.downcase}%"))
3942
.open(TICKET_OPEN_STATUS)
4043
.order('time_entries_count DESC')

test/unit/issue_searcher_test.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ class IssueSearcherTest < ActiveSupport::TestCase
1414
User.current = User.find(2)
1515
@service = IssueSearcher.new
1616
renuo_project = create(:project, name: 'Renuo Project', identifier: 'renuo_project')
17-
@projects = [renuo_project, Project.first, Project.second, Project.third]
17+
closed_project = create(:project, name: 'Closed Project', identifier: 'closed_project',
18+
status: Project::STATUS_CLOSED)
19+
@projects = [renuo_project, Project.first, Project.second, Project.third, closed_project]
1820
@issues = [
1921
create(:issue, id: 100, subject: 'Very special Renuo issue', project: @projects[0]),
2022
create(:issue, id: 101, subject: 'Second special Renuo issue', project: @projects[1]),
21-
create(:issue, :closed, id: 102, subject: 'Closed issue', project: @projects[2])
23+
create(:issue, :closed, id: 102, subject: 'Closed issue', project: @projects[2]),
24+
create(:issue, id: 200, subject: 'My project is not open', project: closed_project)
2225
]
2326
end
2427

@@ -42,6 +45,14 @@ class IssueSearcherTest < ActiveSupport::TestCase
4245
assert_equal [], @service.call(search_term, Issue.all)
4346
end
4447

48+
test 'call - filters issues belonging to a closed project' do
49+
issue_search_term = 'My project is not open'
50+
project_search_term = 'closed project'
51+
52+
assert_not_includes @service.call(issue_search_term, Issue.all), @issues[3]
53+
assert_not_includes @service.call(project_search_term, Issue.all), @issues[3]
54+
end
55+
4556
test 'call - hits by id are first' do
4657
second_issue = FactoryBot.create(:issue, id: 103, subject: '100th issue', project: Project.first)
4758

0 commit comments

Comments
 (0)