Newer
Older
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
61
62
63
64
65
66
67
68
# == Schema Information
#
# Table name: communication_blocks
#
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# data :jsonb
# position :integer default(0), not null
# template :integer default(NULL), not null
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid indexed => [about_type]
# university_id :uuid not null, indexed
#
# Indexes
#
# index_communication_blocks_on_university_id (university_id)
# index_communication_website_blocks_on_about (about_type,about_id)
#
# Foreign Keys
#
# fk_rails_18291ef65f (university_id => universities.id)
#
class Communication::Block < ApplicationRecord
include WithPosition
belongs_to :university
belongs_to :about, polymorphic: true
enum template: {
organization_chart: 100,
partners: 200
}
def data=(value)
value = JSON.parse value if value.is_a? String
super(value)
end
def git_dependencies
m = "git_dependencies_for_#{template}"
respond_to?(m, true) ? send(m) : []
end
def last_ordered_element
about.blocks.ordered.last
end
def to_s
"Bloc #{position}"
end
protected
def git_dependencies_for_organization_chart
dependencies = []
data['elements'].each do |element|
element['persons'].each do |person|
id = person['id']
next if id.blank?
person = university.people.find id
next if person.nil?
dependencies += [person]
dependencies += person.active_storage_blobs
end
end
dependencies.uniq
end
def git_dependencies_for_partners
dependencies = []
data['elements'].each do |element|
element['partners'].each do |partner|
id = partner.dig('logo', 'id')
next if id.blank?
blob = university.active_storage_blobs.find id
next if blob.nil?
dependencies += [blob]
end
end
dependencies.uniq
end