mirror of
https://github.com/we-promise/sure.git
synced 2026-04-20 12:34:12 +00:00
feat: Add duplicate button when a transaction is selected (#1123)
* feat: Add duplicate button when a transaction is selected * feat: add merchant field * feat: add duplicate transaction btn 2
This commit is contained in:
@@ -5,7 +5,11 @@ class TransactionsController < ApplicationController
|
||||
before_action :store_params!, only: :index
|
||||
|
||||
def new
|
||||
prefill_params_from_duplicate!
|
||||
super
|
||||
apply_duplicate_attributes!
|
||||
@income_categories = Current.family.categories.incomes.alphabetically
|
||||
@expense_categories = Current.family.categories.expenses.alphabetically
|
||||
@categories = Current.family.categories.alphabetically
|
||||
end
|
||||
|
||||
@@ -306,6 +310,35 @@ class TransactionsController < ApplicationController
|
||||
end
|
||||
|
||||
private
|
||||
def duplicate_source
|
||||
return @duplicate_source if defined?(@duplicate_source)
|
||||
@duplicate_source = if params[:duplicate_entry_id].present?
|
||||
source = Current.family.entries.find_by(id: params[:duplicate_entry_id])
|
||||
source if source&.transaction?
|
||||
end
|
||||
end
|
||||
|
||||
def prefill_params_from_duplicate!
|
||||
return unless duplicate_source
|
||||
params[:nature] ||= duplicate_source.amount.negative? ? "inflow" : "outflow"
|
||||
params[:account_id] ||= duplicate_source.account_id.to_s
|
||||
end
|
||||
|
||||
def apply_duplicate_attributes!
|
||||
return unless duplicate_source
|
||||
@entry.assign_attributes(
|
||||
name: duplicate_source.name,
|
||||
amount: duplicate_source.amount.abs,
|
||||
currency: duplicate_source.currency,
|
||||
notes: duplicate_source.notes
|
||||
)
|
||||
@entry.entryable.assign_attributes(
|
||||
category_id: duplicate_source.entryable.category_id,
|
||||
merchant_id: duplicate_source.entryable.merchant_id
|
||||
)
|
||||
@entry.entryable.tag_ids = duplicate_source.entryable.tag_ids
|
||||
end
|
||||
|
||||
def set_entry_for_unlock
|
||||
transaction = Current.family.transactions.find(params[:id])
|
||||
@entry = transaction.entry
|
||||
|
||||
Reference in New Issue
Block a user