overview.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Overview < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksConditionValidation
  5. include CanSeed
  6. include CanPriorization
  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
  16. before_update :fill_link_on_update
  17. private
  18. def fill_link_on_create
  19. self.link = if link.present?
  20. link_name(link)
  21. else
  22. link_name(name)
  23. end
  24. true
  25. end
  26. def fill_link_on_update
  27. return true if !changes['name'] && !changes['link']
  28. self.link = if link.present?
  29. link_name(link)
  30. else
  31. link_name(name)
  32. end
  33. true
  34. end
  35. def link_name(name)
  36. local_link = name.downcase
  37. local_link = local_link.parameterize(separator: '_')
  38. local_link.gsub!(%r{\s}, '_')
  39. local_link.squeeze!('_')
  40. local_link = CGI.escape(local_link)
  41. if local_link.blank?
  42. local_link = id || SecureRandom.uuid
  43. end
  44. check = true
  45. count = 0
  46. local_lookup_link = local_link
  47. while check
  48. count += 1
  49. exists = Overview.find_by(link: local_lookup_link)
  50. if exists && exists.id != id
  51. local_lookup_link = "#{local_link}_#{count}"
  52. else
  53. check = false
  54. local_link = local_lookup_link
  55. end
  56. end
  57. local_link
  58. end
  59. end