Refactor: move IssuesController#auto_complete to a new controller. #4382

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3945 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Eric Davis 2010-08-17 15:03:58 +00:00
parent e63acb70ca
commit 3eff27344b
9 changed files with 52 additions and 34 deletions

View File

@ -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

View 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])

View 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,

View 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'

View 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]}

View File

@ -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

View 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

View 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