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.

custom_value.rb 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. class CustomValue < ActiveRecord::Base
  18. belongs_to :custom_field
  19. belongs_to :customized, :polymorphic => true
  20. after_save :custom_field_after_save_custom_value
  21. def initialize(attributes=nil, *args)
  22. super
  23. if new_record? && custom_field && !attributes.key?(:value) && (customized.nil? || customized.set_custom_field_default?(self))
  24. self.value ||= custom_field.default_value
  25. end
  26. end
  27. # Returns true if the boolean custom value is true
  28. def true?
  29. self.value == '1'
  30. end
  31. def editable?
  32. custom_field.editable?
  33. end
  34. def visible?(user=User.current)
  35. if custom_field.visible?
  36. true
  37. elsif customized.respond_to?(:project)
  38. custom_field.visible_by?(customized.project, user)
  39. else
  40. false
  41. end
  42. end
  43. def attachments_visible?(user)
  44. visible?(user) && customized && customized.visible?(user)
  45. end
  46. def required?
  47. custom_field.is_required?
  48. end
  49. def to_s
  50. value.to_s
  51. end
  52. private
  53. def custom_field_after_save_custom_value
  54. custom_field.after_save_custom_value(self)
  55. end
  56. end