annotate .svn/pristine/30/30e01132e9ab5b3c9ae62f165db3ff3e34e482a8.svn-base @ 929:5f33065ddc4b redmine-1.3

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