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