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.

acts_as_attachable.rb 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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. module Redmine
  19. module Acts
  20. module Attachable
  21. def self.included(base)
  22. base.extend ClassMethods
  23. end
  24. module ClassMethods
  25. def acts_as_attachable(options = {})
  26. cattr_accessor :attachable_options
  27. self.attachable_options = {}
  28. attachable_options[:view_permission] = options.delete(:view_permission) || "view_#{self.name.pluralize.underscore}".to_sym
  29. attachable_options[:edit_permission] = options.delete(:edit_permission) || "edit_#{self.name.pluralize.underscore}".to_sym
  30. attachable_options[:delete_permission] = options.delete(:delete_permission) || "edit_#{self.name.pluralize.underscore}".to_sym
  31. has_many :attachments, lambda {order("#{Attachment.table_name}.created_on ASC, #{Attachment.table_name}.id ASC")},
  32. **options, as: :container, dependent: :destroy, inverse_of: :container
  33. send :include, Redmine::Acts::Attachable::InstanceMethods
  34. before_save :attach_saved_attachments
  35. after_rollback :detach_saved_attachments
  36. validate :warn_about_failed_attachments
  37. end
  38. end
  39. module InstanceMethods
  40. def self.included(base)
  41. base.extend ClassMethods
  42. end
  43. def attachments_visible?(user=User.current)
  44. (respond_to?(:visible?) ? visible?(user) : true) &&
  45. user.allowed_to?(self.class.attachable_options[:view_permission], self.project)
  46. end
  47. def attachments_editable?(user=User.current)
  48. (respond_to?(:visible?) ? visible?(user) : true) &&
  49. user.allowed_to?(self.class.attachable_options[:edit_permission], self.project)
  50. end
  51. def attachments_deletable?(user=User.current)
  52. (respond_to?(:visible?) ? visible?(user) : true) &&
  53. user.allowed_to?(self.class.attachable_options[:delete_permission], self.project)
  54. end
  55. def saved_attachments
  56. @saved_attachments ||= []
  57. end
  58. def unsaved_attachments
  59. @unsaved_attachments ||= []
  60. end
  61. def save_attachments(attachments, author=User.current)
  62. if attachments.respond_to?(:to_unsafe_hash)
  63. attachments = attachments.to_unsafe_hash
  64. end
  65. if attachments.is_a?(Hash)
  66. attachments = attachments.stringify_keys
  67. attachments = attachments.to_a.sort {|a, b|
  68. if a.first.to_i > 0 && b.first.to_i > 0
  69. a.first.to_i <=> b.first.to_i
  70. elsif a.first.to_i > 0
  71. 1
  72. elsif b.first.to_i > 0
  73. -1
  74. else
  75. a.first <=> b.first
  76. end
  77. }
  78. attachments = attachments.map(&:last)
  79. end
  80. if attachments.is_a?(Array)
  81. @failed_attachment_count = 0
  82. attachments.each do |attachment|
  83. next unless attachment.present?
  84. a = nil
  85. if file = attachment['file']
  86. a = Attachment.create(:file => file, :author => author)
  87. elsif token = attachment['token'].presence
  88. a = Attachment.find_by_token(token)
  89. unless a
  90. @failed_attachment_count += 1
  91. next
  92. end
  93. a.filename = attachment['filename'] unless attachment['filename'].blank?
  94. a.content_type = attachment['content_type'] unless attachment['content_type'].blank?
  95. end
  96. next unless a
  97. a.description = attachment['description'].to_s.strip
  98. if a.new_record? || a.invalid?
  99. unsaved_attachments << a
  100. else
  101. saved_attachments << a
  102. end
  103. end
  104. end
  105. {:files => saved_attachments, :unsaved => unsaved_attachments}
  106. end
  107. def attach_saved_attachments
  108. saved_attachments.each do |attachment|
  109. self.attachments << attachment
  110. end
  111. end
  112. def detach_saved_attachments
  113. saved_attachments.each do |attachment|
  114. attachment.reload
  115. end
  116. end
  117. def warn_about_failed_attachments
  118. if @failed_attachment_count && @failed_attachment_count > 0
  119. errors.add :base, ::I18n.t('warning_attachments_not_saved', count: @failed_attachment_count)
  120. end
  121. end
  122. module ClassMethods
  123. end
  124. end
  125. end
  126. end
  127. end