mirror of
https://github.com/we-promise/sure.git
synced 2026-04-14 01:24:06 +00:00
fix: Preserve tags on bulk edits (take 3) (#889)
* fix: handle tags separately from entryable_attributes in bulk updates Tags use a join table (taggings) rather than a direct column, which means empty tag_ids clears all tags rather than meaning "no change". This caused bulk category-only edits to accidentally clear existing tags. This fix: - Removes tag_ids from entryable_attributes in Entry.bulk_update! - Adds update_tags parameter to explicitly control tag updates - Uses params.key?(:tag_ids) in controller to detect explicit tag changes - Preserves existing tags when tag_ids is not provided in the request This is a cleaner architectural solution compared to tracking "touched" state in the frontend, as it properly acknowledges the semantic difference between column attributes and join table associations. https://claude.ai/code/session_014CsmTwjteP4qJs6YZqCKnY * fix: handle tags separately in API transaction updates Apply the same pattern to the API endpoint: tags are now handled separately from entryable_attributes to distinguish between "not provided" (preserve existing tags) and "explicitly set to empty" (clear all tags). This allows API consumers to: - Update other fields without affecting tags (omit tag_ids) - Clear all tags (send tag_ids: []) - Set specific tags (send tag_ids: [id1, id2]) https://claude.ai/code/session_014CsmTwjteP4qJs6YZqCKnY * Proposed fix * fix: improve tag handling in bulk updates for transactions * fix: allow bulk edit to clear/preserve tags by omitting hidden multi-select field * PR comments * Dumb copy/paste error * Linter --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: Juan José Mata <jjmata@jjmata.com>
This commit is contained in:
committed by
GitHub
parent
8b89d24314
commit
c77971ea0d
@@ -105,19 +105,29 @@ class Api::V1::TransactionsController < Api::V1::BaseController
|
||||
end
|
||||
|
||||
def update
|
||||
if @entry.update(entry_params_for_update)
|
||||
@entry.sync_account_later
|
||||
@entry.lock_saved_attributes!
|
||||
@entry.transaction.lock_attr!(:tag_ids) if @entry.transaction.tags.any?
|
||||
Entry.transaction do
|
||||
if @entry.update(entry_params_for_update)
|
||||
# Handle tags separately - only when explicitly provided in the request
|
||||
# This allows clearing tags with tag_ids: [] while preserving tags when not specified
|
||||
if tags_provided?
|
||||
@entry.transaction.tag_ids = transaction_params[:tag_ids] || []
|
||||
@entry.transaction.save!
|
||||
@entry.transaction.lock_attr!(:tag_ids) if @entry.transaction.tags.any?
|
||||
end
|
||||
|
||||
@transaction = @entry.transaction
|
||||
render :show
|
||||
else
|
||||
render json: {
|
||||
error: "validation_failed",
|
||||
message: "Transaction could not be updated",
|
||||
errors: @entry.errors.full_messages
|
||||
}, status: :unprocessable_entity
|
||||
@entry.sync_account_later
|
||||
@entry.lock_saved_attributes!
|
||||
|
||||
@transaction = @entry.transaction
|
||||
render :show
|
||||
else
|
||||
render json: {
|
||||
error: "validation_failed",
|
||||
message: "Transaction could not be updated",
|
||||
errors: @entry.errors.full_messages
|
||||
}, status: :unprocessable_entity
|
||||
raise ActiveRecord::Rollback
|
||||
end
|
||||
end
|
||||
|
||||
rescue => e
|
||||
@@ -283,8 +293,9 @@ end
|
||||
entryable_attributes: {
|
||||
id: @entry.entryable_id,
|
||||
category_id: transaction_params[:category_id],
|
||||
merchant_id: transaction_params[:merchant_id],
|
||||
tag_ids: transaction_params[:tag_ids]
|
||||
merchant_id: transaction_params[:merchant_id]
|
||||
# Note: tag_ids handled separately in update action to distinguish
|
||||
# "not provided" from "explicitly set to empty"
|
||||
}.compact_blank
|
||||
}
|
||||
|
||||
@@ -296,6 +307,12 @@ end
|
||||
entry_params.compact
|
||||
end
|
||||
|
||||
# Check if tag_ids was explicitly provided in the request.
|
||||
# This distinguishes between "user wants to update tags" vs "user didn't specify tags".
|
||||
def tags_provided?
|
||||
params[:transaction].key?(:tag_ids)
|
||||
end
|
||||
|
||||
def calculate_signed_amount
|
||||
amount = transaction_params[:amount].to_f
|
||||
nature = transaction_params[:nature]
|
||||
|
||||
Reference in New Issue
Block a user