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