Commit 92f6de03703cb64fb5758c629a83522287af2d19

Authored by Adam Leonard
1 parent 09558634

If terms are removed show all results for current status

app/controllers/issues_controller.rb
... ... @@ -79,8 +79,17 @@ class IssuesController < ApplicationController
79 79 end
80 80  
81 81 def search
82   - @project = Project.find(params['project'])
83   - @issues = @project.issues.where("title LIKE ? OR content LIKE ?", "%#{params['terms']}%", "%#{params['terms']}%")
  82 + terms = params['terms']
  83 +
  84 + @project = Project.find(params['project'])
  85 + @issues = case params[:status].to_i
  86 + when 1 then @project.issues
  87 + when 2 then @project.issues.closed
  88 + when 3 then @project.issues.opened.assigned(current_user)
  89 + else @project.issues.opened
  90 + end
  91 +
  92 + @issues = @issues.where("title LIKE ? OR content LIKE ?", "%#{terms}%", "%#{terms}%") unless terms.blank?
84 93  
85 94 render :partial => 'issues'
86 95 end
... ...
app/views/issues/index.html.haml
... ... @@ -2,37 +2,44 @@
2 2 - if can? current_user, :write_issue, @project
3 3 .left
4 4 = form_tag search_project_issues_path(@project), :method => :get, :remote => true do
  5 + = hidden_field_tag :project_id, @project.id, { :id => 'project_id' }
5 6 = search_field_tag :issue_search, nil, { :placeholder => 'Search', :class => 'issue_search' }
6 7 = link_to 'New Issue', new_project_issue_path(@project), :remote => true, :class => "lbutton vm"
7 8  
8 9 .right
9 10 = form_tag project_issues_path(@project), :method => :get do
10 11 .span-2
11   - = radio_button_tag :f, 0, (params[:f] || "0") == "0", :onclick => "this.form.submit()", :id => "open_issues"
  12 + = radio_button_tag :f, 0, (params[:f] || "0") == "0", :onclick => "this.form.submit()", :id => "open_issues", :class => "status"
12 13 = label_tag "open_issues","Open"
13 14 .span-2
14   - = radio_button_tag :f, 2, params[:f] == "2", :onclick => "this.form.submit()", :id => "closed_issues"
  15 + = radio_button_tag :f, 2, params[:f] == "2", :onclick => "this.form.submit()", :id => "closed_issues", :class => "status"
15 16 = label_tag "closed_issues","Closed"
16 17 .span-2
17   - = radio_button_tag :f, 3, params[:f] == "3", :onclick => "this.form.submit()", :id => "my_issues"
  18 + = radio_button_tag :f, 3, params[:f] == "3", :onclick => "this.form.submit()", :id => "my_issues", :class => "status"
18 19 = label_tag "my_issues","To Me"
19   -
20 20 .span-2
21   - = radio_button_tag :f, 1, params[:f] == "1", :onclick => "this.form.submit()", :id => "all_issues"
  21 + = radio_button_tag :f, 1, params[:f] == "1", :onclick => "this.form.submit()", :id => "all_issues", :class => "status"
22 22 = label_tag "all_issues","All"
23 23  
24 24 #issues-table-holder= render "issues"
25 25 %br
26 26 :javascript
  27 + var href = $('.issue_search').parent().attr('action');
  28 + var last_terms = '';
  29 +
27 30 $('.issue_search').keyup(function() {
28   - var terms = $(this).val();
29   - var project_id = 1;
  31 + var terms = $(this).val();
  32 + var project_id = $('#project_id').val();
  33 + var status = $('.status:checked').val();
  34 + if (terms != last_terms) {
  35 + last_terms = terms;
30 36  
31   - if (terms.length >= 2) {
32   - $.get($(this).parent().attr('action'), { 'terms': terms, project: project_id }, function(response) {
33   - $('#issues-table').html(response);
34   - setSortable();
35   - });
  37 + if (terms.length >= 2 || terms.length == 0) {
  38 + $.get(href, { 'status': status, 'terms': terms, project: project_id }, function(response) {
  39 + $('#issues-table').html(response);
  40 + setSortable();
  41 + });
  42 + }
36 43 }
37 44 });
38 45  
... ...
spec/requests/issues_spec.rb
... ... @@ -149,21 +149,46 @@ describe "Issues" do
149 149 before do
150 150 ['foobar', 'foobar2', 'gitlab'].each do |title|
151 151 @issue = Factory :issue,
152   - :author => @user,
  152 + :author => @user,
153 153 :assignee => @user,
154   - :project => project,
155   - :title => title
  154 + :project => project,
  155 + :title => title
156 156 @issue.save
157 157 end
158 158 end
159 159  
160   - it "should search and return the correct results" do
  160 + it "should be able to search on different statuses" do
  161 + @issue = Issue.first
  162 + @issue.closed = true
  163 + @issue.save
  164 +
161 165 visit project_issues_path(project)
162   - fill_in "issue_search", :with => "foobar"
  166 + choose 'closed_issues'
  167 + fill_in 'issue_search', :with => 'foobar'
  168 +
  169 + page.should have_content 'foobar'
  170 + page.should_not have_content 'foobar2'
  171 + page.should_not have_content 'gitlab'
  172 + end
  173 +
  174 + it "should search for term and return the correct results" do
  175 + visit project_issues_path(project)
  176 + fill_in 'issue_search', :with => 'foobar'
  177 +
163 178 page.should have_content 'foobar'
164 179 page.should have_content 'foobar2'
165 180 page.should_not have_content 'gitlab'
166 181 end
167   - end
168 182  
  183 + it "should return all results if term has been cleared" do
  184 + visit project_issues_path(project)
  185 + fill_in "issue_search", :with => "foobar"
  186 + # Because fill_in, :with => "" triggers nothing we need to trigger a keyup event
  187 + page.execute_script("$('.issue_search').val('').keyup();");
  188 +
  189 + page.should have_content 'foobar'
  190 + page.should have_content 'foobar2'
  191 + page.should have_content 'gitlab'
  192 + end
  193 + end
169 194 end
... ...