From 2d6c3fc2b8085de0f492c1550a4d98caa9f5bc7c Mon Sep 17 00:00:00 2001 From: Arnaud Levy Date: Thu, 9 Nov 2023 15:50:53 +0100 Subject: [PATCH 1/2] clean --- app/models/communication/block.rb | 21 ++++++++++++++++--- .../communication/blocks/_static.html.erb | 6 +++++- .../blocks/headings/_static.html.erb | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/models/communication/block.rb b/app/models/communication/block.rb index 6c62ac2d1..544fd22c9 100644 --- a/app/models/communication/block.rb +++ b/app/models/communication/block.rb @@ -153,9 +153,19 @@ def full_text template.full_text end - def heading_level - heading.present? ? heading.level + 1 - : DEFAULT_HEADING_LEVEL + def heading_level_self + title.present? ? heading_level_base + : false + end + + def heading_level_children + return false unless has_children? + heading_level_self ? heading_level_self + 1 + : heading_level_base + end + + def has_children? + template.respond_to?(:elements) && template.elements.any? end def to_s @@ -165,6 +175,11 @@ def to_s protected + def heading_level_base + heading.present? ? heading.level + 1 + : DEFAULT_HEADING_LEVEL + end + def last_ordered_element about.blocks.where(heading_id: heading_id).ordered.last end diff --git a/app/views/admin/communication/blocks/_static.html.erb b/app/views/admin/communication/blocks/_static.html.erb index c19209abb..80edd2749 100644 --- a/app/views/admin/communication/blocks/_static.html.erb +++ b/app/views/admin/communication/blocks/_static.html.erb @@ -7,7 +7,11 @@ should_render_data = block.data && block.data.present? title: >- <%= prepare_text_for_static block.title %> position: <%= block.position %> - heading_level: <%= block.heading_level %> + ranks: + self: <%= block.heading_level_self %> +<% if block.has_children? %> + children: <%= block.heading_level_children %> +<% end %> data: <%= render template_path, block: block, diff --git a/app/views/admin/communication/blocks/headings/_static.html.erb b/app/views/admin/communication/blocks/headings/_static.html.erb index 0692265a8..316919c0f 100644 --- a/app/views/admin/communication/blocks/headings/_static.html.erb +++ b/app/views/admin/communication/blocks/headings/_static.html.erb @@ -2,7 +2,7 @@ title: >- <%= prepare_text_for_static heading.title %> position: <%= heading.position %> - level: <%= heading.level %> + rank: <%= heading.level %> <% heading.blocks.published.ordered.each do |block| %> <%= render 'admin/communication/blocks/static', block: block %> <% end %> From 0cbfaafa5290e291b92089873971442dd9314497 Mon Sep 17 00:00:00 2001 From: Arnaud Levy Date: Thu, 9 Nov 2023 16:13:59 +0100 Subject: [PATCH 2/2] trait --- app/models/communication/block.rb | 22 +-------------- .../communication/block/with_heading_ranks.rb | 27 +++++++++++++++++++ .../communication/blocks/_static.html.erb | 6 ++--- 3 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 app/models/communication/block/with_heading_ranks.rb diff --git a/app/models/communication/block.rb b/app/models/communication/block.rb index 544fd22c9..71828903b 100644 --- a/app/models/communication/block.rb +++ b/app/models/communication/block.rb @@ -33,13 +33,13 @@ class Communication::Block < ApplicationRecord include AsIndirectObject include WithAccessibility + include WithHeadingRanks include WithPosition include WithUniversity include Sanitizable IMAGE_MAX_SIZE = 5.megabytes FILE_MAX_SIZE = 100.megabytes - DEFAULT_HEADING_LEVEL = 2 # h1 is the page title belongs_to :about, polymorphic: true belongs_to :heading, optional: true @@ -153,21 +153,6 @@ def full_text template.full_text end - def heading_level_self - title.present? ? heading_level_base - : false - end - - def heading_level_children - return false unless has_children? - heading_level_self ? heading_level_self + 1 - : heading_level_base - end - - def has_children? - template.respond_to?(:elements) && template.elements.any? - end - def to_s title.blank? ? "#{Communication::Block.model_name.human} #{position}" : "#{title}" @@ -175,11 +160,6 @@ def to_s protected - def heading_level_base - heading.present? ? heading.level + 1 - : DEFAULT_HEADING_LEVEL - end - def last_ordered_element about.blocks.where(heading_id: heading_id).ordered.last end diff --git a/app/models/communication/block/with_heading_ranks.rb b/app/models/communication/block/with_heading_ranks.rb new file mode 100644 index 000000000..e134f0ae8 --- /dev/null +++ b/app/models/communication/block/with_heading_ranks.rb @@ -0,0 +1,27 @@ +module Communication::Block::WithHeadingRanks + extend ActiveSupport::Concern + + DEFAULT_HEADING_LEVEL = 2 # h1 is the page title + + def heading_rank_self + title.present? ? heading_rank_base + : false + end + + def heading_rank_children + return false unless heading_children? + heading_rank_self ? heading_rank_self + 1 + : heading_rank_base + end + + def heading_children? + template.respond_to?(:elements) && template.elements.any? + end + + protected + + def heading_rank_base + heading.present? ? heading.level + 1 + : DEFAULT_HEADING_LEVEL + end +end diff --git a/app/views/admin/communication/blocks/_static.html.erb b/app/views/admin/communication/blocks/_static.html.erb index 80edd2749..e0c2d9d88 100644 --- a/app/views/admin/communication/blocks/_static.html.erb +++ b/app/views/admin/communication/blocks/_static.html.erb @@ -8,9 +8,9 @@ should_render_data = block.data && block.data.present? <%= prepare_text_for_static block.title %> position: <%= block.position %> ranks: - self: <%= block.heading_level_self %> -<% if block.has_children? %> - children: <%= block.heading_level_children %> + self: <%= block.heading_rank_self %> +<% if block.heading_children? %> + children: <%= block.heading_rank_children %> <% end %> data: <%= render template_path,