annotate .svn/pristine/01/01df356dd5562f68f76338bddccfd73e2d039ee5.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 module Redmine
Chris@1295 19 module Acts
Chris@1295 20 module Attachable
Chris@1295 21 def self.included(base)
Chris@1295 22 base.extend ClassMethods
Chris@1295 23 end
Chris@1295 24
Chris@1295 25 module ClassMethods
Chris@1295 26 def acts_as_attachable(options = {})
Chris@1295 27 cattr_accessor :attachable_options
Chris@1295 28 self.attachable_options = {}
Chris@1295 29 attachable_options[:view_permission] = options.delete(:view_permission) || "view_#{self.name.pluralize.underscore}".to_sym
Chris@1295 30 attachable_options[:delete_permission] = options.delete(:delete_permission) || "edit_#{self.name.pluralize.underscore}".to_sym
Chris@1295 31
Chris@1295 32 has_many :attachments, options.merge(:as => :container,
Chris@1295 33 :order => "#{Attachment.table_name}.created_on ASC, #{Attachment.table_name}.id ASC",
Chris@1295 34 :dependent => :destroy)
Chris@1295 35 send :include, Redmine::Acts::Attachable::InstanceMethods
Chris@1295 36 before_save :attach_saved_attachments
Chris@1295 37 end
Chris@1295 38 end
Chris@1295 39
Chris@1295 40 module InstanceMethods
Chris@1295 41 def self.included(base)
Chris@1295 42 base.extend ClassMethods
Chris@1295 43 end
Chris@1295 44
Chris@1295 45 def attachments_visible?(user=User.current)
Chris@1295 46 (respond_to?(:visible?) ? visible?(user) : true) &&
Chris@1295 47 user.allowed_to?(self.class.attachable_options[:view_permission], self.project)
Chris@1295 48 end
Chris@1295 49
Chris@1295 50 def attachments_deletable?(user=User.current)
Chris@1295 51 (respond_to?(:visible?) ? visible?(user) : true) &&
Chris@1295 52 user.allowed_to?(self.class.attachable_options[:delete_permission], self.project)
Chris@1295 53 end
Chris@1295 54
Chris@1295 55 def saved_attachments
Chris@1295 56 @saved_attachments ||= []
Chris@1295 57 end
Chris@1295 58
Chris@1295 59 def unsaved_attachments
Chris@1295 60 @unsaved_attachments ||= []
Chris@1295 61 end
Chris@1295 62
Chris@1295 63 def save_attachments(attachments, author=User.current)
Chris@1295 64 if attachments.is_a?(Hash)
Chris@1295 65 attachments = attachments.stringify_keys
Chris@1295 66 attachments = attachments.to_a.sort {|a, b|
Chris@1295 67 if a.first.to_i > 0 && b.first.to_i > 0
Chris@1295 68 a.first.to_i <=> b.first.to_i
Chris@1295 69 elsif a.first.to_i > 0
Chris@1295 70 1
Chris@1295 71 elsif b.first.to_i > 0
Chris@1295 72 -1
Chris@1295 73 else
Chris@1295 74 a.first <=> b.first
Chris@1295 75 end
Chris@1295 76 }
Chris@1295 77 attachments = attachments.map(&:last)
Chris@1295 78 end
Chris@1295 79 if attachments.is_a?(Array)
Chris@1295 80 attachments.each do |attachment|
Chris@1295 81 a = nil
Chris@1295 82 if file = attachment['file']
Chris@1295 83 next unless file.size > 0
Chris@1295 84 a = Attachment.create(:file => file, :author => author)
Chris@1295 85 elsif token = attachment['token']
Chris@1295 86 a = Attachment.find_by_token(token)
Chris@1295 87 next unless a
Chris@1295 88 a.filename = attachment['filename'] unless attachment['filename'].blank?
Chris@1295 89 a.content_type = attachment['content_type']
Chris@1295 90 end
Chris@1295 91 next unless a
Chris@1295 92 a.description = attachment['description'].to_s.strip
Chris@1295 93 if a.new_record?
Chris@1295 94 unsaved_attachments << a
Chris@1295 95 else
Chris@1295 96 saved_attachments << a
Chris@1295 97 end
Chris@1295 98 end
Chris@1295 99 end
Chris@1295 100 {:files => saved_attachments, :unsaved => unsaved_attachments}
Chris@1295 101 end
Chris@1295 102
Chris@1295 103 def attach_saved_attachments
Chris@1295 104 saved_attachments.each do |attachment|
Chris@1295 105 self.attachments << attachment
Chris@1295 106 end
Chris@1295 107 end
Chris@1295 108
Chris@1295 109 module ClassMethods
Chris@1295 110 end
Chris@1295 111 end
Chris@1295 112 end
Chris@1295 113 end
Chris@1295 114 end