From 743d3b6bacd37917db1121666b04b6a9b907149e Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Mon, 28 Feb 2022 17:42:12 +0100 Subject: [PATCH] nice refactor of block dependencies --- app/models/communication/block/abstract.rb | 18 +++++++++++++++++- app/models/communication/block/gallery.rb | 9 +++------ .../communication/block/organization_chart.rb | 8 +++----- app/models/communication/block/partner.rb | 11 +++-------- app/models/communication/block/testimonial.rb | 7 ++----- 5 files changed, 28 insertions(+), 25 deletions(-) diff --git a/app/models/communication/block/abstract.rb b/app/models/communication/block/abstract.rb index 8e78e8dfe..27d7e6f47 100644 --- a/app/models/communication/block/abstract.rb +++ b/app/models/communication/block/abstract.rb @@ -6,11 +6,27 @@ class Communication::Block::Abstract end def git_dependencies - [] + unless @git_dependencies + @git_dependencies = [] + build_git_dependencies + @git_dependencies.uniq! + end + @git_dependencies end protected + def build_git_dependencies + end + + def add_dependency(dependency) + if dependency.is_a? Array + @git_dependencies += dependency + else + @git_dependencies += [dependency] + end + end + def find_blob(object, key) id = object.dig(key, 'id') return if id.blank? diff --git a/app/models/communication/block/gallery.rb b/app/models/communication/block/gallery.rb index f43d7ee2f..be03ef820 100644 --- a/app/models/communication/block/gallery.rb +++ b/app/models/communication/block/gallery.rb @@ -1,12 +1,9 @@ class Communication::Block::Gallery < Communication::Block::Abstract - def git_dependencies + def build_git_dependencies dependencies = [] elements.each do |image| - id = image.dig('file', 'id') - next if id.blank? - blob = university.active_storage_blobs.find id - next if blob.nil? - dependencies += [blob] + blob = find_blob image, 'file' + add_dependency blob unless blob.nil? end dependencies.uniq end diff --git a/app/models/communication/block/organization_chart.rb b/app/models/communication/block/organization_chart.rb index 78c76ca61..9e5859b2c 100644 --- a/app/models/communication/block/organization_chart.rb +++ b/app/models/communication/block/organization_chart.rb @@ -1,14 +1,12 @@ class Communication::Block::OrganizationChart < Communication::Block::Abstract - def git_dependencies - dependencies = [] + def build_git_dependencies elements.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 + add_dependency person + add_dependency person.active_storage_blobs end - dependencies.uniq end end diff --git a/app/models/communication/block/partner.rb b/app/models/communication/block/partner.rb index 539a41f47..c54cfbe06 100644 --- a/app/models/communication/block/partner.rb +++ b/app/models/communication/block/partner.rb @@ -1,13 +1,8 @@ class Communication::Block::Partner < Communication::Block::Abstract - def git_dependencies - dependencies = [] + def build_git_dependencies elements.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] + blob = find_blob partner, 'logo' + add_dependency blob unless blob.nil? end - dependencies.uniq end end diff --git a/app/models/communication/block/testimonial.rb b/app/models/communication/block/testimonial.rb index 7b1ce22be..797d11954 100644 --- a/app/models/communication/block/testimonial.rb +++ b/app/models/communication/block/testimonial.rb @@ -1,11 +1,8 @@ class Communication::Block::Testimonial < Communication::Block::Abstract - def git_dependencies - dependencies = [] + def build_git_dependencies elements.each do |testimonial| blob = find_blob testimonial, 'photo' - next if blob.nil? - dependencies += [blob] + add_dependency blob unless blob.nil? end - dependencies.uniq end end -- GitLab