2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
class Overview < ApplicationModel
|
2017-05-02 15:21:13 +00:00
|
|
|
include ChecksClientNotification
|
|
|
|
include ChecksLatestChangeObserved
|
|
|
|
include ChecksConditionValidation
|
|
|
|
include CanSeed
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2016-03-20 19:09:52 +00:00
|
|
|
include Overview::Assets
|
|
|
|
|
2017-04-19 19:54:04 +00:00
|
|
|
has_and_belongs_to_many :roles, after_add: :cache_update, after_remove: :cache_update, class_name: 'Role'
|
2018-01-09 07:39:50 +00:00
|
|
|
has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update, class_name: 'User'
|
2012-04-10 14:06:46 +00:00
|
|
|
store :condition
|
|
|
|
store :order
|
|
|
|
store :view
|
2015-04-27 13:42:53 +00:00
|
|
|
validates :name, presence: true
|
2018-01-09 07:39:50 +00:00
|
|
|
validates :roles, presence: true
|
2015-09-17 18:39:51 +00:00
|
|
|
|
2016-03-12 16:23:51 +00:00
|
|
|
before_create :fill_link_on_create, :fill_prio
|
2017-11-27 10:50:57 +00:00
|
|
|
before_update :fill_link_on_update, :rearrangement
|
2015-09-17 18:39:51 +00:00
|
|
|
|
2021-04-02 05:48:57 +00:00
|
|
|
def self.calculate_prio
|
|
|
|
existing_maximum = Overview.maximum(:prio)
|
|
|
|
|
|
|
|
return 0 if !existing_maximum
|
|
|
|
|
|
|
|
existing_maximum + 1
|
|
|
|
end
|
|
|
|
|
2015-09-17 18:39:51 +00:00
|
|
|
private
|
|
|
|
|
2017-11-27 10:50:57 +00:00
|
|
|
def rearrangement
|
2018-06-22 12:57:25 +00:00
|
|
|
# rearrange only in case of changed prio
|
2017-11-27 10:50:57 +00:00
|
|
|
return true if !changes['prio']
|
2018-06-22 12:57:25 +00:00
|
|
|
|
|
|
|
previous_ordered_ids = self.class.all.order(
|
|
|
|
prio: :asc,
|
|
|
|
updated_at: :desc
|
|
|
|
).pluck(:id)
|
|
|
|
|
|
|
|
rearranged_prio = 0
|
|
|
|
previous_ordered_ids.each do |overview_id|
|
|
|
|
|
|
|
|
# don't process currently updated overview
|
2017-11-27 10:50:57 +00:00
|
|
|
next if id == overview_id
|
2018-06-22 12:57:25 +00:00
|
|
|
|
|
|
|
rearranged_prio += 1
|
|
|
|
|
|
|
|
# increase rearranged prio by one to avoid a collition
|
|
|
|
# with the changed prio of current instance
|
|
|
|
if rearranged_prio == prio
|
|
|
|
rearranged_prio += 1
|
|
|
|
end
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# don't start rearranging logic for overviews that have already been rearranged
|
2018-06-22 12:57:25 +00:00
|
|
|
self.class.without_callback(:update, :before, :rearrangement) do
|
|
|
|
# fetch and update overview only if prio needs to change
|
|
|
|
overview = self.class.where(
|
|
|
|
id: overview_id
|
|
|
|
).where.not(
|
|
|
|
prio: rearranged_prio
|
|
|
|
).take
|
|
|
|
|
|
|
|
next if overview.blank?
|
|
|
|
|
|
|
|
overview.update!(prio: rearranged_prio)
|
2017-11-27 10:50:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-12 16:23:51 +00:00
|
|
|
def fill_prio
|
2017-11-27 10:50:57 +00:00
|
|
|
return true if prio.present?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-04-02 05:48:57 +00:00
|
|
|
self.prio = self.class.calculate_prio
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2016-03-12 16:23:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def fill_link_on_create
|
2017-11-27 10:50:57 +00:00
|
|
|
self.link = if link.present?
|
|
|
|
link_name(link)
|
|
|
|
else
|
|
|
|
link_name(name)
|
|
|
|
end
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2016-03-12 16:23:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def fill_link_on_update
|
2017-11-27 10:50:57 +00:00
|
|
|
return true if !changes['name'] && !changes['link']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-11-27 10:50:57 +00:00
|
|
|
self.link = if link.present?
|
|
|
|
link_name(link)
|
|
|
|
else
|
|
|
|
link_name(name)
|
|
|
|
end
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2016-03-12 16:23:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_name(name)
|
2017-07-14 13:47:10 +00:00
|
|
|
local_link = name.downcase
|
2017-09-08 08:28:34 +00:00
|
|
|
local_link = local_link.parameterize(separator: '_')
|
2021-05-12 11:37:44 +00:00
|
|
|
local_link.gsub!(%r{\s}, '_')
|
2020-07-07 06:30:20 +00:00
|
|
|
local_link.squeeze!('_')
|
2017-11-23 08:09:44 +00:00
|
|
|
local_link = CGI.escape(local_link)
|
2017-07-14 13:47:10 +00:00
|
|
|
if local_link.blank?
|
2021-09-20 10:47:05 +00:00
|
|
|
local_link = id || SecureRandom.uuid
|
2017-07-14 13:47:10 +00:00
|
|
|
end
|
|
|
|
check = true
|
2017-11-27 10:50:57 +00:00
|
|
|
count = 0
|
|
|
|
local_lookup_link = local_link
|
2017-07-14 13:47:10 +00:00
|
|
|
while check
|
2017-11-27 10:50:57 +00:00
|
|
|
count += 1
|
|
|
|
exists = Overview.find_by(link: local_lookup_link)
|
2018-04-12 14:57:37 +00:00
|
|
|
if exists && exists.id != id
|
2017-11-27 10:50:57 +00:00
|
|
|
local_lookup_link = "#{local_link}_#{count}"
|
2017-07-14 13:47:10 +00:00
|
|
|
else
|
|
|
|
check = false
|
2017-11-27 10:50:57 +00:00
|
|
|
local_link = local_lookup_link
|
2017-07-14 13:47:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
local_link
|
2015-09-17 18:39:51 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|