trabajo-afectivo/app/models/taskbar.rb

161 lines
3.6 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2013-05-29 15:11:11 +00:00
class Taskbar < ApplicationModel
2013-05-31 23:10:40 +00:00
store :state
store :params
store :preferences
before_create :update_last_contact, :set_user, :update_preferences_infos
before_update :update_last_contact, :set_user, :update_preferences_infos
after_update :notify_clients
after_destroy :update_preferences_infos, :notify_clients
attr_accessor :local_update
def state_changed?
2017-09-08 08:28:34 +00:00
return false if state.blank?
2017-11-23 08:09:44 +00:00
state.each_value do |value|
2017-09-08 08:28:34 +00:00
if value.is_a? Hash
value.each do |key1, value1|
2017-09-08 08:28:34 +00:00
next if value1.blank?
next if key1 == 'form_id'
return true
end
else
2017-09-08 08:28:34 +00:00
next if value.blank?
return true
end
end
false
end
2013-05-29 15:11:11 +00:00
def attributes_with_association_names
add_attachments_to_attributes(super)
end
def attributes_with_association_ids
add_attachments_to_attributes(super)
end
def as_json(options = {})
add_attachments_to_attributes(super)
end
# form_id is saved directly in a new ticket, but inside of the article when updating an existing ticket
def persisted_form_id
state&.dig(:form_id) || state&.dig(:article, :form_id)
end
2013-05-31 23:10:40 +00:00
private
def attachments
return [] if persisted_form_id.blank?
Store.list(object: 'UploadCache', o_id: persisted_form_id)
end
def add_attachments_to_attributes(attributes)
attributes.tap do |result|
result['attachments'] = attachments.map(&:attributes_for_display)
end
end
def update_last_contact
return true if local_update
2017-11-23 08:09:44 +00:00
return true if changes.blank?
if changes['notify']
count = 0
2017-11-23 08:09:44 +00:00
changes.each_key do |attribute|
next if attribute == 'updated_at'
next if attribute == 'created_at'
count += 1
end
return true if count <= 1
end
self.last_contact = Time.zone.now
end
def set_user
return true if local_update
return true if !UserInfo.current_user_id
self.user_id = UserInfo.current_user_id
end
def update_preferences_infos
return true if local_update
# find other same open tasks
if !preferences
self.preferences = {}
end
preferences[:tasks] = []
Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
2017-01-09 12:22:07 +00:00
if taskbar.id == id
local_changed = state_changed?
local_last_contact = last_contact
else
local_changed = taskbar.state_changed?
local_last_contact = taskbar.last_contact
end
task = {
id: taskbar.id,
user_id: taskbar.user_id,
2017-01-09 12:22:07 +00:00
last_contact: local_last_contact,
changed: local_changed,
}
preferences[:tasks].push task
end
if !id
changed = state_changed?
task = {
user_id: user_id,
last_contact: last_contact,
changed: changed,
}
preferences[:tasks].push task
end
# update other taskbars
Taskbar.where(key: key).order(:created_at, :id).each do |taskbar|
next if taskbar.id == id
2017-08-22 17:36:48 +00:00
taskbar.with_lock do
taskbar.preferences = preferences
taskbar.local_update = true
taskbar.save!
end
end
return true if destroyed?
# remember preferences for current taskbar
self.preferences = preferences
true
end
def notify_clients
2017-09-23 06:25:55 +00:00
return true if !saved_change_to_attribute?('preferences')
data = {
event: 'taskbar:preferences',
data: {
id: id,
key: key,
preferences: preferences,
},
}
PushMessages.send_to(
user_id,
data,
)
end
2013-05-31 23:10:40 +00:00
end