overview.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Overview < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksLatestChangeObserved
  5. include ChecksConditionValidation
  6. include CanSeed
  7. include Overview::Assets
  8. has_and_belongs_to_many :roles, after_add: :cache_update, after_remove: :cache_update, class_name: 'Role'
  9. has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update, class_name: 'User'
  10. store :condition
  11. store :order
  12. store :view
  13. validates :name, presence: true
  14. validates :roles, presence: true
  15. before_create :fill_link_on_create, :fill_prio
  16. before_update :fill_link_on_update, :rearrangement
  17. def self.calculate_prio
  18. existing_maximum = Overview.maximum(:prio)
  19. return 0 if !existing_maximum
  20. existing_maximum + 1
  21. end
  22. private
  23. def rearrangement
  24. # rearrange only in case of changed prio
  25. return true if !changes['prio']
  26. previous_ordered_ids = self.class.all.order(
  27. prio: :asc,
  28. updated_at: :desc
  29. ).pluck(:id)
  30. rearranged_prio = 0
  31. previous_ordered_ids.each do |overview_id|
  32. # don't process currently updated overview
  33. next if id == overview_id
  34. rearranged_prio += 1
  35. # increase rearranged prio by one to avoid a collition
  36. # with the changed prio of current instance
  37. if rearranged_prio == prio
  38. rearranged_prio += 1
  39. end
  40. # don't start rearranging logic for overviews that have already been rearranged
  41. self.class.without_callback(:update, :before, :rearrangement) do
  42. # fetch and update overview only if prio needs to change
  43. overview = self.class.where(
  44. id: overview_id
  45. ).where.not(
  46. prio: rearranged_prio
  47. ).take
  48. next if overview.blank?
  49. overview.update!(prio: rearranged_prio)
  50. end
  51. end
  52. end
  53. def fill_prio
  54. return true if prio.present?
  55. self.prio = self.class.calculate_prio
  56. true
  57. end
  58. def fill_link_on_create
  59. self.link = if link.present?
  60. link_name(link)
  61. else
  62. link_name(name)
  63. end
  64. true
  65. end
  66. def fill_link_on_update
  67. return true if !changes['name'] && !changes['link']
  68. self.link = if link.present?
  69. link_name(link)
  70. else
  71. link_name(name)
  72. end
  73. true
  74. end
  75. def link_name(name)
  76. local_link = name.downcase
  77. local_link = local_link.parameterize(separator: '_')
  78. local_link.gsub!(%r{\s}, '_')
  79. local_link.squeeze!('_')
  80. local_link = CGI.escape(local_link)
  81. if local_link.blank?
  82. local_link = id || SecureRandom.uuid
  83. end
  84. check = true
  85. count = 0
  86. local_lookup_link = local_link
  87. while check
  88. count += 1
  89. exists = Overview.find_by(link: local_lookup_link)
  90. if exists && exists.id != id
  91. local_lookup_link = "#{local_link}_#{count}"
  92. else
  93. check = false
  94. local_link = local_lookup_link
  95. end
  96. end
  97. local_link
  98. end
  99. end