Trackers controller refactoring.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2526 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2009-02-26 09:21:41 +00:00
parent 46f52d306d
commit 589320337d
5 changed files with 78 additions and 27 deletions

View File

@ -1,5 +1,5 @@
# redMine - project management software
# Copyright (C) 2006 Jean-Philippe Lang
# Redmine - project management software
# Copyright (C) 2006-2009 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@ -22,9 +22,8 @@ class TrackersController < ApplicationController
list
render :action => 'list' unless request.xhr?
end
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :move ], :redirect_to => { :action => :list }
verify :method => :post, :only => :destroy, :redirect_to => { :action => :list }
def list
@tracker_pages, @trackers = paginate :trackers, :per_page => 10, :order => 'position'
@ -55,21 +54,6 @@ class TrackersController < ApplicationController
end
@projects = Project.find(:all)
end
def move
@tracker = Tracker.find(params[:id])
case params[:position]
when 'highest'
@tracker.move_to_top
when 'higher'
@tracker.move_higher
when 'lower'
@tracker.move_lower
when 'lowest'
@tracker.move_to_bottom
end if params[:position]
redirect_to :action => 'list'
end
def destroy
@tracker = Tracker.find(params[:id])

View File

@ -265,6 +265,13 @@ module ApplicationHelper
end
links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
end
def reorder_links(name, url)
link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)), url.merge({"#{name}[move_to]" => 'highest'}), :method => :post, :title => l(:label_sort_highest)) +
link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)), url.merge({"#{name}[move_to]" => 'higher'}), :method => :post, :title => l(:label_sort_higher)) +
link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)), url.merge({"#{name}[move_to]" => 'lower'}), :method => :post, :title => l(:label_sort_lower)) +
link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), url.merge({"#{name}[move_to]" => 'lowest'}), :method => :post, :title => l(:label_sort_lowest))
end
def breadcrumb(*args)
elements = args.flatten

View File

@ -16,12 +16,7 @@
<tr class="<%= cycle("odd", "even") %>">
<td><%= link_to tracker.name, :action => 'edit', :id => tracker %></td>
<td align="center"><% unless tracker.workflows.count > 0 %><span class="icon icon-warning"><%= l(:text_tracker_no_workflow) %> (<%= link_to l(:button_edit), {:controller => 'workflows', :action => 'edit', :tracker_id => tracker} %>)</span><% end %></td>
<td align="center" style="width:15%;">
<%= link_to image_tag('2uparrow.png', :alt => l(:label_sort_highest)), {:action => 'move', :id => tracker, :position => 'highest'}, :method => :post, :title => l(:label_sort_highest) %>
<%= link_to image_tag('1uparrow.png', :alt => l(:label_sort_higher)), {:action => 'move', :id => tracker, :position => 'higher'}, :method => :post, :title => l(:label_sort_higher) %> -
<%= link_to image_tag('1downarrow.png', :alt => l(:label_sort_lower)), {:action => 'move', :id => tracker, :position => 'lower'}, :method => :post, :title => l(:label_sort_lower) %>
<%= link_to image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), {:action => 'move', :id => tracker, :position => 'lowest'}, :method => :post, :title => l(:label_sort_lowest) %>
</td>
<td align="center" style="width:15%;"><%= reorder_links('tracker', {:action => 'edit', :id => tracker}) %></td>
<td align="center" style="width:10%;">
<%= button_to l(:button_delete), { :action => 'destroy', :id => tracker }, :confirm => l(:text_are_you_sure), :class => "button-small" %>
</td>

View File

@ -22,7 +22,7 @@ require 'trackers_controller'
class TrackersController; def rescue_action(e) raise e end; end
class TrackersControllerTest < Test::Unit::TestCase
fixtures :trackers, :projects, :projects_trackers, :users
fixtures :trackers, :projects, :projects_trackers, :users, :issues
def setup
@controller = TrackersController.new
@ -32,6 +32,34 @@ class TrackersControllerTest < Test::Unit::TestCase
@request.session[:user_id] = 1 # admin
end
def test_index
get :index
assert_response :success
assert_template 'list'
end
def test_get_new
get :new
assert_response :success
assert_template 'new'
end
def test_post_new
post :new, :tracker => { :name => 'New tracker', :project_ids => ['1', '', ''] }
assert_redirected_to '/trackers/list'
tracker = Tracker.find_by_name('New tracker')
assert_equal [1], tracker.project_ids.sort
assert_equal 0, tracker.workflows.count
end
def test_post_new_with_workflow_copy
post :new, :tracker => { :name => 'New tracker' }, :copy_workflow_from => 1
assert_redirected_to '/trackers/list'
tracker = Tracker.find_by_name('New tracker')
assert_equal 0, tracker.projects.count
assert_equal Tracker.find(1).workflows.count, tracker.workflows.count
end
def test_get_edit
Tracker.find(1).project_ids = [1, 3]
@ -65,4 +93,27 @@ class TrackersControllerTest < Test::Unit::TestCase
assert_redirected_to '/trackers/list'
assert Tracker.find(1).project_ids.empty?
end
def test_move_lower
tracker = Tracker.find_by_position(1)
post :edit, :id => 1, :tracker => { :move_to => 'lower' }
assert_equal 2, tracker.reload.position
end
def test_destroy
tracker = Tracker.create!(:name => 'Destroyable')
assert_difference 'Tracker.count', -1 do
post :destroy, :id => tracker.id
end
assert_redirected_to '/trackers/list'
assert_nil flash[:error]
end
def test_destroy_tracker_in_use
assert_no_difference 'Tracker.count' do
post :destroy, :id => 1
end
assert_redirected_to '/trackers/list'
assert_not_nil flash[:error]
end
end

View File

@ -118,6 +118,20 @@ module ActiveRecord
assume_top_position
end
end
# Move to the given position
def move_to=(pos)
case pos.to_s
when 'highest'
move_to_top
when 'higher'
move_higher
when 'lower'
move_lower
when 'lowest'
move_to_bottom
end
end
# Removes the item from the list.
def remove_from_list