overview.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. has_many :overview_sortings, class_name: 'User::OverviewSorting', dependent: :destroy
  18. association_attributes_ignored :overview_sortings
  19. before_create :fill_link_on_create
  20. before_update :fill_link_on_update
  21. private
  22. def fill_link_on_create
  23. self.link = if link.present?
  24. link_name(link)
  25. else
  26. link_name(name)
  27. end
  28. true
  29. end
  30. def fill_link_on_update
  31. return true if !changes['name'] && !changes['link']
  32. self.link = if link.present?
  33. link_name(link)
  34. else
  35. link_name(name)
  36. end
  37. true
  38. end
  39. def link_name(name)
  40. local_link = name.downcase
  41. local_link = local_link.parameterize(separator: '_')
  42. local_link.gsub!(%r{\s}, '_')
  43. local_link.squeeze!('_')
  44. local_link = CGI.escape(local_link)
  45. if local_link.blank?
  46. local_link = id || SecureRandom.uuid
  47. end
  48. check = true
  49. count = 0
  50. local_lookup_link = local_link
  51. while check
  52. count += 1
  53. exists = Overview.find_by(link: local_lookup_link)
  54. if exists && exists.id != id
  55. local_lookup_link = "#{local_link}_#{count}"
  56. else
  57. check = false
  58. local_link = local_lookup_link
  59. end
  60. end
  61. local_link
  62. end
  63. end