-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve account transaction, trade, and valuation editing and sync ex…
…perience (#1506) * Consolidate entry controller logic * Transaction builder * Update trades controller to use new params * Load account charts in turbo frames, fix PG overflow * Consolidate tests * Tests passing * Remove unused code * Add client side trade form validations
- Loading branch information
Showing
97 changed files
with
1,079 additions
and
1,135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
class Account::CashesController < ApplicationController | ||
layout :with_sidebar | ||
|
||
before_action :set_account | ||
|
||
def index | ||
@account = Current.family.accounts.find(params[:account_id]) | ||
end | ||
|
||
private | ||
|
||
def set_account | ||
@account = Current.family.accounts.find(params[:account_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
class Account::HoldingsController < ApplicationController | ||
layout :with_sidebar | ||
|
||
before_action :set_account | ||
before_action :set_holding, only: %i[show destroy] | ||
|
||
def index | ||
@holdings = @account.holdings.current | ||
@account = Current.family.accounts.find(params[:account_id]) | ||
@holdings = Current.family.holdings.current | ||
@holdings = @holdings.where(account: @account) if @account | ||
end | ||
|
||
def show | ||
end | ||
|
||
def destroy | ||
@holding.destroy_holding_and_entries! | ||
redirect_back_or_to account_holdings_path(@account) | ||
end | ||
|
||
private | ||
flash[:notice] = t(".success") | ||
|
||
def set_account | ||
@account = Current.family.accounts.find(params[:account_id]) | ||
respond_to do |format| | ||
format.html { redirect_back_or_to account_path(@holding.account) } | ||
format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, account_path(@holding.account)) } | ||
end | ||
end | ||
|
||
private | ||
def set_holding | ||
@holding = @account.holdings.current.find(params[:id]) | ||
@holding = Current.family.holdings.current.find(params[:id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,37 @@ | ||
class Account::TradesController < ApplicationController | ||
layout :with_sidebar | ||
include EntryableResource | ||
|
||
before_action :set_account | ||
before_action :set_entry, only: :update | ||
permitted_entryable_attributes :id, :qty, :price | ||
|
||
def new | ||
@entry = @account.entries.account_trades.new( | ||
currency: @account.currency, | ||
entryable_attributes: {} | ||
) | ||
end | ||
|
||
def index | ||
@entries = @account.entries.reverse_chronological.where(entryable_type: %w[Account::Trade Account::Transaction]) | ||
end | ||
|
||
def create | ||
@builder = Account::EntryBuilder.new(entry_params) | ||
|
||
if entry = @builder.save | ||
entry.sync_account_later | ||
redirect_to @account, notice: t(".success") | ||
else | ||
flash[:alert] = t(".failure") | ||
redirect_back_or_to @account | ||
private | ||
def build_entry | ||
Account::TradeBuilder.new(create_entry_params) | ||
end | ||
end | ||
|
||
def update | ||
@entry.update!(entry_params) | ||
|
||
respond_to do |format| | ||
format.html { redirect_to account_entry_path(@account, @entry), notice: t(".success") } | ||
format.turbo_stream { render turbo_stream: turbo_stream.replace(@entry) } | ||
def create_entry_params | ||
params.require(:account_entry).permit( | ||
:account_id, :date, :amount, :currency, :qty, :price, :ticker, :type, :transfer_account_id | ||
).tap do |params| | ||
account_id = params.delete(:account_id) | ||
params[:account] = Current.family.accounts.find(account_id) | ||
end | ||
end | ||
end | ||
|
||
def securities | ||
query = params[:q] | ||
return render json: [] if query.blank? || query.length < 2 || query.length > 100 | ||
def update_entry_params | ||
return entry_params unless entry_params[:entryable_attributes].present? | ||
|
||
@securities = Security::SynthComboboxOption.find_in_synth(query) | ||
end | ||
update_params = entry_params | ||
update_params = update_params.merge(entryable_type: "Account::Trade") | ||
|
||
private | ||
|
||
def set_account | ||
@account = Current.family.accounts.find(params[:account_id]) | ||
end | ||
qty = update_params[:entryable_attributes][:qty] | ||
price = update_params[:entryable_attributes][:price] | ||
|
||
def set_entry | ||
@entry = @account.entries.find(params[:id]) | ||
end | ||
if qty.present? && price.present? | ||
qty = update_params[:nature] == "inflow" ? -qty.to_d : qty.to_d | ||
update_params[:entryable_attributes][:qty] = qty | ||
update_params[:amount] = qty * price.to_d | ||
end | ||
|
||
def entry_params | ||
params.require(:account_entry) | ||
.permit( | ||
:type, :date, :qty, :ticker, :price, :amount, :notes, :excluded, :currency, :transfer_account_id, :entryable_type, | ||
entryable_attributes: [ | ||
:id, | ||
:qty, | ||
:ticker, | ||
:price | ||
] | ||
) | ||
.merge(account: @account) | ||
update_params.except(:nature) | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
app/controllers/account/transaction_categories_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Account::TransactionCategoriesController < ApplicationController | ||
def update | ||
@entry = Current.family.entries.account_transactions.find(params[:transaction_id]) | ||
@entry.update!(entry_params) | ||
|
||
respond_to do |format| | ||
format.html { redirect_back_or_to account_transaction_path(@entry) } | ||
format.turbo_stream do | ||
render turbo_stream: turbo_stream.replace( | ||
"category_menu_account_transaction_#{@entry.account_transaction_id}", | ||
partial: "categories/menu", | ||
locals: { transaction: @entry.account_transaction } | ||
) | ||
end | ||
end | ||
end | ||
|
||
private | ||
def entry_params | ||
params.require(:account_entry).permit(:entryable_type, entryable_attributes: [ :id, :category_id ]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,55 @@ | ||
class Account::TransactionsController < ApplicationController | ||
layout :with_sidebar | ||
include EntryableResource | ||
|
||
before_action :set_account | ||
before_action :set_entry, only: :update | ||
permitted_entryable_attributes :id, :category_id, :merchant_id, { tag_ids: [] } | ||
|
||
def index | ||
@pagy, @entries = pagy( | ||
@account.entries.account_transactions.reverse_chronological, | ||
limit: params[:per_page] || "10" | ||
) | ||
def bulk_delete | ||
destroyed = Current.family.entries.destroy_by(id: bulk_delete_params[:entry_ids]) | ||
destroyed.map(&:account).uniq.each(&:sync_later) | ||
redirect_back_or_to transactions_url, notice: t(".success", count: destroyed.count) | ||
end | ||
|
||
def update | ||
prev_amount = @entry.amount | ||
prev_date = @entry.date | ||
def bulk_edit | ||
end | ||
|
||
@entry.update!(entry_params.except(:origin)) | ||
@entry.sync_account_later if prev_amount != @entry.amount || prev_date != @entry.date | ||
def bulk_update | ||
updated = Current.family | ||
.entries | ||
.where(id: bulk_update_params[:entry_ids]) | ||
.bulk_update!(bulk_update_params) | ||
|
||
respond_to do |format| | ||
format.html { redirect_to account_entry_path(@account, @entry), notice: t(".success") } | ||
format.turbo_stream do | ||
render turbo_stream: turbo_stream.replace( | ||
@entry, | ||
partial: "account/entries/entry", | ||
locals: entry_locals.merge(entry: @entry) | ||
) | ||
end | ||
end | ||
redirect_back_or_to transactions_url, notice: t(".success", count: updated) | ||
end | ||
|
||
private | ||
def set_account | ||
@account = Current.family.accounts.find(params[:account_id]) | ||
end | ||
def mark_transfers | ||
Current.family | ||
.entries | ||
.where(id: bulk_update_params[:entry_ids]) | ||
.mark_transfers! | ||
|
||
def set_entry | ||
@entry = @account.entries.find(params[:id]) | ||
end | ||
redirect_back_or_to transactions_url, notice: t(".success") | ||
end | ||
|
||
def entry_locals | ||
{ | ||
selectable: entry_params[:origin].present?, | ||
show_balance: entry_params[:origin] == "account", | ||
origin: entry_params[:origin] | ||
} | ||
end | ||
def unmark_transfers | ||
Current.family | ||
.entries | ||
.where(id: bulk_update_params[:entry_ids]) | ||
.update_all marked_as_transfer: false | ||
|
||
def entry_params | ||
params.require(:account_entry) | ||
.permit( | ||
:name, :date, :amount, :currency, :excluded, :notes, :entryable_type, :nature, :origin, | ||
entryable_attributes: [ | ||
:id, | ||
:category_id, | ||
:merchant_id, | ||
{ tag_ids: [] } | ||
] | ||
).tap do |permitted_params| | ||
nature = permitted_params.delete(:nature) | ||
redirect_back_or_to transactions_url, notice: t(".success") | ||
end | ||
|
||
if permitted_params[:amount] | ||
amount_value = permitted_params[:amount].to_d | ||
private | ||
def bulk_delete_params | ||
params.require(:bulk_delete).permit(entry_ids: []) | ||
end | ||
|
||
if nature == "income" | ||
amount_value *= -1 | ||
end | ||
def bulk_update_params | ||
params.require(:bulk_update).permit(:date, :notes, :category_id, :merchant_id, entry_ids: []) | ||
end | ||
|
||
permitted_params[:amount] = amount_value | ||
end | ||
end | ||
def search_params | ||
params.fetch(:q, {}) | ||
.permit(:start_date, :end_date, :search, :amount, :amount_operator, accounts: [], account_ids: [], categories: [], merchants: [], types: [], tags: []) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,3 @@ | ||
class Account::ValuationsController < ApplicationController | ||
layout :with_sidebar | ||
|
||
before_action :set_account | ||
|
||
def new | ||
@entry = @account.entries.account_valuations.new( | ||
currency: @account.currency, | ||
entryable_attributes: {} | ||
) | ||
end | ||
|
||
def create | ||
@entry = @account.entries.account_valuations.new(entry_params.merge(entryable_attributes: {})) | ||
|
||
if @entry.save | ||
@entry.sync_account_later | ||
redirect_back_or_to account_valuations_path(@account), notice: t(".success") | ||
else | ||
flash[:alert] = @entry.errors.full_messages.to_sentence | ||
redirect_to @account | ||
end | ||
end | ||
|
||
def index | ||
@entries = @account.entries.account_valuations.reverse_chronological | ||
end | ||
|
||
private | ||
|
||
def set_account | ||
@account = Current.family.accounts.find(params[:account_id]) | ||
end | ||
|
||
def entry_params | ||
params.require(:account_entry).permit(:name, :date, :amount, :currency) | ||
end | ||
include EntryableResource | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.