OSDN Git Service

Refactor: convert FilesController to a restful resource.
authorEric Davis <edavis@littlestreamsoftware.com>
Tue, 14 Sep 2010 16:24:07 +0000 (16:24 +0000)
committerEric Davis <edavis@littlestreamsoftware.com>
Tue, 14 Sep 2010 16:24:07 +0000 (16:24 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4085 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/files_controller.rb
app/views/files/index.html.erb
app/views/files/new.html.erb
config/routes.rb
lib/redmine.rb
test/functional/files_controller_test.rb
test/integration/routing_test.rb

index 0a4903d..72a4a24 100644 (file)
@@ -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
@@ -31,6 +31,6 @@ class FilesController < ApplicationController
     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
index 66f5d12..2071040 100644 (file)
@@ -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>
index 870a315..d9d1b6e 100644 (file)
@@ -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>
index 152b90f..c85f23e 100644 (file)
@@ -181,6 +181,7 @@ ActionController::Routing::Routes.draw do |map|
     :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
@@ -189,15 +190,9 @@ ActionController::Routing::Routes.draw do |map|
   # 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|
index 9d59390..f8f5642 100644 (file)
@@ -198,7 +198,7 @@ Redmine::MenuManager.map :project_menu do |menu|
               :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
index 2f6f009..6035c4b 100644 (file)
@@ -12,7 +12,7 @@ class FilesControllerTest < ActionController::TestCase
   end
 
   def test_index
-    get :index, :id => 1
+    get :index, :project_id => 1
     assert_response :success
     assert_template 'index'
     assert_not_nil assigns(:containers)
@@ -33,7 +33,7 @@ class FilesControllerTest < ActionController::TestCase
     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
@@ -54,7 +54,7 @@ class FilesControllerTest < ActionController::TestCase
     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
index 1867a60..38c1b2a 100644 (file)
@@ -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'