annotate .svn/pristine/ba/ba42eee3e77a50e83df68b00fbb077c6b731d95a.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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