Use tag.attributes to set conditional HTML attributes

Using an `if..else` block made the code harder to follow since the
opening tag was inside the block but the closing tag was outside it.
Moreover, it didn't work well with HTML Beautifier (a gem we're going to
introduce to manage ERB indentations).
This commit is contained in:
Javi Martín
2025-02-22 18:48:36 +01:00
parent 1389d45646
commit 291620abf7
3 changed files with 22 additions and 14 deletions

View File

@@ -1,9 +1,4 @@
<% if budget.image.present? %> <div <%= tag.attributes(html_attributes) %>>
<div class="budget-header with-background-image"
style="<%= attached_background_css polymorphic_path(budget.image.variant(:large)) %>">
<% else %>
<div class="budget-header">
<% end %>
<div class="row"> <div class="row">
<div class="small-12 column text-center"> <div class="small-12 column text-center">
<span class="budget-title"><%= t("budgets.index.title") %></span> <span class="budget-title"><%= t("budgets.index.title") %></span>

View File

@@ -5,4 +5,17 @@ class Budgets::BudgetComponent < ApplicationComponent
def initialize(budget) def initialize(budget)
@budget = budget @budget = budget
end end
private
def html_attributes
if budget.image.present?
{
class: "budget-header with-background-image",
style: attached_background_css(polymorphic_path(budget.image.variant(:large)))
}
else
{ class: "budget-header" }
end
end
end end

View File

@@ -58,14 +58,14 @@
<div><span class="panel-title"><%= t("legislation.draft_versions.show.text_body") %></span></div> <div><span class="panel-title"><%= t("legislation.draft_versions.show.text_body") %></span></div>
</div> </div>
<div id="sticky-panel" class="draft-text"> <div id="sticky-panel" class="draft-text">
<% if @draft_version.final_version? %> <section <%= tag.attributes(
<section> class: "legislation-annotatable",
<% else %> data: {
<section class="legislation-annotatable" "legislation-draft-version-id": @draft_version.id,
data-legislation-draft-version-id="<%= @draft_version.id %>" "legislation-annotatable-base-url": legislation_process_draft_version_path(@process, @draft_version),
data-legislation-annotatable-base-url="<%= legislation_process_draft_version_path(@process, @draft_version) %>" "legislation-open-phase": @process.allegations_phase.open?
data-legislation-open-phase="<%= @process.allegations_phase.open? %>"> }
<% end %> ) unless @draft_version.final_version? %>>
<%= AdminLegislationSanitizer.new.sanitize(@draft_version.body_html) %> <%= AdminLegislationSanitizer.new.sanitize(@draft_version.body_html) %>
</section> </section>
</div> </div>