Commit 0e33bf6eb0883259af4b258532b948e5798e7d1d

Authored by Dmitriy Zaporozhets
1 parent 3a2d7a66

Refactored IssuesController

app/assets/javascripts/issues.js
@@ -35,3 +35,29 @@ function backToIssues(){ @@ -35,3 +35,29 @@ function backToIssues(){
35 }); 35 });
36 }); 36 });
37 } 37 }
  38 +
  39 +function initIssuesSearch() {
  40 + var href = $('.issue_search').parent().attr('action');
  41 + var last_terms = '';
  42 +
  43 + $('.issue_search').keyup(function() {
  44 + var terms = $(this).val();
  45 + var milestone_id = $('#milestone_id').val();
  46 + var status = $('#status').val();
  47 +
  48 + if (terms != last_terms) {
  49 + last_terms = terms;
  50 +
  51 + if (terms.length >= 2 || terms.length == 0) {
  52 + $.get(href, { 'f': status, 'terms': terms, 'milestone_id': milestone_id }, function(response) {
  53 + $('#issues-table').html(response);
  54 + setSortable();
  55 + });
  56 + }
  57 + }
  58 + });
  59 +
  60 + $('.delete-issue').live('ajax:success', function() {
  61 + $(this).closest('tr').fadeOut(); updatePage();
  62 + });
  63 +}
app/controllers/issues_controller.rb
@@ -23,16 +23,9 @@ class IssuesController < ApplicationController @@ -23,16 +23,9 @@ class IssuesController < ApplicationController
23 respond_to :js, :html 23 respond_to :js, :html
24 24
25 def index 25 def index
26 - @issues = case params[:f].to_i  
27 - when 1 then @project.issues  
28 - when 2 then @project.issues.closed  
29 - when 3 then @project.issues.opened.assigned(current_user)  
30 - else @project.issues.opened  
31 - end 26 + @issues = issues_filtered
32 27
33 - @issues = @issues.where(:milestone_id => params[:milestone_id]) if params[:milestone_id].present?  
34 @issues = @issues.page(params[:page]).per(20) 28 @issues = @issues.page(params[:page]).per(20)
35 - @issues = @issues.includes(:author, :project).order("critical, updated_at")  
36 29
37 respond_to do |format| 30 respond_to do |format|
38 format.html # index.html.erb 31 format.html # index.html.erb
@@ -111,15 +104,9 @@ class IssuesController < ApplicationController @@ -111,15 +104,9 @@ class IssuesController < ApplicationController
111 def search 104 def search
112 terms = params['terms'] 105 terms = params['terms']
113 106
114 - @project = Project.find(params['project'])  
115 - @issues = case params[:status].to_i  
116 - when 1 then @project.issues  
117 - when 2 then @project.issues.closed  
118 - when 3 then @project.issues.opened.assigned(current_user)  
119 - else @project.issues.opened  
120 - end.page(params[:page]).per(100)  
121 - 107 + @issues = issues_filtered
122 @issues = @issues.where("title LIKE ?", "%#{terms}%") unless terms.blank? 108 @issues = @issues.where("title LIKE ?", "%#{terms}%") unless terms.blank?
  109 + @issues = @issues.page(params[:page]).per(100)
123 110
124 render :partial => 'issues' 111 render :partial => 'issues'
125 end 112 end
@@ -141,4 +128,17 @@ class IssuesController < ApplicationController @@ -141,4 +128,17 @@ class IssuesController < ApplicationController
141 def module_enabled 128 def module_enabled
142 return render_404 unless @project.issues_enabled 129 return render_404 unless @project.issues_enabled
143 end 130 end
  131 +
  132 + def issues_filtered
  133 + @issues = case params[:f].to_i
  134 + when 1 then @project.issues
  135 + when 2 then @project.issues.closed
  136 + when 3 then @project.issues.opened.assigned(current_user)
  137 + else @project.issues.opened
  138 + end
  139 +
  140 + @issues = @issues.where(:milestone_id => params[:milestone_id]) if params[:milestone_id].present?
  141 + @issues = @issues.includes(:author, :project).order("critical, updated_at")
  142 + @issues
  143 + end
144 end 144 end
app/views/issues/index.html.haml
@@ -41,27 +41,14 @@ @@ -41,27 +41,14 @@
41 = render "issues" 41 = render "issues"
42 42
43 :javascript 43 :javascript
44 - var href = $('.issue_search').parent().attr('action');  
45 - var last_terms = '';  
46 -  
47 - $('.issue_search').keyup(function() {  
48 - var terms = $(this).val();  
49 - var project_id = $('#project_id').val();  
50 - var status = $('#status').val();  
51 - if (terms != last_terms) {  
52 - last_terms = terms;  
53 -  
54 - if (terms.length >= 2 || terms.length == 0) {  
55 - $.get(href, { 'status': status, 'terms': terms, project: project_id }, function(response) {  
56 - $('#issues-table').html(response);  
57 - setSortable();  
58 - });  
59 - }  
60 - }  
61 - });  
62 -  
63 - $('.delete-issue').live('ajax:success', function() {  
64 - $(this).closest('tr').fadeOut(); updatePage();}); 44 + $(function(){
  45 + initIssuesSearch();
  46 + setSortable();
  47 + $("#milestone_id").chosen();
  48 + $("#milestone_id").live("change", function(){
  49 + $(this).closest("form").submit();
  50 + });
  51 + })
65 52
66 function setSortable(){ 53 function setSortable(){
67 $('#issues-table').sortable({ 54 $('#issues-table').sortable({
@@ -83,11 +70,3 @@ @@ -83,11 +70,3 @@
83 } 70 }
84 }); 71 });
85 } 72 }
86 -  
87 - $(function(){  
88 - setSortable();  
89 - $("#milestone_id").chosen();  
90 - $("#milestone_id").live("change", function(){  
91 - $(this).closest("form").submit();  
92 - });  
93 - });