From 7acd21bb9a269ade36e6a357722c204cc7f3c3a3 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Wed, 13 Apr 2022 23:25:00 +0200 Subject: [PATCH] refactor --- .../communication/block/template/gallery.rb | 10 +--- .../block/template/organization_chart.rb | 23 ++++---- .../communication/block/template/partner.rb | 57 +++++++------------ .../communication/block/template/post.rb | 8 +-- .../block/template/testimonial.rb | 22 ++++--- 5 files changed, 47 insertions(+), 73 deletions(-) diff --git a/app/models/communication/block/template/gallery.rb b/app/models/communication/block/template/gallery.rb index d0c87d2ce..f8e0032fd 100644 --- a/app/models/communication/block/template/gallery.rb +++ b/app/models/communication/block/template/gallery.rb @@ -4,14 +4,8 @@ class Communication::Block::Template::Gallery < Communication::Block::Template end def images_with_alt - unless @images_with_alt - @images_with_alt = [] - elements.each do |element| - @images_with_alt << image_with_alt(element) - end - @images_with_alt.compact! - end - @images_with_alt + @images_with_alt ||= elements.map { |element| image_with_alt(element) } + .compact end protected diff --git a/app/models/communication/block/template/organization_chart.rb b/app/models/communication/block/template/organization_chart.rb index ee2f6df39..0164c35af 100644 --- a/app/models/communication/block/template/organization_chart.rb +++ b/app/models/communication/block/template/organization_chart.rb @@ -7,18 +7,8 @@ class Communication::Block::Template::OrganizationChart < Communication::Block:: end def persons_with_role - unless @persons_with_role - @persons_with_role = [] - elements.each do |element| - person = block.university.people.find_by id: element['id'] - next if person.nil? - @persons_with_role << { - person: person, - role: element['role'] - }.to_dot - end - end - @persons_with_role + @persons_with_role ||= elements.map { |element| person_with_role(element) } + .compact end protected @@ -26,4 +16,13 @@ class Communication::Block::Template::OrganizationChart < Communication::Block:: def persons @persons ||= persons_with_role.map { |hash| hash[:person] } end + + def person_with_role(element) + person = block.university.people.find_by id: element['id'] + return if person.nil? + { + person: person, + role: element['role'] + }.to_dot + end end diff --git a/app/models/communication/block/template/partner.rb b/app/models/communication/block/template/partner.rb index d0722eea1..403c55486 100644 --- a/app/models/communication/block/template/partner.rb +++ b/app/models/communication/block/template/partner.rb @@ -5,50 +5,37 @@ class Communication::Block::Template::Partner < Communication::Block::Template end def partners - unless @partners - @partners = [] - elements.each do |element| - # Init to have easy tests in the views and dependencies - element['organization'] = nil - element['blob'] = nil - if element['id'] - organization = university.organizations.find_by id: element['id'] - if organization - element['organization'] = organization - element['name'] = organization.to_s - element['url'] = organization.url - element['blob'] = organization.logo&.blob - end - else - element['blob'] = find_blob element, 'logo' - end - @partners << element.to_dot - end - end - @partners + @partners ||= elements.map { |element| partner(element) } + .compact end protected def organizations - unless @organizations - @organizations = [] - partners.each do |partner| - next if partner.organization.nil? - @organizations << partner.organization - end - end - @organizations + @organizations ||= partners.map { |partner| partner.organization } + .compact end def blobs - unless @blobs - @blobs = [] - partners.each do |partner| - next if partner.blob.nil? - @blobs << partner.blob + @blobs ||= partners.map { |partner| partner.blob } + .compact + end + + def partner(element) + # Init to have easy tests in the views and dependencies + element['organization'] = nil + element['blob'] = nil + if element['id'] + organization = university.organizations.find_by id: element['id'] + if organization + element['organization'] = organization + element['name'] = organization.to_s + element['url'] = organization.url + element['blob'] = organization.logo&.blob end + else + element['blob'] = find_blob element, 'logo' end - @blobs + element.to_dot end end diff --git a/app/models/communication/block/template/post.rb b/app/models/communication/block/template/post.rb index 7b4f3c55d..560150c7d 100644 --- a/app/models/communication/block/template/post.rb +++ b/app/models/communication/block/template/post.rb @@ -27,12 +27,8 @@ class Communication::Block::Template::Post < Communication::Block::Template end def free_posts - array = [] - elements.map do |element| - array << post(element['id']) - end - array.compact! - array + elements.map { |element| post(element['id']) } + .compact end def post(id) diff --git a/app/models/communication/block/template/testimonial.rb b/app/models/communication/block/template/testimonial.rb index 55e1af60f..21e8abb31 100644 --- a/app/models/communication/block/template/testimonial.rb +++ b/app/models/communication/block/template/testimonial.rb @@ -4,22 +4,20 @@ class Communication::Block::Template::Testimonial < Communication::Block::Templa end def testimonials - @testimonials ||= elements.map do |element| - blob = find_blob element, 'photo' - element['blob'] = blob - element.to_dot - end + @testimonials ||= elements.map { |element| testimonial(element) } + .compact end protected def photos - unless @photos - @photos = [] - testimonials.each do |testimonial| - @photos << testimonial.blob if testimonial.blob - end - end - @photos + @photos ||= testimonials.map { |testimonial| testimonial.blob } + .compact + end + + def testimonial(element) + blob = find_blob element, 'photo' + element['blob'] = blob + element.to_dot end end -- GitLab