/
20180410000001_cleanup_user_preferences_notification_sound2.rb
60 lines (50 loc) · 1.63 KB
/
20180410000001_cleanup_user_preferences_notification_sound2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class CleanupUserPreferencesNotificationSound2 < ActiveRecord::Migration[5.1]
def local_to_h!(value)
if value.class == ActionController::Parameters
value = value.permit!.to_h
end
if value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess
value.each_key do |local_key|
value[local_key] = local_to_h!(value[local_key])
end
end
value
end
def local_clear_preferences(user)
return false if !user
return false if !user.preferences
return false if !user.preferences[:notification_sound]
return false if !user.preferences[:notification_sound][:enabled]
if user.preferences[:notification_sound][:enabled] == 'true'
user.preferences[:notification_sound][:enabled] = true
user.save!
return true
end
return false if user.preferences[:notification_sound][:enabled] != 'false'
user.preferences[:notification_sound][:enabled] = false
user.save!
true
end
def up
User.with_permissions('ticket.agent').each do |user|
local_to_h!(user.preferences)
user.save!
end
items = SearchIndexBackend.search('preferences.notification_sound.enabled:*', 'User', limit: 3000) || []
items.each do |item|
next if !item[:id]
user = User.find_by(id: item[:id])
local_to_h!(user.preferences)
local_clear_preferences(user)
end
Organization.all.each do |organization|
organization.members.each do |user|
local_to_h!(user.preferences)
local_clear_preferences(user)
end
end
Delayed::Job.limit(2_000).each do |job|
Delayed::Worker.new.run(job)
end
end
end