annotate lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb @ 1284:3ce07a57ce68 redmine-2.2-integration

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