|
1
|
1 |
|
module Textris |
|
2
|
1 |
|
module Delay |
|
3
|
1 |
|
module Sidekiq |
|
4
|
1 |
|
module Serializer |
|
5
|
1 |
|
ACTIVERECORD_POINTER = 'Textris::ActiveRecordPointer' |
|
6
|
1 |
|
ACTIVERECORD_ARRAY_POINTER = 'Textris::ActiveRecordArrayPointer' |
|
7
|
|
|
|
|
8
|
1 |
|
class << self |
|
9
|
1 |
|
def serialize(objects) |
|
10
|
8 |
|
objects.collect do |object| |
|
11
|
|
|
serialize_active_record_object(object) || |
|
12
|
11 |
|
serialize_active_record_array(object) || |
|
13
|
|
|
serialize_active_record_relation(object) || |
|
14
|
|
|
object |
|
15
|
|
|
end |
|
16
|
|
|
rescue NameError |
|
17
|
1 |
|
objects |
|
18
|
|
|
end |
|
19
|
|
|
|
|
20
|
1 |
|
def deserialize(objects) |
|
21
|
8 |
|
objects.collect do |object| |
|
22
|
|
|
deserialize_active_record_object(object) || |
|
23
|
11 |
|
object |
|
24
|
|
|
end |
|
25
|
|
|
end |
|
26
|
|
|
|
|
27
|
1 |
|
private |
|
28
|
|
|
|
|
29
|
1 |
|
def serialize_active_record_object(object) |
|
30
|
11 |
|
if object.class < ActiveRecord::Base && object.id.present? |
|
31
|
1 |
|
[ACTIVERECORD_POINTER, object.class.to_s, object.id] |
|
32
|
|
|
end |
|
33
|
|
|
end |
|
34
|
|
|
|
|
35
|
1 |
|
def serialize_active_record_relation(array) |
|
36
|
8 |
|
if array.class < ActiveRecord::Relation |
|
37
|
1 |
|
[ACTIVERECORD_ARRAY_POINTER, array.model.to_s, array.map(&:id)] |
|
38
|
|
|
end |
|
39
|
|
|
end |
|
40
|
|
|
|
|
41
|
1 |
|
def serialize_active_record_array(array) |
|
42
|
|
|
if array.is_a?(Array) && |
|
43
|
9 |
|
(model = get_active_record_common_model(array)) |
|
44
|
1 |
|
[ACTIVERECORD_ARRAY_POINTER, model, array.map(&:id)] |
|
45
|
|
|
end |
|
46
|
|
|
end |
|
47
|
|
|
|
|
48
|
1 |
|
def deserialize_active_record_object(object) |
|
49
|
|
|
if object.is_a?(Array) && |
|
50
|
11 |
|
object.try(:length) == 3 && |
|
51
|
|
|
[ACTIVERECORD_POINTER, |
|
52
|
|
|
ACTIVERECORD_ARRAY_POINTER].include?(object[0]) |
|
53
|
3 |
|
object[1].constantize.find(object[2]) |
|
54
|
|
|
end |
|
55
|
|
|
end |
|
56
|
|
|
|
|
57
|
1 |
|
def get_active_record_common_model(items) |
|
58
|
2 |
|
items = items.collect do |item| |
|
59
|
4 |
|
if item.class < ActiveRecord::Base |
|
60
|
4 |
|
item.class.to_s |
|
61
|
|
|
end |
|
62
|
|
|
end.uniq |
|
63
|
|
|
|
|
64
|
2 |
|
if items.size == 1 |
|
65
|
1 |
|
items.first |
|
66
|
|
|
end |
|
67
|
|
|
end |
|
68
|
|
|
end |
|
69
|
|
|
end |
|
70
|
|
|
end |
|
71
|
|
|
end |
|
72
|
|
|
end |
|
73
|
|
|
|