annotate app/controllers/documents_controller.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents e248c7af89ec
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1115 21 before_filter :find_project_by_project_id, :only => [:index, :new, :create]
Chris@1115 22 before_filter :find_model_object, :except => [:index, :new, :create]
Chris@1115 23 before_filter :find_project_from_association, :except => [:index, :new, :create]
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@1464 30 documents = @project.documents.includes(:attachments, :category).all
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@1464 46 @attachments = @document.attachments.all
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@1115 52 end
Chris@1115 53
Chris@1115 54 def create
Chris@1115 55 @document = @project.documents.build
Chris@1115 56 @document.safe_attributes = params[:document]
Chris@1115 57 @document.save_attachments(params[:attachments])
Chris@1115 58 if @document.save
Chris@0 59 render_attachment_warning_if_needed(@document)
Chris@0 60 flash[:notice] = l(:notice_successful_create)
Chris@1464 61 redirect_to project_documents_path(@project)
Chris@1115 62 else
Chris@1115 63 render :action => 'new'
Chris@0 64 end
Chris@0 65 end
Chris@909 66
Chris@0 67 def edit
Chris@1115 68 end
Chris@1115 69
Chris@1115 70 def update
Chris@929 71 @document.safe_attributes = params[:document]
Chris@1115 72 if request.put? and @document.save
Chris@0 73 flash[:notice] = l(:notice_successful_update)
Chris@1464 74 redirect_to document_path(@document)
Chris@1115 75 else
Chris@1115 76 render :action => 'edit'
Chris@0 77 end
Chris@909 78 end
Chris@0 79
Chris@0 80 def destroy
Chris@1115 81 @document.destroy if request.delete?
Chris@1464 82 redirect_to project_documents_path(@project)
Chris@0 83 end
Chris@909 84
Chris@0 85 def add_attachment
Chris@0 86 attachments = Attachment.attach_files(@document, params[:attachments])
Chris@0 87 render_attachment_warning_if_needed(@document)
Chris@0 88
Chris@1115 89 if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
Chris@1115 90 Mailer.attachments_added(attachments[:files]).deliver
Chris@1115 91 end
Chris@1464 92 redirect_to document_path(@document)
Chris@0 93 end
Chris@0 94 end