Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: module Redmine Chris@1494: module Acts Chris@1494: module Attachable Chris@1494: def self.included(base) Chris@1494: base.extend ClassMethods Chris@1494: end Chris@1494: Chris@1494: module ClassMethods Chris@1494: def acts_as_attachable(options = {}) Chris@1494: cattr_accessor :attachable_options Chris@1494: self.attachable_options = {} Chris@1494: attachable_options[:view_permission] = options.delete(:view_permission) || "view_#{self.name.pluralize.underscore}".to_sym Chris@1494: attachable_options[:delete_permission] = options.delete(:delete_permission) || "edit_#{self.name.pluralize.underscore}".to_sym Chris@1494: Chris@1494: has_many :attachments, options.merge(:as => :container, Chris@1494: :order => "#{Attachment.table_name}.created_on ASC, #{Attachment.table_name}.id ASC", Chris@1494: :dependent => :destroy) Chris@1494: send :include, Redmine::Acts::Attachable::InstanceMethods Chris@1494: before_save :attach_saved_attachments Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: module InstanceMethods Chris@1494: def self.included(base) Chris@1494: base.extend ClassMethods Chris@1494: end Chris@1494: Chris@1494: def attachments_visible?(user=User.current) Chris@1494: (respond_to?(:visible?) ? visible?(user) : true) && Chris@1494: user.allowed_to?(self.class.attachable_options[:view_permission], self.project) Chris@1494: end Chris@1494: Chris@1494: def attachments_deletable?(user=User.current) Chris@1494: (respond_to?(:visible?) ? visible?(user) : true) && Chris@1494: user.allowed_to?(self.class.attachable_options[:delete_permission], self.project) Chris@1494: end Chris@1494: Chris@1494: def saved_attachments Chris@1494: @saved_attachments ||= [] Chris@1494: end Chris@1494: Chris@1494: def unsaved_attachments Chris@1494: @unsaved_attachments ||= [] Chris@1494: end Chris@1494: Chris@1494: def save_attachments(attachments, author=User.current) Chris@1494: if attachments.is_a?(Hash) Chris@1494: attachments = attachments.stringify_keys Chris@1494: attachments = attachments.to_a.sort {|a, b| Chris@1494: if a.first.to_i > 0 && b.first.to_i > 0 Chris@1494: a.first.to_i <=> b.first.to_i Chris@1494: elsif a.first.to_i > 0 Chris@1494: 1 Chris@1494: elsif b.first.to_i > 0 Chris@1494: -1 Chris@1494: else Chris@1494: a.first <=> b.first Chris@1494: end Chris@1494: } Chris@1494: attachments = attachments.map(&:last) Chris@1494: end Chris@1494: if attachments.is_a?(Array) Chris@1494: attachments.each do |attachment| Chris@1494: next unless attachment.is_a?(Hash) Chris@1494: a = nil Chris@1494: if file = attachment['file'] Chris@1494: next unless file.size > 0 Chris@1494: a = Attachment.create(:file => file, :author => author) Chris@1494: elsif token = attachment['token'] Chris@1494: a = Attachment.find_by_token(token) Chris@1494: next unless a Chris@1494: a.filename = attachment['filename'] unless attachment['filename'].blank? Chris@1494: a.content_type = attachment['content_type'] Chris@1494: end Chris@1494: next unless a Chris@1494: a.description = attachment['description'].to_s.strip Chris@1494: if a.new_record? Chris@1494: unsaved_attachments << a Chris@1494: else Chris@1494: saved_attachments << a Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: {:files => saved_attachments, :unsaved => unsaved_attachments} Chris@1494: end Chris@1494: Chris@1494: def attach_saved_attachments Chris@1494: saved_attachments.each do |attachment| Chris@1494: self.attachments << attachment Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: module ClassMethods Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end