]> source.dussan.org Git - redmine.git/commitdiff
Resourcified documents.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 30 Nov 2011 19:51:16 +0000 (19:51 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 30 Nov 2011 19:51:16 +0000 (19:51 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8010 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/documents_controller.rb
app/views/documents/_document.html.erb
app/views/documents/_form.html.erb
app/views/documents/edit.html.erb
app/views/documents/index.html.erb
app/views/documents/new.html.erb
app/views/documents/show.html.erb
config/routes.rb
lib/redmine.rb
test/functional/documents_controller_test.rb

index 48b66df2d96489764075bb2355b5f314d7fba45e..3f6c9497ef07bccff2202faeb4df2003b3f20949 100644 (file)
@@ -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
 
index 91bed442f2f9115bf71a3b4a6986dfa77d7b7837..e14d880a7de02b5046f5a5ae7760406c1668d151 100644 (file)
@@ -1,4 +1,4 @@
-<h4><%= link_to h(document.title), :controller => 'documents', :action => 'show', :id => document %></h4>
+<h4><%= link_to h(document.title), document_path(document) %></h4>
 <p><em><%= format_time(document.updated_on) %></em></p>
 
 <div class="wiki">
index 3e805c19875fd5e613d3c368849f6c3a10ded541..600bf66ebf4e9128bb991a8ef0b2a261057fa346 100644 (file)
@@ -1,15 +1,15 @@
-<%= error_messages_for 'document' %>
-<div class="box">
-<!--[form:document]-->
-<p><label for="document_category_id"><%=l(:field_category)%></label>
-<%= select('document', 'category_id', DocumentCategory.active.collect {|c| [c.name, c.id]}) %></p>
+<%= f.error_messages %>
 
-<p><label for="document_title"><%=l(:field_title)%> <span class="required">*</span></label>
-<%= text_field 'document', 'title', :size => 60 %></p>
-
-<p><label for="document_description"><%=l(:field_description)%></label>
-<%= text_area 'document', 'description', :cols => 60, :rows => 15, :class => 'wiki-edit' %></p>
-<!--[eoform:document]-->
+<div class="box tabular">
+<p><%= f.select :category_id, DocumentCategory.active.collect {|c| [c.name, c.id]} %></p>
+<p><%= f.text_field :title, :required => true, :size => 60 %></p>
+<p><%= f.text_area :description, :cols => 60, :rows => 15, :class => 'wiki-edit' %></p>
 </div>
 
 <%= wikitoolbar_for 'document_description' %>
+
+<% if @document.new_record? %>
+<div class="box tabular">
+<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
+</div>
+<% end %>
index 0b9f31f8453863cab0e056ddd9e029047bcce7a5..04e9b72e11e400be21bca9cc0790c7625ca36047 100644 (file)
@@ -1,8 +1,8 @@
 <h2><%=l(:label_document)%></h2>
 
-<% 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} %>
+  <p><%= submit_tag l(:button_save) %></p>
 <% end %>
 
 
index 7449b67c66bb8b088587d5c3fd5200ac5dcb0c5a..31f5d2ec744cf8179afcbb22e8a35743a3085919 100644 (file)
@@ -1,19 +1,16 @@
 <div class="contextual">
-<%= 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) %>
 </div>
 
 <div id="add-document" style="display:none;">
 <h2><%=l(:label_document_new)%></h2>
-<% form_tag({:controller => 'documents', :action => 'new', :project_id => @project}, :class => "tabular", :multipart => true) do %>
-<%= render :partial => 'documents/form' %>
-<div class="box">
-<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
-</div>
-<%= submit_tag l(:button_create) %>
-<%= link_to l(:button_cancel), "#", :onclick => 'Element.hide("add-document")' %>
+<% labelled_form_for @document, :url => project_documents_path(@project), :html => {:multipart => true} do |f| %>
+<%= render :partial => 'form', :locals => {:f => f} %>
+<p>
+       <%= submit_tag l(:button_create) %>
+  <%= link_to l(:button_cancel), "#", :onclick => 'Element.hide("add-document")' %>
+</p>
 <% end %>
 </div>
 
index 639b4f2922304aacef0d1ff482a2012d7d3cc101..772093e31d17f27d637116a386905f03d7d8f01f 100644 (file)
@@ -1,13 +1,6 @@
 <h2><%=l(:label_document_new)%></h2>
 
-<% form_tag({:controller => 'documents', :action => 'new', :project_id => @project}, :class => "tabular", :multipart => true) do %>
-<%= render :partial => 'documents/form' %>
-
-<div class="box">
-<p><label><%=l(:label_attachment_plural)%></label><%= render :partial => 'attachments/form' %></p>
-</div>
-
-<%= 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} %>
+  <p><%= submit_tag l(:button_create) %></p>
 <% end %>
-
-
index 75854357e9d5c73a9ba4935c99f07089893375e5..fded300524ec1e7490f3e5a042a2b861620eec8c 100644 (file)
@@ -1,6 +1,8 @@
 <div class="contextual">
-<%= 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 %>
 </div>
 
 <h2><%=h @document.title %></h2>
index 833f238498edd58c16760574eaadac6d652a36eb..3ce5efdbecc5dd12a3a501946b827e1922c1ff34 100644 (file)
@@ -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'
index 252c1ca62e0d7799975eb0f279cd32b9580fe216..6028599d2615b7a8a40e9902237b500d8f639222 100644 (file)
@@ -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
 
index ad7260c1eb209697a07d2dfe723a1132574b8921..3b4424e510b6ac3be2052a4a3e86a2a5b082c0c7 100644 (file)
@@ -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)