annotate app/controllers/documents_controller.rb @ 1471:d65e60e20a50 bug_531

Close obsolete branch bug_531
author Chris Cannam
date Fri, 23 Nov 2012 18:10:33 +0000
parents 5f33065ddc4b
children 433d4f72a19b
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 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@909 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@909 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@909 25
Chris@0 26 helper :attachments
Chris@909 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@909 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@929 50 @document = @project.documents.build
Chris@929 51 @document.safe_attributes = params[:document]
Chris@0 52 if request.post? and @document.save
Chris@0 53 attachments = Attachment.attach_files(@document, params[:attachments])
Chris@0 54 render_attachment_warning_if_needed(@document)
Chris@0 55 flash[:notice] = l(:notice_successful_create)
Chris@0 56 redirect_to :action => 'index', :project_id => @project
Chris@0 57 end
Chris@0 58 end
Chris@909 59
Chris@0 60 def edit
Chris@909 61 @categories = DocumentCategory.active #TODO: use it in the views
Chris@929 62 @document.safe_attributes = params[:document]
Chris@929 63 if request.post? and @document.save
Chris@0 64 flash[:notice] = l(:notice_successful_update)
Chris@0 65 redirect_to :action => 'show', :id => @document
Chris@0 66 end
Chris@909 67 end
Chris@0 68
Chris@0 69 def destroy
Chris@0 70 @document.destroy
Chris@0 71 redirect_to :controller => 'documents', :action => 'index', :project_id => @project
Chris@0 72 end
Chris@909 73
Chris@0 74 def add_attachment
Chris@0 75 attachments = Attachment.attach_files(@document, params[:attachments])
Chris@0 76 render_attachment_warning_if_needed(@document)
Chris@0 77
Chris@0 78 Mailer.deliver_attachments_added(attachments[:files]) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
Chris@0 79 redirect_to :action => 'show', :id => @document
Chris@0 80 end
Chris@0 81
Chris@0 82 private
Chris@0 83 def find_project
Chris@0 84 @project = Project.find(params[:project_id])
Chris@0 85 rescue ActiveRecord::RecordNotFound
Chris@0 86 render_404
Chris@0 87 end
Chris@0 88 end