Unify code applying the colors of a process

We had some duplication because the `css_for_process_header` was using
an instance variable, and so it couldn't be called from a partial where
this instance variable wasn't available.

Using a local variable and passing it as a parameter (as we should
always do) solves the issue and lets us simplify the code.
This commit is contained in:
Javi Martín
2025-02-20 18:30:57 +01:00
parent a1352de9eb
commit 53ff81dfdf
4 changed files with 16 additions and 19 deletions

View File

@@ -18,13 +18,13 @@ module LegislationHelper
} }
end end
def banner_color? def banner_color?(process)
@process.background_color.present? && @process.font_color.present? process.background_color.present? && process.font_color.present?
end end
def css_for_process_header def css_for_process_header(process)
if banner_color? if banner_color?(process)
"background: #{@process.background_color};color: #{@process.font_color};" "background: #{process.background_color};color: #{process.font_color};"
end end
end end
end end

View File

@@ -1,8 +1,8 @@
<div class="legislation-hero jumbo" style="<%= css_for_process_header %>"> <div class="legislation-hero jumbo" style="<%= css_for_process_header(@process) %>">
<div class="row"> <div class="row">
<div class="small-12 medium-9 column"> <div class="small-12 medium-9 column">
<% if banner_color? %> <% if banner_color?(@process) %>
<%= link_to t("shared.back"), <%= link_to t("shared.back"),
legislation_processes_path, legislation_processes_path,
class: "icon-angle-left", class: "icon-angle-left",

View File

@@ -12,10 +12,7 @@
<%= markdown @process.additional_info if @process.additional_info %> <%= markdown @process.additional_info if @process.additional_info %>
</div> </div>
<button type="button" class="button" data-toggle="additional_info" <button type="button" class="button" data-toggle="additional_info" style="<%= css_for_process_header(process) %>">
<% if banner_color? %>
style="background:<%= process.font_color %>; color:<%= process.background_color %>;"
<% end %>>
<%= t("legislation.processes.header.additional_info") %> <%= t("legislation.processes.header.additional_info") %>
</button> </button>
<% end %> <% end %>

View File

@@ -9,23 +9,23 @@ describe LegislationHelper do
describe "banner colors presence" do describe "banner colors presence" do
it "background and font color exist" do it "background and font color exist" do
@process = build(:legislation_process, background_color: "#944949", font_color: "#ffffff") process = build(:legislation_process, background_color: "#944949", font_color: "#ffffff")
expect(banner_color?).to be true expect(banner_color?(process)).to be true
end end
it "background color exist and font color not exist" do it "background color exist and font color not exist" do
@process = build(:legislation_process, background_color: "#944949", font_color: "") process = build(:legislation_process, background_color: "#944949", font_color: "")
expect(banner_color?).to be false expect(banner_color?(process)).to be false
end end
it "background color not exist and font color exist" do it "background color not exist and font color exist" do
@process = build(:legislation_process, background_color: "", font_color: "#944949") process = build(:legislation_process, background_color: "", font_color: "#944949")
expect(banner_color?).to be false expect(banner_color?(process)).to be false
end end
it "background and font color not exist" do it "background and font color not exist" do
@process = build(:legislation_process, background_color: "", font_color: "") process = build(:legislation_process, background_color: "", font_color: "")
expect(banner_color?).to be false expect(banner_color?(process)).to be false
end end
end end
end end