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_field_enumeration.rb 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 CustomFieldEnumeration < ActiveRecord::Base
  18. belongs_to :custom_field
  19. validates_presence_of :name, :position, :custom_field_id
  20. validates_length_of :name, :maximum => 60
  21. validates_numericality_of :position, :only_integer => true
  22. before_create :set_position
  23. scope :active, lambda { where(:active => true) }
  24. def to_s
  25. name.to_s
  26. end
  27. def objects_count
  28. custom_values.count
  29. end
  30. def in_use?
  31. objects_count > 0
  32. end
  33. alias :destroy_without_reassign :destroy
  34. def destroy(reassign_to=nil)
  35. if reassign_to
  36. custom_values.update_all(:value => reassign_to.id.to_s)
  37. end
  38. destroy_without_reassign
  39. end
  40. def custom_values
  41. custom_field.custom_values.where(:value => id.to_s)
  42. end
  43. def self.update_each(custom_field, attributes)
  44. transaction do
  45. attributes.each do |enumeration_id, enumeration_attributes|
  46. enumeration = custom_field.enumerations.find_by_id(enumeration_id)
  47. if enumeration
  48. if block_given?
  49. yield enumeration, enumeration_attributes
  50. else
  51. enumeration.attributes = enumeration_attributes
  52. end
  53. unless enumeration.save
  54. raise ActiveRecord::Rollback
  55. end
  56. end
  57. end
  58. end
  59. end
  60. def self.fields_for_order_statement(table=nil)
  61. table ||= table_name
  62. columns = ['position']
  63. columns.uniq.map {|field| "#{table}.#{field}"}
  64. end
  65. private
  66. def set_position
  67. max = self.class.where(:custom_field_id => custom_field_id).maximum(:position) || 0
  68. self.position = max + 1
  69. end
  70. end