You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

acts_as_attachable.rb 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2008 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. module Redmine
  18. module Acts
  19. module Attachable
  20. def self.included(base)
  21. base.extend ClassMethods
  22. end
  23. module ClassMethods
  24. def acts_as_attachable(options = {})
  25. cattr_accessor :attachable_options
  26. self.attachable_options = {}
  27. attachable_options[:view_permission] = options.delete(:view_permission) || "view_#{self.name.pluralize.underscore}".to_sym
  28. attachable_options[:delete_permission] = options.delete(:delete_permission) || "edit_#{self.name.pluralize.underscore}".to_sym
  29. has_many :attachments, options.merge(:as => :container,
  30. :order => "#{Attachment.table_name}.created_on",
  31. :dependent => :destroy)
  32. send :include, Redmine::Acts::Attachable::InstanceMethods
  33. end
  34. end
  35. module InstanceMethods
  36. def self.included(base)
  37. base.extend ClassMethods
  38. end
  39. def attachments_visible?(user=User.current)
  40. user.allowed_to?(self.class.attachable_options[:view_permission], self.project)
  41. end
  42. def attachments_deletable?(user=User.current)
  43. user.allowed_to?(self.class.attachable_options[:delete_permission], self.project)
  44. end
  45. module ClassMethods
  46. end
  47. end
  48. end
  49. end
  50. end