From c3c2a4afe0083bf704110c3d13ee7b80262293ee Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Wed, 30 Nov 2011 19:51:16 +0000 Subject: [PATCH] Resourcified documents. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8010 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/documents_controller.rb | 22 ++++++++++++++------ app/views/documents/_document.html.erb | 2 +- app/views/documents/_form.html.erb | 22 ++++++++++---------- app/views/documents/edit.html.erb | 6 +++--- app/views/documents/index.html.erb | 19 +++++++---------- app/views/documents/new.html.erb | 13 +++--------- app/views/documents/show.html.erb | 6 ++++-- config/routes.rb | 15 +------------ lib/redmine.rb | 2 +- test/functional/documents_controller_test.rb | 15 +++++++++---- 10 files changed, 59 insertions(+), 63 deletions(-) diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 48b66df2d..3f6c9497e 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -18,9 +18,9 @@ class DocumentsController < ApplicationController default_search_scope :documents model_object Document - before_filter :find_project_by_project_id, :only => [:index, :new] - before_filter :find_model_object, :except => [:index, :new] - before_filter :find_project_from_association, :except => [:index, :new] + before_filter :find_project_by_project_id, :only => [:index, :new, :create] + before_filter :find_model_object, :except => [:index, :new, :create] + before_filter :find_project_from_association, :except => [:index, :new, :create] before_filter :authorize helper :attachments @@ -48,24 +48,34 @@ class DocumentsController < ApplicationController def new @document = @project.documents.build(params[:document]) + end + + def create + @document = @project.documents.build(params[:document]) if request.post? and @document.save attachments = Attachment.attach_files(@document, params[:attachments]) render_attachment_warning_if_needed(@document) flash[:notice] = l(:notice_successful_create) redirect_to :action => 'index', :project_id => @project + else + render :action => 'new' end end def edit - @categories = DocumentCategory.active #TODO: use it in the views - if request.post? and @document.update_attributes(params[:document]) + end + + def update + if request.put? and @document.update_attributes(params[:document]) flash[:notice] = l(:notice_successful_update) redirect_to :action => 'show', :id => @document + else + render :action => 'edit' end end def destroy - @document.destroy + @document.destroy if request.delete? redirect_to :controller => 'documents', :action => 'index', :project_id => @project end diff --git a/app/views/documents/_document.html.erb b/app/views/documents/_document.html.erb index 91bed442f..e14d880a7 100644 --- a/app/views/documents/_document.html.erb +++ b/app/views/documents/_document.html.erb @@ -1,4 +1,4 @@ -

<%= link_to h(document.title), :controller => 'documents', :action => 'show', :id => document %>

+

<%= link_to h(document.title), document_path(document) %>

<%= format_time(document.updated_on) %>

diff --git a/app/views/documents/_form.html.erb b/app/views/documents/_form.html.erb index 3e805c198..600bf66eb 100644 --- a/app/views/documents/_form.html.erb +++ b/app/views/documents/_form.html.erb @@ -1,15 +1,15 @@ -<%= error_messages_for 'document' %> -
- -

-<%= select('document', 'category_id', DocumentCategory.active.collect {|c| [c.name, c.id]}) %>

+<%= f.error_messages %> -

-<%= text_field 'document', 'title', :size => 60 %>

- -

-<%= text_area 'document', 'description', :cols => 60, :rows => 15, :class => 'wiki-edit' %>

- +
+

<%= f.select :category_id, DocumentCategory.active.collect {|c| [c.name, c.id]} %>

+

<%= f.text_field :title, :required => true, :size => 60 %>

+

<%= f.text_area :description, :cols => 60, :rows => 15, :class => 'wiki-edit' %>

<%= wikitoolbar_for 'document_description' %> + +<% if @document.new_record? %> +
+

<%= render :partial => 'attachments/form' %>

+
+<% end %> diff --git a/app/views/documents/edit.html.erb b/app/views/documents/edit.html.erb index 0b9f31f84..04e9b72e1 100644 --- a/app/views/documents/edit.html.erb +++ b/app/views/documents/edit.html.erb @@ -1,8 +1,8 @@

<%=l(:label_document)%>

-<% form_tag({:action => 'edit', :id => @document}, :class => "tabular") do %> - <%= render :partial => 'form' %> - <%= submit_tag l(:button_save) %> +<% labelled_form_for @document do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> +

<%= submit_tag l(:button_save) %>

<% end %> diff --git a/app/views/documents/index.html.erb b/app/views/documents/index.html.erb index 7449b67c6..31f5d2ec7 100644 --- a/app/views/documents/index.html.erb +++ b/app/views/documents/index.html.erb @@ -1,19 +1,16 @@
-<%= link_to_if_authorized l(:label_document_new), - {:controller => 'documents', :action => 'new', :project_id => @project}, - :class => 'icon icon-add', - :onclick => 'Element.show("add-document"); Form.Element.focus("document_title"); return false;' %> +<%= link_to l(:label_document_new), new_project_document_path(@project), :class => 'icon icon-add', + :onclick => 'Element.show("add-document"); Form.Element.focus("document_title"); return false;' if User.current.allowed_to?(:manage_documents, @project) %>
diff --git a/app/views/documents/new.html.erb b/app/views/documents/new.html.erb index 639b4f292..772093e31 100644 --- a/app/views/documents/new.html.erb +++ b/app/views/documents/new.html.erb @@ -1,13 +1,6 @@

<%=l(:label_document_new)%>

-<% form_tag({:controller => 'documents', :action => 'new', :project_id => @project}, :class => "tabular", :multipart => true) do %> -<%= render :partial => 'documents/form' %> - -
-

<%= render :partial => 'attachments/form' %>

-
- -<%= submit_tag l(:button_create) %> +<% labelled_form_for @document, :url => project_documents_path(@project), :html => {:multipart => true} do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> +

<%= submit_tag l(:button_create) %>

<% end %> - - diff --git a/app/views/documents/show.html.erb b/app/views/documents/show.html.erb index 75854357e..fded30052 100644 --- a/app/views/documents/show.html.erb +++ b/app/views/documents/show.html.erb @@ -1,6 +1,8 @@
-<%= link_to_if_authorized l(:button_edit), {:controller => 'documents', :action => 'edit', :id => @document}, :class => 'icon icon-edit', :accesskey => accesskey(:edit) %> -<%= link_to_if_authorized l(:button_delete), {:controller => 'documents', :action => 'destroy', :id => @document}, :confirm => l(:text_are_you_sure), :method => :post, :class => 'icon icon-del' %> +<% if User.current.allowed_to?(:manage_documents, @project) %> +<%= link_to l(:button_edit), edit_document_path(@document), :class => 'icon icon-edit', :accesskey => accesskey(:edit) %> +<%= link_to l(:button_delete), document_path(@document), :confirm => l(:text_are_you_sure), :method => :delete, :class => 'icon icon-del' %> +<% end %>

<%=h @document.title %>

diff --git a/config/routes.rb b/config/routes.rb index 833f23849..3ce5efdbe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,19 +63,6 @@ ActionController::Routing::Routes.draw do |map| end end - map.with_options :controller => 'documents' do |document_routes| - document_routes.with_options :conditions => {:method => :get} do |document_views| - document_views.connect 'projects/:project_id/documents', :action => 'index' - document_views.connect 'projects/:project_id/documents/new', :action => 'new' - document_views.connect 'documents/:id', :action => 'show' - document_views.connect 'documents/:id/edit', :action => 'edit' - end - document_routes.with_options :conditions => {:method => :post} do |document_actions| - document_actions.connect 'projects/:project_id/documents', :action => 'new' - document_actions.connect 'documents/:id/:action', :action => /destroy|edit/ - end - end - map.resources :issue_moves, :only => [:new, :create], :path_prefix => '/issues', :as => 'move' map.resources :queries, :except => [:show] @@ -158,6 +145,7 @@ ActionController::Routing::Routes.draw do |map| project.resources :time_entries, :controller => 'timelog', :path_prefix => 'projects/:project_id' project.resources :queries, :only => [:new, :create] project.resources :issue_categories, :shallow => true + project.resources :documents, :shallow => true, :member => {:add_attachment => :post} project.wiki_start_page 'wiki', :controller => 'wiki', :action => 'show', :conditions => {:method => :get} project.wiki_index 'wiki/index', :controller => 'wiki', :action => 'index', :conditions => {:method => :get} @@ -230,7 +218,6 @@ ActionController::Routing::Routes.draw do |map| #left old routes at the bottom for backwards compat map.connect 'projects/:project_id/issues/:action', :controller => 'issues' - map.connect 'projects/:project_id/documents/:action', :controller => 'documents' map.connect 'projects/:project_id/boards/:action/:id', :controller => 'boards' map.connect 'boards/:board_id/topics/:action/:id', :controller => 'messages' map.connect 'wiki/:id/:page/:action', :page => nil, :controller => 'wiki' diff --git a/lib/redmine.rb b/lib/redmine.rb index 252c1ca62..6028599d2 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -102,7 +102,7 @@ Redmine::AccessControl.map do |map| end map.project_module :documents do |map| - map.permission :manage_documents, {:documents => [:new, :edit, :destroy, :add_attachment]}, :require => :loggedin + map.permission :manage_documents, {:documents => [:new, :create, :edit, :update, :destroy, :add_attachment]}, :require => :loggedin map.permission :view_documents, :documents => [:index, :show, :download] end diff --git a/test/functional/documents_controller_test.rb b/test/functional/documents_controller_test.rb index ad7260c1e..3b4424e51 100644 --- a/test/functional/documents_controller_test.rb +++ b/test/functional/documents_controller_test.rb @@ -86,13 +86,20 @@ LOREM assert_template 'show' end - def test_new_with_one_attachment + def test_new + @request.session[:user_id] = 2 + get :new, :project_id => 1 + assert_response :success + assert_template 'new' + end + + def test_create_with_one_attachment ActionMailer::Base.deliveries.clear Setting.notified_events << 'document_added' @request.session[:user_id] = 2 set_tmp_attachments_directory - post :new, :project_id => 'ecookbook', + post :create, :project_id => 'ecookbook', :document => { :title => 'DocumentsControllerTest#test_post_new', :description => 'This is a new document', :category_id => 2}, @@ -117,7 +124,7 @@ LOREM def test_update @request.session[:user_id] = 2 - post :edit, :id => 1, :document => {:title => 'test_update'} + put :update, :id => 1, :document => {:title => 'test_update'} assert_redirected_to '/documents/1' document = Document.find(1) assert_equal 'test_update', document.title @@ -126,7 +133,7 @@ LOREM def test_destroy @request.session[:user_id] = 2 assert_difference 'Document.count', -1 do - post :destroy, :id => 1 + delete :destroy, :id => 1 end assert_redirected_to '/projects/ecookbook/documents' assert_nil Document.find_by_id(1)