comparison app/controllers/documents_controller.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents 433d4f72a19b
children 622f24f53b42
comparison
equal deleted inserted replaced
1209:1b1138f6f55e 1338:25603efa57b5
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang 2 # Copyright (C) 2006-2012 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.
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 default_search_scope :documents 19 default_search_scope :documents
20 model_object Document 20 model_object Document
21 before_filter :find_project, :only => [:index, :new] 21 before_filter :find_project_by_project_id, :only => [:index, :new, :create]
22 before_filter :find_model_object, :except => [:index, :new] 22 before_filter :find_model_object, :except => [:index, :new, :create]
23 before_filter :find_project_from_association, :except => [:index, :new] 23 before_filter :find_project_from_association, :except => [:index, :new, :create]
24 before_filter :authorize 24 before_filter :authorize
25 25
26 helper :attachments 26 helper :attachments
27 27
28 def index 28 def index
47 end 47 end
48 48
49 def new 49 def new
50 @document = @project.documents.build 50 @document = @project.documents.build
51 @document.safe_attributes = params[:document] 51 @document.safe_attributes = params[:document]
52 if request.post? and @document.save 52 end
53 attachments = Attachment.attach_files(@document, params[:attachments]) 53
54 def create
55 @document = @project.documents.build
56 @document.safe_attributes = params[:document]
57 @document.save_attachments(params[:attachments])
58 if @document.save
54 render_attachment_warning_if_needed(@document) 59 render_attachment_warning_if_needed(@document)
55 flash[:notice] = l(:notice_successful_create) 60 flash[:notice] = l(:notice_successful_create)
56 redirect_to :action => 'index', :project_id => @project 61 redirect_to :action => 'index', :project_id => @project
62 else
63 render :action => 'new'
57 end 64 end
58 end 65 end
59 66
60 def edit 67 def edit
61 @categories = DocumentCategory.active #TODO: use it in the views 68 end
69
70 def update
62 @document.safe_attributes = params[:document] 71 @document.safe_attributes = params[:document]
63 if request.post? and @document.save 72 if request.put? and @document.save
64 flash[:notice] = l(:notice_successful_update) 73 flash[:notice] = l(:notice_successful_update)
65 redirect_to :action => 'show', :id => @document 74 redirect_to :action => 'show', :id => @document
75 else
76 render :action => 'edit'
66 end 77 end
67 end 78 end
68 79
69 def destroy 80 def destroy
70 @document.destroy 81 @document.destroy if request.delete?
71 redirect_to :controller => 'documents', :action => 'index', :project_id => @project 82 redirect_to :controller => 'documents', :action => 'index', :project_id => @project
72 end 83 end
73 84
74 def add_attachment 85 def add_attachment
75 attachments = Attachment.attach_files(@document, params[:attachments]) 86 attachments = Attachment.attach_files(@document, params[:attachments])
76 render_attachment_warning_if_needed(@document) 87 render_attachment_warning_if_needed(@document)
77 88
78 Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added') 89 if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
90 Mailer.attachments_added(attachments[:files]).deliver
91 end
79 redirect_to :action => 'show', :id => @document 92 redirect_to :action => 'show', :id => @document
80 end 93 end
81
82 private
83 def find_project
84 @project = Project.find(params[:project_id])
85 rescue ActiveRecord::RecordNotFound
86 render_404
87 end
88 end 94 end