Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Make #icon_for_color skip render if hexcode invalid

Alexander Brandon Colesc65265c7e814
diff --git a/app/helpers/colors_helper.rb b/app/helpers/colors_helper.rb
index 5effa615bc0..c84419ed2dc 100644
--- a/app/helpers/colors_helper.rb
+++ b/app/helpers/colors_helper.rb
@@ -122,8 +122,7 @@ module ColorsHelper
end
def icon_for_color(color, options = {})
- return unless color
- return if color.hexcode.blank?
+ return unless color&.valid_attribute?(:hexcode)
style = join_style_arguments(
"background-color: #{color.hexcode}",
diff --git a/spec/helpers/colors_helper_spec.rb b/spec/helpers/colors_helper_spec.rb
index b9f7c4fdb0a..b66388208c5 100644
--- a/spec/helpers/colors_helper_spec.rb
+++ b/spec/helpers/colors_helper_spec.rb
@@ -44,4 +44,26 @@ RSpec.describe ColorsHelper do
expect(helper.hl_background_class("foo_bar", model)).to eq("__hl_background_foo_bar_5")
end
end
+
+ describe "#icon_for_color" do
+ context "with nil color" do
+ it "renders nothing" do
+ expect(helper.icon_for_color(nil)).to be_blank
+ end
+ end
+
+ context "with valid color" do
+ it "renders a color preview" do
+ expect(helper.icon_for_color(Color.new(hexcode: "#ff00ff"))).to be_html_eql %{
+ <span class="color--preview " style="background-color: #FF00FF;border-color: #80008050"> </span>
+ }.squish
+ end
+ end
+
+ context "with invalid color (invalid hexcode)" do
+ it "renders nothing" do
+ expect(helper.icon_for_color(Color.new(hexcode: "#ffXXff"))).to be_blank
+ end
+ end
+ end
end