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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 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 < ActiveRecord::Base
  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 :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"},
  27. :author => Proc.new {|o| o.attachments.reorder("#{Attachment.table_name}.created_on ASC").first.try(:author) },
  28. :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}}
  29. acts_as_activity_provider :scope => proc { preload(:project) }
  30. validates_presence_of :project, :title, :category
  31. validates_length_of :title, :maximum => 255
  32. after_create_commit :send_notification
  33. scope :visible, lambda {|*args|
  34. joins(:project).
  35. where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args))
  36. }
  37. safe_attributes 'category_id', 'title', 'description', 'custom_fields', 'custom_field_values'
  38. def visible?(user=User.current)
  39. !user.nil? && user.allowed_to?(:view_documents, project)
  40. end
  41. def initialize(attributes=nil, *args)
  42. super
  43. if new_record?
  44. self.category ||= DocumentCategory.default
  45. end
  46. end
  47. def updated_on
  48. unless @updated_on
  49. a = attachments.last
  50. @updated_on = (a && a.created_on) || created_on
  51. end
  52. @updated_on
  53. end
  54. def notified_users
  55. project.notified_users.reject {|user| !visible?(user)}
  56. end
  57. private
  58. def send_notification
  59. if Setting.notified_events.include?('document_added')
  60. Mailer.deliver_document_added(self, User.current)
  61. end
  62. end
  63. end