##// END OF EJS Templates
Moved ProjectsController#list_documents and add_document to DocumentsController#index and new....
Jean-Philippe Lang -
r998:8e00f57a886a
parent child
Show More
@@ -0,0 +1,64
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'documents_controller'
20
21 # Re-raise errors caught by the controller.
22 class DocumentsController; def rescue_action(e) raise e end; end
23
24 class DocumentsControllerTest < Test::Unit::TestCase
25 fixtures :projects, :users, :roles, :members, :enabled_modules, :documents, :enumerations
26
27 def setup
28 @controller = DocumentsController.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
32 end
33
34 def test_index
35 get :index, :project_id => 'ecookbook'
36 assert_response :success
37 assert_template 'index'
38 assert_not_nil assigns(:grouped)
39 end
40
41 def test_new_with_one_attachment
42 @request.session[:user_id] = 2
43 post :new, :project_id => 'ecookbook',
44 :document => { :title => 'DocumentsControllerTest#test_post_new',
45 :description => 'This is a new document',
46 :category_id => 2},
47 :attachments => [ ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/testfile.txt', 'text/plain') ]
48
49 assert_redirected_to 'projects/ecookbook/documents'
50
51 document = Document.find_by_title('DocumentsControllerTest#test_post_new')
52 assert_not_nil document
53 assert_equal Enumeration.find(2), document.category
54 assert_equal 1, document.attachments.size
55 assert_equal 'testfile.txt', document.attachments.first.filename
56 end
57
58 def test_destroy
59 @request.session[:user_id] = 2
60 post :destroy, :id => 1
61 assert_redirected_to 'projects/ecookbook/documents'
62 assert_nil Document.find_by_id(1)
63 end
64 end
@@ -17,12 +17,40
17
17
18 class DocumentsController < ApplicationController
18 class DocumentsController < ApplicationController
19 layout 'base'
19 layout 'base'
20 before_filter :find_project, :authorize
20 before_filter :find_project, :only => [:index, :new]
21
21 before_filter :find_document, :except => [:index, :new]
22 before_filter :authorize
23
24 def index
25 @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
26 documents = @project.documents.find :all, :include => [:attachments, :category]
27 case @sort_by
28 when 'date'
29 @grouped = documents.group_by {|d| d.created_on.to_date }
30 when 'title'
31 @grouped = documents.group_by {|d| d.title.first.upcase}
32 when 'author'
33 @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
34 else
35 @grouped = documents.group_by(&:category)
36 end
37 render :layout => false if request.xhr?
38 end
39
22 def show
40 def show
23 @attachments = @document.attachments.find(:all, :order => "created_on DESC")
41 @attachments = @document.attachments.find(:all, :order => "created_on DESC")
24 end
42 end
25
43
44 def new
45 @document = @project.documents.build(params[:document])
46 if request.post? and @document.save
47 attach_files(@document, params[:attachments])
48 flash[:notice] = l(:notice_successful_create)
49 Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added')
50 redirect_to :action => 'index', :project_id => @project
51 end
52 end
53
26 def edit
54 def edit
27 @categories = Enumeration::get_values('DCAT')
55 @categories = Enumeration::get_values('DCAT')
28 if request.post? and @document.update_attributes(params[:document])
56 if request.post? and @document.update_attributes(params[:document])
@@ -33,7 +61,7 class DocumentsController < ApplicationController
33
61
34 def destroy
62 def destroy
35 @document.destroy
63 @document.destroy
36 redirect_to :controller => 'projects', :action => 'list_documents', :id => @project
64 redirect_to :controller => 'documents', :action => 'index', :project_id => @project
37 end
65 end
38
66
39 def download
67 def download
@@ -57,9 +85,15 class DocumentsController < ApplicationController
57
85
58 private
86 private
59 def find_project
87 def find_project
88 @project = Project.find(params[:project_id])
89 rescue ActiveRecord::RecordNotFound
90 render_404
91 end
92
93 def find_document
60 @document = Document.find(params[:id])
94 @document = Document.find(params[:id])
61 @project = @document.project
95 @project = @document.project
62 rescue ActiveRecord::RecordNotFound
96 rescue ActiveRecord::RecordNotFound
63 render_404
97 render_404
64 end
98 end
65 end
99 end
@@ -177,34 +177,6 class ProjectsController < ApplicationController
177 end
177 end
178 end
178 end
179
179
180 # Add a new document to @project
181 def add_document
182 @document = @project.documents.build(params[:document])
183 if request.post? and @document.save
184 attach_files(@document, params[:attachments])
185 flash[:notice] = l(:notice_successful_create)
186 Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added')
187 redirect_to :action => 'list_documents', :id => @project
188 end
189 end
190
191 # Show documents list of @project
192 def list_documents
193 @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
194 documents = @project.documents.find :all, :include => [:attachments, :category]
195 case @sort_by
196 when 'date'
197 @grouped = documents.group_by {|d| d.created_on.to_date }
198 when 'title'
199 @grouped = documents.group_by {|d| d.title.first.upcase}
200 when 'author'
201 @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
202 else
203 @grouped = documents.group_by(&:category)
204 end
205 render :layout => false if request.xhr?
206 end
207
208 # Add a new issue to @project
180 # Add a new issue to @project
209 # The new issue will be created from an existing one if copy_from parameter is given
181 # The new issue will be created from an existing one if copy_from parameter is given
210 def add_issue
182 def add_issue
@@ -1,13 +1,13
1 <div class="contextual">
1 <div class="contextual">
2 <%= link_to_if_authorized l(:label_document_new),
2 <%= link_to_if_authorized l(:label_document_new),
3 {:controller => 'projects', :action => 'add_document', :id => @project},
3 {:controller => 'documents', :action => 'new', :project_id => @project},
4 :class => 'icon icon-add',
4 :class => 'icon icon-add',
5 :onclick => 'Element.show("add-document"); return false;' %>
5 :onclick => 'Element.show("add-document"); return false;' %>
6 </div>
6 </div>
7
7
8 <div id="add-document" style="display:none;">
8 <div id="add-document" style="display:none;">
9 <h2><%=l(:label_document_new)%></h2>
9 <h2><%=l(:label_document_new)%></h2>
10 <% form_tag({:action => 'add_document', :id => @project}, :class => "tabular", :multipart => true) do %>
10 <% form_tag({:controller => 'documents', :action => 'new', :project_id => @project}, :class => "tabular", :multipart => true) do %>
11 <%= render :partial => 'documents/form' %>
11 <%= render :partial => 'documents/form' %>
12 <div class="box">
12 <div class="box">
13 <%= render :partial => 'common/attachments_form'%>
13 <%= render :partial => 'common/attachments_form'%>
@@ -1,6 +1,6
1 <h2><%=l(:label_document_new)%></h2>
1 <h2><%=l(:label_document_new)%></h2>
2
2
3 <% form_tag( { :action => 'add_document', :id => @project }, :class => "tabular", :multipart => true) do %>
3 <% form_tag({:controller => 'documents', :action => 'new', :project_id => @project}, :class => "tabular", :multipart => true) do %>
4 <%= render :partial => 'documents/form' %>
4 <%= render :partial => 'documents/form' %>
5
5
6 <div class="box">
6 <div class="box">
@@ -16,6 +16,7 ActionController::Routing::Routes.draw do |map|
16 map.connect 'issues/:issue_id/relations/:action/:id', :controller => 'issue_relations'
16 map.connect 'issues/:issue_id/relations/:action/:id', :controller => 'issue_relations'
17 map.connect 'projects/:project_id/issues/:action', :controller => 'issues'
17 map.connect 'projects/:project_id/issues/:action', :controller => 'issues'
18 map.connect 'projects/:project_id/news/:action', :controller => 'news'
18 map.connect 'projects/:project_id/news/:action', :controller => 'news'
19 map.connect 'projects/:project_id/documents/:action', :controller => 'documents'
19 map.connect 'projects/:project_id/boards/:action/:id', :controller => 'boards'
20 map.connect 'projects/:project_id/boards/:action/:id', :controller => 'boards'
20 map.connect 'boards/:board_id/topics/:action/:id', :controller => 'messages'
21 map.connect 'boards/:board_id/topics/:action/:id', :controller => 'messages'
21
22
@@ -58,8 +58,8 Redmine::AccessControl.map do |map|
58 end
58 end
59
59
60 map.project_module :documents do |map|
60 map.project_module :documents do |map|
61 map.permission :manage_documents, {:projects => :add_document, :documents => [:edit, :destroy, :add_attachment, :destroy_attachment]}, :require => :loggedin
61 map.permission :manage_documents, {:documents => [:new, :edit, :destroy, :add_attachment, :destroy_attachment]}, :require => :loggedin
62 map.permission :view_documents, :projects => :list_documents, :documents => [:show, :download]
62 map.permission :view_documents, :documents => [:index, :show, :download]
63 end
63 end
64
64
65 map.project_module :files do |map|
65 map.project_module :files do |map|
@@ -97,7 +97,7 Redmine::MenuManager.map :project_menu do |menu|
97 menu.push :label_roadmap, :controller => 'projects', :action => 'roadmap'
97 menu.push :label_roadmap, :controller => 'projects', :action => 'roadmap'
98 menu.push :label_issue_plural, { :controller => 'issues', :action => 'index' }, :param => :project_id
98 menu.push :label_issue_plural, { :controller => 'issues', :action => 'index' }, :param => :project_id
99 menu.push :label_news_plural, { :controller => 'news', :action => 'index' }, :param => :project_id
99 menu.push :label_news_plural, { :controller => 'news', :action => 'index' }, :param => :project_id
100 menu.push :label_document_plural, :controller => 'projects', :action => 'list_documents'
100 menu.push :label_document_plural, { :controller => 'documents', :action => 'index' }, :param => :project_id
101 menu.push :label_wiki, { :controller => 'wiki', :action => 'index', :page => nil }, :if => Proc.new { |p| p.wiki && !p.wiki.new_record? }
101 menu.push :label_wiki, { :controller => 'wiki', :action => 'index', :page => nil }, :if => Proc.new { |p| p.wiki && !p.wiki.new_record? }
102 menu.push :label_board_plural, { :controller => 'boards', :action => 'index', :id => nil }, :param => :project_id, :if => Proc.new { |p| p.boards.any? }
102 menu.push :label_board_plural, { :controller => 'boards', :action => 'index', :id => nil }, :param => :project_id, :if => Proc.new { |p| p.boards.any? }
103 menu.push :label_attachment_plural, :controller => 'projects', :action => 'list_files'
103 menu.push :label_attachment_plural, :controller => 'projects', :action => 'list_files'
@@ -92,13 +92,6 class ProjectsControllerTest < Test::Unit::TestCase
92 assert_nil Project.find_by_id(1)
92 assert_nil Project.find_by_id(1)
93 end
93 end
94
94
95 def test_list_documents
96 get :list_documents, :id => 1
97 assert_response :success
98 assert_template 'list_documents'
99 assert_not_nil assigns(:grouped)
100 end
101
102 def test_bulk_edit_issues
95 def test_bulk_edit_issues
103 @request.session[:user_id] = 2
96 @request.session[:user_id] = 2
104 # update issues priority
97 # update issues priority
General Comments 0
You need to be logged in to leave comments. Login now