overview.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Overview < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksLatestChangeObserved
  5. include ChecksConditionValidation
  6. include CanSeed
  7. load 'overview/assets.rb'
  8. include Overview::Assets
  9. has_and_belongs_to_many :roles, after_add: :cache_update, after_remove: :cache_update, class_name: 'Role'
  10. has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update
  11. store :condition
  12. store :order
  13. store :view
  14. validates :name, presence: true
  15. before_create :fill_link_on_create, :fill_prio
  16. before_update :fill_link_on_update
  17. private
  18. def fill_prio
  19. return true if prio
  20. self.prio = 9999
  21. true
  22. end
  23. def fill_link_on_create
  24. return true if link.present?
  25. self.link = link_name(name)
  26. true
  27. end
  28. def fill_link_on_update
  29. return true if !changes['name']
  30. return true if changes['link']
  31. self.link = link_name(name)
  32. true
  33. end
  34. def link_name(name)
  35. local_link = name.downcase
  36. local_link = local_link.parameterize(separator: '_')
  37. local_link.gsub!(/\s/, '_')
  38. local_link.gsub!(/_+/, '_')
  39. local_link = URI.escape(local_link)
  40. if local_link.blank?
  41. local_link = id || rand(999)
  42. end
  43. check = true
  44. while check
  45. exists = Overview.find_by(link: local_link)
  46. if exists && exists.id != id
  47. local_link = "#{local_link}_#{rand(999)}"
  48. else
  49. check = false
  50. end
  51. end
  52. local_link
  53. end
  54. end