overview.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. class Overview < ApplicationModel
  3. include HasDefaultModelUserRelations
  4. include ChecksClientNotification
  5. include ChecksConditionValidation
  6. include CanSeed
  7. include CanPriorization
  8. include HasSearchIndexBackend
  9. include CanSelector
  10. include CanSearch
  11. include Overview::Assets
  12. include Overview::TriggersSubscriptions
  13. has_and_belongs_to_many :roles, after_add: :cache_update, after_remove: :cache_update, class_name: 'Role'
  14. has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update, class_name: 'User'
  15. store :condition
  16. store :order
  17. store :view
  18. validates :name, presence: true
  19. validates :roles, presence: true
  20. has_many :overview_sortings, class_name: 'User::OverviewSorting', dependent: :destroy
  21. association_attributes_ignored :overview_sortings
  22. before_create :fill_link_on_create
  23. before_update :fill_link_on_update
  24. private
  25. def fill_link_on_create
  26. self.link = if link.present?
  27. link_name(link)
  28. else
  29. link_name(name)
  30. end
  31. true
  32. end
  33. def fill_link_on_update
  34. return true if !changes['name'] && !changes['link']
  35. self.link = if link.present?
  36. link_name(link)
  37. else
  38. link_name(name)
  39. end
  40. true
  41. end
  42. def link_name(name)
  43. local_link = name.downcase
  44. local_link = local_link.parameterize(separator: '_')
  45. local_link.gsub!(%r{\s}, '_')
  46. local_link.squeeze!('_')
  47. local_link = CGI.escape(local_link)
  48. if local_link.blank?
  49. local_link = id || SecureRandom.uuid
  50. end
  51. check = true
  52. count = 0
  53. local_lookup_link = local_link
  54. while check
  55. count += 1
  56. exists = Overview.find_by(link: local_lookup_link)
  57. if exists && exists.id != id
  58. local_lookup_link = "#{local_link}_#{count}"
  59. else
  60. check = false
  61. local_link = local_lookup_link
  62. end
  63. end
  64. local_link
  65. end
  66. end