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.

document.rb 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class Document < ApplicationRecord
  19. include Redmine::SafeAttributes
  20. belongs_to :project
  21. belongs_to :category, :class_name => "DocumentCategory"
  22. acts_as_attachable :delete_permission => :delete_documents
  23. acts_as_customizable
  24. acts_as_searchable :columns => ['title', "#{table_name}.description"],
  25. :preload => :project
  26. acts_as_event(
  27. :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"},
  28. :author =>
  29. Proc.new do |o|
  30. o.attachments.reorder("#{Attachment.table_name}.created_on ASC").
  31. first.try(:author)
  32. end,
  33. :url =>
  34. Proc.new do |o|
  35. {:controller => 'documents', :action => 'show', :id => o.id}
  36. end
  37. )
  38. acts_as_activity_provider :scope => proc {preload(:project)}
  39. validates_presence_of :project, :title, :category
  40. validates_length_of :title, :maximum => 255
  41. after_create_commit :send_notification
  42. scope :visible, (lambda do |*args|
  43. joins(:project).
  44. where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args))
  45. end)
  46. safe_attributes 'category_id', 'title', 'description', 'custom_fields', 'custom_field_values'
  47. def visible?(user=User.current)
  48. !user.nil? && user.allowed_to?(:view_documents, project)
  49. end
  50. def initialize(attributes=nil, *args)
  51. super
  52. if new_record?
  53. self.category ||= DocumentCategory.default
  54. end
  55. end
  56. def updated_on
  57. unless @updated_on
  58. a = attachments.last
  59. @updated_on = (a && a.created_on) || created_on
  60. end
  61. @updated_on
  62. end
  63. def notified_users
  64. project.notified_users.reject {|user| !visible?(user)}
  65. end
  66. private
  67. def send_notification
  68. if Setting.notified_events.include?('document_added')
  69. Mailer.deliver_document_added(self, User.current)
  70. end
  71. end
  72. end