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