1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
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.
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.
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.
18 class DocumentsController < ApplicationController
19 default_search_scope :documents
21 before_filter :find_project, :only => [:index, :new]
22 before_filter :find_model_object, :except => [:index, :new]
23 before_filter :find_project_from_association, :except => [:index, :new]
24 before_filter :authorize
29 @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
30 documents = @project.documents.find :all, :include => [:attachments, :category]
33 @grouped = documents.group_by {|d| d.updated_on.to_date }
35 @grouped = documents.group_by {|d| d.title.first.upcase}
37 @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
39 @grouped = documents.group_by(&:category)
41 @document = @project.documents.build
42 render :layout => false if request.xhr?
46 @attachments = @document.attachments.find(:all, :order => "created_on DESC")
50 @document = @project.documents.build(params[:document])
51 if request.post? and @document.save
52 attachments = Attachment.attach_files(@document, params[:attachments])
53 render_attachment_warning_if_needed(@document)
54 flash[:notice] = l(:notice_successful_create)
55 redirect_to :action => 'index', :project_id => @project
60 @categories = DocumentCategory.all
61 if request.post? and @document.update_attributes(params[:document])
62 flash[:notice] = l(:notice_successful_update)
63 redirect_to :action => 'show', :id => @document
69 redirect_to :controller => 'documents', :action => 'index', :project_id => @project
73 attachments = Attachment.attach_files(@document, params[:attachments])
74 render_attachment_warning_if_needed(@document)
76 Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
77 redirect_to :action => 'show', :id => @document
82 @project = Project.find(params[:project_id])
83 rescue ActiveRecord::RecordNotFound