You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

documents_controller.rb 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  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.
  8. #
  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.
  13. #
  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.
  17. class DocumentsController < ApplicationController
  18. default_search_scope :documents
  19. model_object Document
  20. before_action :find_project_by_project_id, :only => [:index, :new, :create]
  21. before_action :find_model_object, :except => [:index, :new, :create]
  22. before_action :find_project_from_association, :except => [:index, :new, :create]
  23. before_action :authorize
  24. helper :attachments
  25. helper :custom_fields
  26. def index
  27. @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
  28. documents = @project.documents.includes(:attachments, :category).to_a
  29. case @sort_by
  30. when 'date'
  31. @grouped = documents.group_by {|d| d.updated_on.to_date }
  32. when 'title'
  33. @grouped = documents.group_by {|d| d.title.first.upcase}
  34. when 'author'
  35. @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
  36. else
  37. @grouped = documents.group_by(&:category)
  38. end
  39. @document = @project.documents.build
  40. render :layout => false if request.xhr?
  41. end
  42. def show
  43. @attachments = @document.attachments.to_a
  44. end
  45. def new
  46. @document = @project.documents.build
  47. @document.safe_attributes = params[:document]
  48. end
  49. def create
  50. @document = @project.documents.build
  51. @document.safe_attributes = params[:document]
  52. @document.save_attachments(params[:attachments])
  53. if @document.save
  54. render_attachment_warning_if_needed(@document)
  55. flash[:notice] = l(:notice_successful_create)
  56. redirect_to project_documents_path(@project)
  57. else
  58. render :action => 'new'
  59. end
  60. end
  61. def edit
  62. end
  63. def update
  64. @document.safe_attributes = params[:document]
  65. if @document.save
  66. flash[:notice] = l(:notice_successful_update)
  67. redirect_to document_path(@document)
  68. else
  69. render :action => 'edit'
  70. end
  71. end
  72. def destroy
  73. @document.destroy if request.delete?
  74. redirect_to project_documents_path(@project)
  75. end
  76. def add_attachment
  77. attachments = Attachment.attach_files(@document, params[:attachments])
  78. render_attachment_warning_if_needed(@document)
  79. if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
  80. Mailer.deliver_attachments_added(attachments[:files])
  81. end
  82. redirect_to document_path(@document)
  83. end
  84. end