trabajo-afectivo/app/models/taskbar.rb

106 lines
2.4 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?
return false if !state
return false if state.empty?
state.each { |_key, value|
if value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess
value.each { |_key1, value1|
next if value1 && value1.empty?
return true
}
else
next if value && value.empty?
return true
end
}
false
end
2013-05-29 15:11:11 +00:00
2013-05-31 23:10:40 +00:00
private
def update_last_contact
return true if local_update
self.last_contact = Time.zone.now
end
def set_user
return true if local_update
self.user_id = UserInfo.current_user_id
end
def update_preferences_infos
return true if local_update
# find other same open tasks
preferences[:tasks] = []
Taskbar.where(key: key).order(:created_at).each { |taskbar|
changed = if taskbar.id == id
state_changed?
else
taskbar.state_changed?
end
task = {
id: taskbar.id,
user_id: taskbar.user_id,
last_contact: taskbar.last_contact,
changed: changed,
}
preferences[:tasks].push task
}
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).each { |taskbar|
next if taskbar.id == id
taskbar.preferences = preferences
taskbar.local_update = true
taskbar.save!
}
return true if destroyed?
# remember preferences for current taskbar
self.preferences = preferences
true
end
def notify_clients
return true if !changes['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