overview.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2012-2024 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 Overview::Assets
  9. include Overview::TriggersSubscriptions
  10. has_and_belongs_to_many :roles, after_add: :cache_update, after_remove: :cache_update, class_name: 'Role'
  11. has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update, class_name: 'User'
  12. store :condition
  13. store :order
  14. store :view
  15. validates :name, presence: true
  16. validates :roles, presence: true
  17. before_create :fill_link_on_create
  18. before_update :fill_link_on_update
  19. private
  20. def fill_link_on_create
  21. self.link = if link.present?
  22. link_name(link)
  23. else
  24. link_name(name)
  25. end
  26. true
  27. end
  28. def fill_link_on_update
  29. return true if !changes['name'] && !changes['link']
  30. self.link = if link.present?
  31. link_name(link)
  32. else
  33. link_name(name)
  34. end
  35. true
  36. end
  37. def link_name(name)
  38. local_link = name.downcase
  39. local_link = local_link.parameterize(separator: '_')
  40. local_link.gsub!(%r{\s}, '_')
  41. local_link.squeeze!('_')
  42. local_link = CGI.escape(local_link)
  43. if local_link.blank?
  44. local_link = id || SecureRandom.uuid
  45. end
  46. check = true
  47. count = 0
  48. local_lookup_link = local_link
  49. while check
  50. count += 1
  51. exists = Overview.find_by(link: local_lookup_link)
  52. if exists && exists.id != id
  53. local_lookup_link = "#{local_link}_#{count}"
  54. else
  55. check = false
  56. local_link = local_lookup_link
  57. end
  58. end
  59. local_link
  60. end
  61. end