annotate app/controllers/documents_controller.rb @ 1172:60d42b9850d2 bug_367

Close obsolete branch bug_367
author Chris Cannam
date Fri, 03 Feb 2012 15:20:50 +0000
parents 513646585e45
children cbb26bc654de
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class DocumentsController < ApplicationController
Chris@0 19 default_search_scope :documents
Chris@0 20 model_object Document
Chris@0 21 before_filter :find_project, :only => [:index, :new]
Chris@0 22 before_filter :find_model_object, :except => [:index, :new]
Chris@0 23 before_filter :find_project_from_association, :except => [:index, :new]
Chris@0 24 before_filter :authorize
Chris@0 25
Chris@0 26 helper :attachments
Chris@0 27
Chris@0 28 def index
Chris@0 29 @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
Chris@0 30 documents = @project.documents.find :all, :include => [:attachments, :category]
Chris@0 31 case @sort_by
Chris@0 32 when 'date'
Chris@0 33 @grouped = documents.group_by {|d| d.updated_on.to_date }
Chris@0 34 when 'title'
Chris@0 35 @grouped = documents.group_by {|d| d.title.first.upcase}
Chris@0 36 when 'author'
Chris@0 37 @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
Chris@0 38 else
Chris@0 39 @grouped = documents.group_by(&:category)
Chris@0 40 end
Chris@0 41 @document = @project.documents.build
Chris@0 42 render :layout => false if request.xhr?
Chris@0 43 end
Chris@0 44
Chris@0 45 def show
Chris@0 46 @attachments = @document.attachments.find(:all, :order => "created_on DESC")
Chris@0 47 end
Chris@0 48
Chris@0 49 def new
Chris@0 50 @document = @project.documents.build(params[:document])
Chris@0 51 if request.post? and @document.save
Chris@0 52 attachments = Attachment.attach_files(@document, params[:attachments])
Chris@0 53 render_attachment_warning_if_needed(@document)
Chris@0 54 flash[:notice] = l(:notice_successful_create)
Chris@0 55 redirect_to :action => 'index', :project_id => @project
Chris@0 56 end
Chris@0 57 end
Chris@0 58
Chris@0 59 def edit
Chris@0 60 @categories = DocumentCategory.all
Chris@0 61 if request.post? and @document.update_attributes(params[:document])
Chris@0 62 flash[:notice] = l(:notice_successful_update)
Chris@0 63 redirect_to :action => 'show', :id => @document
Chris@0 64 end
Chris@0 65 end
Chris@0 66
Chris@0 67 def destroy
Chris@0 68 @document.destroy
Chris@0 69 redirect_to :controller => 'documents', :action => 'index', :project_id => @project
Chris@0 70 end
Chris@0 71
Chris@0 72 def add_attachment
Chris@0 73 attachments = Attachment.attach_files(@document, params[:attachments])
Chris@0 74 render_attachment_warning_if_needed(@document)
Chris@0 75
Chris@0 76 Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
Chris@0 77 redirect_to :action => 'show', :id => @document
Chris@0 78 end
Chris@0 79
Chris@0 80 private
Chris@0 81 def find_project
Chris@0 82 @project = Project.find(params[:project_id])
Chris@0 83 rescue ActiveRecord::RecordNotFound
Chris@0 84 render_404
Chris@0 85 end
Chris@0 86 end