overview.rb 2.0 KB

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