##// END OF EJS Templates
Fixed: default category ignored when adding a document (#2328)....
Jean-Philippe Lang -
r2122:b21b6c365cc1
parent child
Show More
@@ -0,0 +1,37
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 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
20 class DocumentTest < Test::Unit::TestCase
21 fixtures :projects, :enumerations, :documents
22
23 def test_create
24 doc = Document.new(:project => Project.find(1), :title => 'New document', :category => Enumeration.find_by_name('User documentation'))
25 assert doc.save
26 end
27
28 def test_create_with_default_category
29 # Sets a default category
30 e = Enumeration.find_by_name('Technical documentation')
31 e.update_attributes(:is_default => true)
32
33 doc = Document.new(:project => Project.find(1), :title => 'New document')
34 assert_equal e, doc.category
35 assert doc.save
36 end
37 end
@@ -1,87 +1,88
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class DocumentsController < ApplicationController
18 class DocumentsController < ApplicationController
19 before_filter :find_project, :only => [:index, :new]
19 before_filter :find_project, :only => [:index, :new]
20 before_filter :find_document, :except => [:index, :new]
20 before_filter :find_document, :except => [:index, :new]
21 before_filter :authorize
21 before_filter :authorize
22
22
23 helper :attachments
23 helper :attachments
24
24
25 def index
25 def index
26 @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
26 @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
27 documents = @project.documents.find :all, :include => [:attachments, :category]
27 documents = @project.documents.find :all, :include => [:attachments, :category]
28 case @sort_by
28 case @sort_by
29 when 'date'
29 when 'date'
30 @grouped = documents.group_by {|d| d.created_on.to_date }
30 @grouped = documents.group_by {|d| d.created_on.to_date }
31 when 'title'
31 when 'title'
32 @grouped = documents.group_by {|d| d.title.first.upcase}
32 @grouped = documents.group_by {|d| d.title.first.upcase}
33 when 'author'
33 when 'author'
34 @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
34 @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
35 else
35 else
36 @grouped = documents.group_by(&:category)
36 @grouped = documents.group_by(&:category)
37 end
37 end
38 @document = @project.documents.build
38 render :layout => false if request.xhr?
39 render :layout => false if request.xhr?
39 end
40 end
40
41
41 def show
42 def show
42 @attachments = @document.attachments.find(:all, :order => "created_on DESC")
43 @attachments = @document.attachments.find(:all, :order => "created_on DESC")
43 end
44 end
44
45
45 def new
46 def new
46 @document = @project.documents.build(params[:document])
47 @document = @project.documents.build(params[:document])
47 if request.post? and @document.save
48 if request.post? and @document.save
48 attach_files(@document, params[:attachments])
49 attach_files(@document, params[:attachments])
49 flash[:notice] = l(:notice_successful_create)
50 flash[:notice] = l(:notice_successful_create)
50 Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added')
51 Mailer.deliver_document_added(@document) if Setting.notified_events.include?('document_added')
51 redirect_to :action => 'index', :project_id => @project
52 redirect_to :action => 'index', :project_id => @project
52 end
53 end
53 end
54 end
54
55
55 def edit
56 def edit
56 @categories = Enumeration::get_values('DCAT')
57 @categories = Enumeration::get_values('DCAT')
57 if request.post? and @document.update_attributes(params[:document])
58 if request.post? and @document.update_attributes(params[:document])
58 flash[:notice] = l(:notice_successful_update)
59 flash[:notice] = l(:notice_successful_update)
59 redirect_to :action => 'show', :id => @document
60 redirect_to :action => 'show', :id => @document
60 end
61 end
61 end
62 end
62
63
63 def destroy
64 def destroy
64 @document.destroy
65 @document.destroy
65 redirect_to :controller => 'documents', :action => 'index', :project_id => @project
66 redirect_to :controller => 'documents', :action => 'index', :project_id => @project
66 end
67 end
67
68
68 def add_attachment
69 def add_attachment
69 attachments = attach_files(@document, params[:attachments])
70 attachments = attach_files(@document, params[:attachments])
70 Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('document_added')
71 Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('document_added')
71 redirect_to :action => 'show', :id => @document
72 redirect_to :action => 'show', :id => @document
72 end
73 end
73
74
74 private
75 private
75 def find_project
76 def find_project
76 @project = Project.find(params[:project_id])
77 @project = Project.find(params[:project_id])
77 rescue ActiveRecord::RecordNotFound
78 rescue ActiveRecord::RecordNotFound
78 render_404
79 render_404
79 end
80 end
80
81
81 def find_document
82 def find_document
82 @document = Document.find(params[:id])
83 @document = Document.find(params[:id])
83 @project = @document.project
84 @project = @document.project
84 rescue ActiveRecord::RecordNotFound
85 rescue ActiveRecord::RecordNotFound
85 render_404
86 render_404
86 end
87 end
87 end
88 end
@@ -1,31 +1,37
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Document < ActiveRecord::Base
18 class Document < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 belongs_to :category, :class_name => "Enumeration", :foreign_key => "category_id"
20 belongs_to :category, :class_name => "Enumeration", :foreign_key => "category_id"
21 acts_as_attachable :delete_permission => :manage_documents
21 acts_as_attachable :delete_permission => :manage_documents
22
22
23 acts_as_searchable :columns => ['title', "#{table_name}.description"], :include => :project
23 acts_as_searchable :columns => ['title', "#{table_name}.description"], :include => :project
24 acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"},
24 acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"},
25 :author => Proc.new {|o| (a = o.attachments.find(:first, :order => "#{Attachment.table_name}.created_on ASC")) ? a.author : nil },
25 :author => Proc.new {|o| (a = o.attachments.find(:first, :order => "#{Attachment.table_name}.created_on ASC")) ? a.author : nil },
26 :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}}
26 :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}}
27 acts_as_activity_provider :find_options => {:include => :project}
27 acts_as_activity_provider :find_options => {:include => :project}
28
28
29 validates_presence_of :project, :title, :category
29 validates_presence_of :project, :title, :category
30 validates_length_of :title, :maximum => 60
30 validates_length_of :title, :maximum => 60
31
32 def after_initialize
33 if new_record?
34 self.category ||= Enumeration.default('DCAT')
35 end
36 end
31 end
37 end
@@ -1,66 +1,75
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'documents_controller'
19 require 'documents_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class DocumentsController; def rescue_action(e) raise e end; end
22 class DocumentsController; def rescue_action(e) raise e end; end
23
23
24 class DocumentsControllerTest < Test::Unit::TestCase
24 class DocumentsControllerTest < Test::Unit::TestCase
25 fixtures :projects, :users, :roles, :members, :enabled_modules, :documents, :enumerations
25 fixtures :projects, :users, :roles, :members, :enabled_modules, :documents, :enumerations
26
26
27 def setup
27 def setup
28 @controller = DocumentsController.new
28 @controller = DocumentsController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
31 User.current = nil
32 end
32 end
33
33
34 def test_index
34 def test_index
35 # Sets a default category
36 e = Enumeration.find_by_name('Technical documentation')
37 e.update_attributes(:is_default => true)
38
35 get :index, :project_id => 'ecookbook'
39 get :index, :project_id => 'ecookbook'
36 assert_response :success
40 assert_response :success
37 assert_template 'index'
41 assert_template 'index'
38 assert_not_nil assigns(:grouped)
42 assert_not_nil assigns(:grouped)
43
44 # Default category selected in the new document form
45 assert_tag :select, :attributes => {:name => 'document[category_id]'},
46 :child => {:tag => 'option', :attributes => {:selected => 'selected'},
47 :content => 'Technical documentation'}
39 end
48 end
40
49
41 def test_new_with_one_attachment
50 def test_new_with_one_attachment
42 @request.session[:user_id] = 2
51 @request.session[:user_id] = 2
43 set_tmp_attachments_directory
52 set_tmp_attachments_directory
44
53
45 post :new, :project_id => 'ecookbook',
54 post :new, :project_id => 'ecookbook',
46 :document => { :title => 'DocumentsControllerTest#test_post_new',
55 :document => { :title => 'DocumentsControllerTest#test_post_new',
47 :description => 'This is a new document',
56 :description => 'This is a new document',
48 :category_id => 2},
57 :category_id => 2},
49 :attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
58 :attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
50
59
51 assert_redirected_to 'projects/ecookbook/documents'
60 assert_redirected_to 'projects/ecookbook/documents'
52
61
53 document = Document.find_by_title('DocumentsControllerTest#test_post_new')
62 document = Document.find_by_title('DocumentsControllerTest#test_post_new')
54 assert_not_nil document
63 assert_not_nil document
55 assert_equal Enumeration.find(2), document.category
64 assert_equal Enumeration.find(2), document.category
56 assert_equal 1, document.attachments.size
65 assert_equal 1, document.attachments.size
57 assert_equal 'testfile.txt', document.attachments.first.filename
66 assert_equal 'testfile.txt', document.attachments.first.filename
58 end
67 end
59
68
60 def test_destroy
69 def test_destroy
61 @request.session[:user_id] = 2
70 @request.session[:user_id] = 2
62 post :destroy, :id => 1
71 post :destroy, :id => 1
63 assert_redirected_to 'projects/ecookbook/documents'
72 assert_redirected_to 'projects/ecookbook/documents'
64 assert_nil Document.find_by_id(1)
73 assert_nil Document.find_by_id(1)
65 end
74 end
66 end
75 end
General Comments 0
You need to be logged in to leave comments. Login now