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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Document < ActiveRecord::Base
  18. include Redmine::SafeAttributes
  19. belongs_to :project
  20. belongs_to :category, :class_name => "DocumentCategory"
  21. acts_as_attachable :delete_permission => :delete_documents
  22. acts_as_customizable
  23. acts_as_searchable :columns => ['title', "#{table_name}.description"],
  24. :preload => :project
  25. acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"},
  26. :author => Proc.new {|o| o.attachments.reorder("#{Attachment.table_name}.created_on ASC").first.try(:author) },
  27. :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}}
  28. acts_as_activity_provider :scope => preload(:project)
  29. validates_presence_of :project, :title, :category
  30. validates_length_of :title, :maximum => 255
  31. after_create_commit :send_notification
  32. scope :visible, lambda {|*args|
  33. joins(:project).
  34. where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args))
  35. }
  36. safe_attributes 'category_id', 'title', 'description', 'custom_fields', 'custom_field_values'
  37. def visible?(user=User.current)
  38. !user.nil? && user.allowed_to?(:view_documents, project)
  39. end
  40. def initialize(attributes=nil, *args)
  41. super
  42. if new_record?
  43. self.category ||= DocumentCategory.default
  44. end
  45. end
  46. def updated_on
  47. unless @updated_on
  48. a = attachments.last
  49. @updated_on = (a && a.created_on) || created_on
  50. end
  51. @updated_on
  52. end
  53. def notified_users
  54. project.notified_users.reject {|user| !visible?(user)}
  55. end
  56. private
  57. def send_notification
  58. if Setting.notified_events.include?('document_added')
  59. Mailer.deliver_document_added(self, User.current)
  60. end
  61. end
  62. end