2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 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
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-11-27 10:50:57 +00:00
|
|
|
def rearrangement
|
|
|
|
return true if !changes['prio']
|
|
|
|
prio = 0
|
|
|
|
Overview.all.order(prio: :asc, updated_at: :desc).pluck(:id).each do |overview_id|
|
|
|
|
prio += 1
|
|
|
|
next if id == overview_id
|
|
|
|
Overview.without_callback(:update, :before, :rearrangement) do
|
|
|
|
overview = Overview.find(overview_id)
|
|
|
|
next if overview.prio == prio
|
|
|
|
overview.prio = prio
|
|
|
|
overview.save!
|
|
|
|
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?
|
|
|
|
self.prio = Overview.count + 1
|
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']
|
|
|
|
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: '_')
|
2017-07-14 13:47:10 +00:00
|
|
|
local_link.gsub!(/\s/, '_')
|
|
|
|
local_link.gsub!(/_+/, '_')
|
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?
|
|
|
|
local_link = id || rand(999)
|
|
|
|
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
|