The personal finance app for everyone (by everyone)
Ruby
fix: add hex color validation to Category model and form (to solve #1247) (#1341)
Louis8ed69132cfaf
diff --git a/app/models/category.rb b/app/models/category.rbindex 94d5097e11..39a740f7d5 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -9,6 +9,7 @@ class Category < ApplicationRecordbelongs_to :parent, class_name: "Category", optional: truevalidates :name, :color, :lucide_icon, :family, presence: true+ validates :color, format: { with: /\A#[0-9A-Fa-f]{6}\z/ }validates :name, uniqueness: { scope: :family_id }validate :category_level_limitdiff --git a/app/views/categories/_form.html.erb b/app/views/categories/_form.html.erbindex 3b9e6f1b48..1f3e5fbebb 100644--- a/app/views/categories/_form.html.erb+++ b/app/views/categories/_form.html.erb@@ -30,7 +30,7 @@<div class="flex gap-2 items-center hidden flex-col" data-category-target="paletteSection"><div class="flex gap-2 items-center w-full"><div class="w-6 h-6 p-4 rounded-full cursor-pointer" style="background-color: <%= category.color %>" data-category-target="colorPreview"></div>- <%= f.text_field :color , data: { category_target: "colorInput"}, inline: true %>+ <%= f.text_field :color, data: { category_target: "colorInput" }, inline: true, pattern: "^#[0-9A-Fa-f]{6}$" %><%= icon "palette", size: "2xl", data: { action: "click->category#toggleSections" } %></div><div data-category-target="validationMessage" class="hidden self-start flex gap-1 items-center text-xs text-destructive ">diff --git a/test/models/budget_test.rb b/test/models/budget_test.rbindex f681bebc20..13f8f5a2b4 100644--- a/test/models/budget_test.rb+++ b/test/models/budget_test.rb@@ -264,7 +264,7 @@ class BudgetTest < ActiveSupport::TestCasesource_budget.update!(budgeted_spending: 4000, expected_income: 6000)# Create a category only in the source budget- temp_category = Category.create!(name: "Temp #{Time.now.to_f}", family: family, color: "#aaa")+ temp_category = Category.create!(name: "Temp #{Time.now.to_f}", family: family, color: "#aaaaaa")source_budget.budget_categories.create!(category: temp_category, budgeted_spending: 100, currency: "USD")target_budget = Budget.find_or_bootstrap(family, start_date: 1.month.ago)@@ -283,7 +283,7 @@ class BudgetTest < ActiveSupport::TestCasetarget_budget = Budget.find_or_bootstrap(family, start_date: 1.month.ago)# Add a new category only to the target- new_category = Category.create!(name: "New #{Time.now.to_f}", family: family, color: "#bbb")+ new_category = Category.create!(name: "New #{Time.now.to_f}", family: family, color: "#bbbbbb")target_budget.budget_categories.create!(category: new_category, budgeted_spending: 0, currency: "USD")target_budget.copy_from!(source_budget)diff --git a/test/models/category_test.rb b/test/models/category_test.rbindex da4f9a48af..fc4036600d 100644--- a/test/models/category_test.rb+++ b/test/models/category_test.rb@@ -40,4 +40,19 @@ class CategoryTest < ActiveSupport::TestCaseassert names.all? { |name| name.is_a?(String) }assert_equal names, names.uniq # No duplicatesend++ test "should accept valid 6-digit hex colors" do+ [ "#FFFFFF", "#000000", "#123456", "#ABCDEF", "#abcdef" ].each do |color|+ category = Category.new(name: "Category #{color}", color: color, lucide_icon: "shapes", family: @family)+ assert category.valid?, "#{color} should be valid"+ end+ end++ test "should reject invalid colors" do+ [ "invalid", "#123", "#1234567", "#GGGGGG", "red", "ffffff", "#ffff", "" ].each do |color|+ category = Category.new(name: "Category #{color}", color: color, lucide_icon: "shapes", family: @family)+ assert_not category.valid?, "#{color} should be invalid"+ assert_includes category.errors[:color], "is invalid"+ end+ endend