]> source.dussan.org Git - redmine.git/commitdiff
Refactor: move IssuesController#auto_complete to a new controller. #4382
authorEric Davis <edavis@littlestreamsoftware.com>
Tue, 17 Aug 2010 15:03:58 +0000 (15:03 +0000)
committerEric Davis <edavis@littlestreamsoftware.com>
Tue, 17 Aug 2010 15:03:58 +0000 (15:03 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3945 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/auto_completes_controller.rb [new file with mode: 0644]
app/controllers/issues_controller.rb
app/views/auto_completes/issues.html.erb [new file with mode: 0644]
app/views/issues/_form.rhtml
app/views/issues/auto_complete.html.erb [deleted file]
config/routes.rb
lib/redmine.rb
test/functional/auto_completes_controller_test.rb [new file with mode: 0644]
test/functional/issues_controller_test.rb
test/integration/routing_test.rb

diff --git a/app/controllers/auto_completes_controller.rb b/app/controllers/auto_completes_controller.rb
new file mode 100644 (file)
index 0000000..1438106
--- /dev/null
@@ -0,0 +1,25 @@
+class AutoCompletesController < ApplicationController
+  before_filter :find_project
+  
+  def issues
+    @issues = []
+    q = params[:q].to_s
+    if q.match(/^\d+$/)
+      @issues << @project.issues.visible.find_by_id(q.to_i)
+    end
+    unless q.blank?
+      @issues += @project.issues.visible.find(:all, :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE ?", "%#{q.downcase}%"], :limit => 10)
+    end
+    render :layout => false
+  end
+
+  private
+
+  def find_project
+    project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
+    @project = Project.find(project_id)
+  rescue ActiveRecord::RecordNotFound
+    render_404
+  end
+
+end
index eaa6dbc350ad4365ff4ab2bfc0c19709762fdd5b..4981f43ee407f7db845c02099fb3b73d56767d71 100644 (file)
@@ -21,7 +21,7 @@ class IssuesController < ApplicationController
   
   before_filter :find_issue, :only => [:show, :edit, :update]
   before_filter :find_issues, :only => [:bulk_edit, :move, :perform_move, :destroy]
-  before_filter :find_project, :only => [:new, :create, :update_form, :preview, :auto_complete]
+  before_filter :find_project, :only => [:new, :create, :update_form, :preview]
   before_filter :authorize, :except => [:index, :changes, :preview, :context_menu]
   before_filter :find_optional_project, :only => [:index, :changes]
   before_filter :check_for_default_issue_status, :only => [:new, :create]
@@ -316,18 +316,6 @@ class IssuesController < ApplicationController
     render :layout => false
   end
   
-  def auto_complete
-    @issues = []
-    q = params[:q].to_s
-    if q.match(/^\d+$/)
-      @issues << @project.issues.visible.find_by_id(q.to_i)
-    end
-    unless q.blank?
-      @issues += @project.issues.visible.find(:all, :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE ?", "%#{q.downcase}%"], :limit => 10)
-    end
-    render :layout => false
-  end
-  
 private
   def find_issue
     @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
diff --git a/app/views/auto_completes/issues.html.erb b/app/views/auto_completes/issues.html.erb
new file mode 100644 (file)
index 0000000..b130159
--- /dev/null
@@ -0,0 +1,9 @@
+<ul>
+<% if @issues.any? -%>
+  <% @issues.each do |issue| -%>
+    <%= content_tag 'li', h("#{issue.tracker} ##{issue.id}: #{issue.subject}"), :id => issue.id %>
+  <% end -%>
+<% else -%>
+  <%= content_tag("li", l(:label_none), :style => 'display:none') %>
+<% end -%>
+</ul>
index 1e3beaf851785b6ad94bd857488bb161f9bcd812..ccb7a55a3d6420b9e47a7ce37b65efdead183c81 100644 (file)
@@ -9,10 +9,7 @@
 <% unless (@issue.new_record? && @issue.parent_issue_id.nil?) || !User.current.allowed_to?(:manage_subtasks, @project) %>
 <p><%= f.text_field :parent_issue_id, :size => 10 %></p>
 <div id="parent_issue_candidates" class="autocomplete"></div>
-<%= javascript_tag "observeParentIssueField('#{url_for(:controller => :issues,
-                                                       :action => :auto_complete,
-                                                       :id => @issue,
-                                                       :project_id => @project) }')" %>
+<%= javascript_tag "observeParentIssueField('#{auto_complete_issues_path(:id => @issue, :project_id => @project) }')" %>
 <% end %>
 
 <p><%= f.text_area :description,
diff --git a/app/views/issues/auto_complete.html.erb b/app/views/issues/auto_complete.html.erb
deleted file mode 100644 (file)
index b130159..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-<ul>
-<% if @issues.any? -%>
-  <% @issues.each do |issue| -%>
-    <%= content_tag 'li', h("#{issue.tracker} ##{issue.id}: #{issue.subject}"), :id => issue.id %>
-  <% end -%>
-<% else -%>
-  <%= content_tag("li", l(:label_none), :style => 'display:none') %>
-<% end -%>
-</ul>
index 08cd1005565c46004bb3ffb063016d4464811139..910087ab9e381ebdb94a0972e4d68fe6dbdc9959 100644 (file)
@@ -104,7 +104,8 @@ ActionController::Routing::Routes.draw do |map|
   end
 
   map.resources :issue_moves, :only => [:new, :create], :path_prefix => '/issues', :as => 'move'
-
+  map.auto_complete_issues '/issues/auto_complete', :controller => 'auto_completes', :action => 'issues'
+  
   map.with_options :controller => 'issues' do |issues_routes|
     issues_routes.with_options :conditions => {:method => :get} do |issues_views|
       issues_views.connect 'issues', :action => 'index'
index 073e52d2dc9390f50a27b74c67fdd50ddbfde472..aa9fe75d1184d8bc495f99f0e1dfdf7449f0c05f 100644 (file)
@@ -58,7 +58,8 @@ Redmine::AccessControl.map do |map|
     map.permission :manage_categories, {:projects => :settings, :issue_categories => [:new, :edit, :destroy]}, :require => :member
     # Issues
     map.permission :view_issues, {:projects => :roadmap, 
-                                  :issues => [:index, :changes, :show, :context_menu, :auto_complete],
+                                  :issues => [:index, :changes, :show, :context_menu],
+                                  :auto_complete => [:issues],
                                   :versions => [:show, :status_by],
                                   :queries => :index,
                                   :reports => [:issue_report, :issue_report_details]}
diff --git a/test/functional/auto_completes_controller_test.rb b/test/functional/auto_completes_controller_test.rb
new file mode 100644 (file)
index 0000000..25e75fc
--- /dev/null
@@ -0,0 +1,20 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class AutoCompletesControllerTest < ActionController::TestCase
+  fixtures :all
+
+  def test_issues_should_not_be_case_sensitive
+    get :issues, :project_id => 'ecookbook', :q => 'ReCiPe'
+    assert_response :success
+    assert_not_nil assigns(:issues)
+    assert assigns(:issues).detect {|issue| issue.subject.match /recipe/}
+  end
+  
+  def test_issues_should_return_issue_with_given_id
+    get :issues, :project_id => 'subproject1', :q => '13'
+    assert_response :success
+    assert_not_nil assigns(:issues)
+    assert assigns(:issues).include?(Issue.find(13))
+  end
+  
+end
index 0c1172f602c9d60eb6a0a1eaa09340a29f91542d..948cf85984ec65b81c1d476478ff3e501d6ace1a 100644 (file)
@@ -1123,20 +1123,6 @@ class IssuesControllerTest < ActionController::TestCase
     assert_not_nil assigns(:notes)
   end
 
-  def test_auto_complete_should_not_be_case_sensitive
-    get :auto_complete, :project_id => 'ecookbook', :q => 'ReCiPe'
-    assert_response :success
-    assert_not_nil assigns(:issues)
-    assert assigns(:issues).detect {|issue| issue.subject.match /recipe/}
-  end
-  
-  def test_auto_complete_should_return_issue_with_given_id
-    get :auto_complete, :project_id => 'subproject1', :q => '13'
-    assert_response :success
-    assert_not_nil assigns(:issues)
-    assert assigns(:issues).include?(Issue.find(13))
-  end
-  
   def test_destroy_issue_with_no_time_entries
     assert_nil TimeEntry.find_by_issue_id(2)
     @request.session[:user_id] = 2
index b9eac552e528ec61bd7748b11541a8af80ace7e1..607df5b066a0aa65dc5320d0dea8e8669d33f3a5 100644 (file)
@@ -100,7 +100,7 @@ class RoutingTest < ActionController::IntegrationTest
     should_route :get, "/projects/project-name/issues/gantt", :controller => 'gantts', :action => 'show', :project_id => 'project-name'
     should_route :post, "/projects/project-name/issues/gantt", :controller => 'gantts', :action => 'show', :project_id => 'project-name'
 
-    should_route :get, "/issues/auto_complete", :controller => 'issues', :action => 'auto_complete'
+    should_route :get, "/issues/auto_complete", :controller => 'auto_completes', :action => 'issues'
   end
 
   context "issue categories" do