Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Propagate category color changes to subcategories (#4564)

Niels Eulink28cdec5c6b67
diff --git a/app/models/assistant/function/update_category.rb b/app/models/assistant/function/update_category.rb
index 5d0f0edcd9..1324432d47 100644
--- a/app/models/assistant/function/update_category.rb
+++ b/app/models/assistant/function/update_category.rb
@@ -9,8 +9,8 @@ class Assistant::Function::UpdateCategory < Assistant::Function
Updates an existing category's name, color, icon, or parent.
Use get_categories first to find the category id. At least one of name, color,
- icon, or parent_id must be supplied. Changing a parent's color does not cascade to
- existing subcategories (their colors are set when they are saved).
+ icon, or parent_id must be supplied. Changing a parent's color also changes the color
+ of all its subcategories.
Pass parent_id to move the category under another top-level category (it takes the
parent's color), or pass an empty string to make it a top-level category. A category
diff --git a/app/models/category.rb b/app/models/category.rb
index 2dc27638db..93f635bf7c 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -30,6 +30,7 @@ class Category < ApplicationRecord
validate :parent_in_same_family
before_save :inherit_color_from_parent
+ after_update :propagate_color_to_subcategories, if: :saved_change_to_color?
# prepend: runs before dependent: :nullify's own before_destroy, so the
# reassignment (which touches entries to bust report caches) still sees the
@@ -434,6 +435,10 @@ class Category < ApplicationRecord
end
private
+ def propagate_color_to_subcategories
+ subcategories.update_all(color: color, updated_at: Time.current)
+ end
+
def reassign_transactions_to_nothing
Transaction.reassign_category!(transactions, nil)
end
diff --git a/test/models/assistant/function/update_category_test.rb b/test/models/assistant/function/update_category_test.rb
index 800998da27..9e2dcf6739 100644
--- a/test/models/assistant/function/update_category_test.rb
+++ b/test/models/assistant/function/update_category_test.rb
@@ -31,6 +31,15 @@ class Assistant::Function::UpdateCategoryTest < ActiveSupport::TestCase
assert_equal "#6471eb", @category.reload.color
end
+ test "changing a parent's color also changes its subcategories" do
+ subcategory = @family.categories.create!(name: "Takeout", color: "#000000", parent: @category)
+
+ result = @fn.call("id" => @category.id, "color" => "#6471eb")
+
+ assert result[:success]
+ assert_equal "#6471eb", subcategory.reload.color
+ end
+
test "updates category icon" do
result = @fn.call("id" => @category.id, "icon" => "pizza")
diff --git a/test/models/category_test.rb b/test/models/category_test.rb
index 7a4bfcf7c0..7b034c438f 100644
--- a/test/models/category_test.rb
+++ b/test/models/category_test.rb
@@ -107,6 +107,20 @@ class CategoryTest < ActiveSupport::TestCase
assert_equal parent.color, category.color, "subcategories inherit their parent's color"
end
+ test "changing a parent's color updates its subcategories" do
+ parent = categories(:food_and_drink)
+ subcategory = @family.categories.create!(name: "Color Child", color: "#000000", parent: parent)
+ previous_updated_at = subcategory.updated_at
+
+ travel 1.second do
+ parent.update!(color: "#123456")
+ end
+
+ subcategory.reload
+ assert_equal "#123456", subcategory.color
+ assert_operator subcategory.updated_at, :>, previous_updated_at
+ end
+
test "subcategory can only be one level deep" do
category = categories(:subcategory)