Skip to content

Commit

Permalink
Refactor: convert FilesController to a restful resource.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4085 e93f8b46-1217-0410-a6f0-8f06a7374b81
  • Loading branch information
edavis10 committed Sep 14, 2010
1 parent 41c0553 commit 1b90703
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/controllers/files_controller.rb
@@ -1,7 +1,7 @@
class FilesController < ApplicationController
menu_item :files

before_filter :find_project
before_filter :find_project_by_project_id
before_filter :authorize

helper :sort
Expand Down Expand Up @@ -31,6 +31,6 @@ def create
if !attachments.empty? && Setting.notified_events.include?('file_added')
Mailer.deliver_attachments_added(attachments[:files])
end
redirect_to :controller => 'files', :action => 'index', :id => @project
redirect_to project_files_path(@project)
end
end
2 changes: 1 addition & 1 deletion app/views/files/index.html.erb
@@ -1,5 +1,5 @@
<div class="contextual">
<%= link_to_if_authorized l(:label_attachment_new), {:controller => 'files', :action => 'new', :id => @project}, :class => 'icon icon-add' %>
<%= link_to_if_authorized l(:label_attachment_new), new_project_file_path(@project), :class => 'icon icon-add' %>
</div>

<h2><%=l(:label_attachment_plural)%></h2>
Expand Down
2 changes: 1 addition & 1 deletion app/views/files/new.html.erb
Expand Up @@ -2,7 +2,7 @@

<%= error_messages_for 'attachment' %>
<div class="box">
<% form_tag({ :action => 'create', :id => @project }, :multipart => true, :class => "tabular") do %>
<% form_tag(project_files_path(@project), :multipart => true, :class => "tabular") do %>
<% if @versions.any? %>
<p><label for="version_id"><%=l(:field_version)%></label>
Expand Down
7 changes: 1 addition & 6 deletions config/routes.rb
Expand Up @@ -181,6 +181,7 @@
:unarchive => :post
} do |project|
project.resource :project_enumerations, :as => 'enumerations', :only => [:update, :destroy]
project.resources :files, :only => [:index, :new, :create]
end

# Destroy uses a get request to prompt the user before the actual DELETE request
Expand All @@ -189,15 +190,9 @@
# TODO: port to be part of the resources route(s)
map.with_options :controller => 'projects' do |project_mapper|
project_mapper.with_options :conditions => {:method => :get} do |project_views|
project_views.connect 'projects/:id/files', :controller => 'files', :action => 'index'
project_views.connect 'projects/:id/files/new', :controller => 'files', :action => 'new'
project_views.connect 'projects/:id/settings/:tab', :controller => 'projects', :action => 'settings'
project_views.connect 'projects/:project_id/issues/:copy_from/copy', :controller => 'issues', :action => 'new'
end

project_mapper.with_options :conditions => {:method => :post} do |project_actions|
project_actions.connect 'projects/:id/files/new', :controller => 'files', :action => 'create'
end
end

map.with_options :controller => 'activities', :action => 'index', :conditions => {:method => :get} do |activity|
Expand Down
2 changes: 1 addition & 1 deletion lib/redmine.rb
Expand Up @@ -198,7 +198,7 @@
:if => Proc.new { |p| p.wiki && !p.wiki.new_record? }
menu.push :boards, { :controller => 'boards', :action => 'index', :id => nil }, :param => :project_id,
:if => Proc.new { |p| p.boards.any? }, :caption => :label_board_plural
menu.push :files, { :controller => 'files', :action => 'index' }, :caption => :label_file_plural
menu.push :files, { :controller => 'files', :action => 'index' }, :caption => :label_file_plural, :param => :project_id
menu.push :repository, { :controller => 'repositories', :action => 'show' },
:if => Proc.new { |p| p.repository && !p.repository.new_record? }
menu.push :settings, { :controller => 'projects', :action => 'settings' }, :last => true
Expand Down
6 changes: 3 additions & 3 deletions test/functional/files_controller_test.rb
Expand Up @@ -12,7 +12,7 @@ def setup
end

def test_index
get :index, :id => 1
get :index, :project_id => 1
assert_response :success
assert_template 'index'
assert_not_nil assigns(:containers)
Expand All @@ -33,7 +33,7 @@ def test_create_file
ActionMailer::Base.deliveries.clear

assert_difference 'Attachment.count' do
post :create, :id => 1, :version_id => '',
post :create, :project_id => 1, :version_id => '',
:attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
assert_response :redirect
end
Expand All @@ -54,7 +54,7 @@ def test_create_version_file
Setting.notified_events = ['file_added']

assert_difference 'Attachment.count' do
post :create, :id => 1, :version_id => '2',
post :create, :project_id => 1, :version_id => '2',
:attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
assert_response :redirect
end
Expand Down
6 changes: 3 additions & 3 deletions test/integration/routing_test.rb
Expand Up @@ -171,15 +171,15 @@ class RoutingTest < ActionController::IntegrationTest
should_route :get, "/projects/1.xml", :controller => 'projects', :action => 'show', :id => '1', :format => 'xml'
should_route :get, "/projects/4223/settings", :controller => 'projects', :action => 'settings', :id => '4223'
should_route :get, "/projects/4223/settings/members", :controller => 'projects', :action => 'settings', :id => '4223', :tab => 'members'
should_route :get, "/projects/33/files", :controller => 'files', :action => 'index', :id => '33'
should_route :get, "/projects/33/files/new", :controller => 'files', :action => 'new', :id => '33'
should_route :get, "/projects/33/files", :controller => 'files', :action => 'index', :project_id => '33'
should_route :get, "/projects/33/files/new", :controller => 'files', :action => 'new', :project_id => '33'
should_route :get, "/projects/33/roadmap", :controller => 'versions', :action => 'index', :project_id => '33'
should_route :get, "/projects/33/activity", :controller => 'activities', :action => 'index', :id => '33'
should_route :get, "/projects/33/activity.atom", :controller => 'activities', :action => 'index', :id => '33', :format => 'atom'

should_route :post, "/projects", :controller => 'projects', :action => 'create'
should_route :post, "/projects.xml", :controller => 'projects', :action => 'create', :format => 'xml'
should_route :post, "/projects/33/files/new", :controller => 'files', :action => 'create', :id => '33'
should_route :post, "/projects/33/files", :controller => 'files', :action => 'create', :project_id => '33'
should_route :post, "/projects/64/archive", :controller => 'projects', :action => 'archive', :id => '64'
should_route :post, "/projects/64/unarchive", :controller => 'projects', :action => 'unarchive', :id => '64'

Expand Down

0 comments on commit 1b90703

Please sign in to comment.