overview.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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, :fill_prio
  16. before_update :fill_link_on_update, :rearrangement
  17. private
  18. def rearrangement
  19. # rearrange only in case of changed prio
  20. return true if !changes['prio']
  21. previous_ordered_ids = self.class.all.order(
  22. prio: :asc,
  23. updated_at: :desc
  24. ).pluck(:id)
  25. rearranged_prio = 0
  26. previous_ordered_ids.each do |overview_id|
  27. # don't process currently updated overview
  28. next if id == overview_id
  29. rearranged_prio += 1
  30. # increase rearranged prio by one to avoid a collition
  31. # with the changed prio of current instance
  32. if rearranged_prio == prio
  33. rearranged_prio += 1
  34. end
  35. # don't start rearrange logic for overviews that alredy get rearranged
  36. self.class.without_callback(:update, :before, :rearrangement) do
  37. # fetch and update overview only if prio needs to change
  38. overview = self.class.where(
  39. id: overview_id
  40. ).where.not(
  41. prio: rearranged_prio
  42. ).take
  43. next if overview.blank?
  44. overview.update!(prio: rearranged_prio)
  45. end
  46. end
  47. end
  48. def fill_prio
  49. return true if prio.present?
  50. self.prio = Overview.count + 1
  51. true
  52. end
  53. def fill_link_on_create
  54. self.link = if link.present?
  55. link_name(link)
  56. else
  57. link_name(name)
  58. end
  59. true
  60. end
  61. def fill_link_on_update
  62. return true if !changes['name'] && !changes['link']
  63. self.link = if link.present?
  64. link_name(link)
  65. else
  66. link_name(name)
  67. end
  68. true
  69. end
  70. def link_name(name)
  71. local_link = name.downcase
  72. local_link = local_link.parameterize(separator: '_')
  73. local_link.gsub!(/\s/, '_')
  74. local_link.gsub!(/_+/, '_')
  75. local_link = CGI.escape(local_link)
  76. if local_link.blank?
  77. local_link = id || rand(999)
  78. end
  79. check = true
  80. count = 0
  81. local_lookup_link = local_link
  82. while check
  83. count += 1
  84. exists = Overview.find_by(link: local_lookup_link)
  85. if exists && exists.id != id
  86. local_lookup_link = "#{local_link}_#{count}"
  87. else
  88. check = false
  89. local_link = local_lookup_link
  90. end
  91. end
  92. local_link
  93. end
  94. end