Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account Activity View + Account Forms #1406

Merged
merged 22 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions app/controllers/account/entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ class Account::EntriesController < ApplicationController
before_action :set_account
before_action :set_entry, only: %i[edit update show destroy]

def index
@q = search_params
@pagy, @entries = pagy(@account.entries.search(@q).reverse_chronological, limit: params[:per_page] || "10")
end

def edit
render entryable_view_path(:edit)
end

def update
prev_amount = @entry.amount
prev_date = @entry.date

@entry.update!(entry_params)
@entry.sync_account_later
@entry.sync_account_later if prev_amount != @entry.amount || prev_date != @entry.date

respond_to do |format|
format.html { redirect_to account_entry_path(@account, @entry), notice: t(".success") }
Expand Down Expand Up @@ -43,6 +51,11 @@ def set_entry
end

def entry_params
params.require(:account_entry).permit(:name, :date, :amount, :currency)
params.require(:account_entry).permit(:name, :date, :amount, :currency, :notes)
end

def search_params
params.fetch(:q, {})
.permit(:search)
end
end
4 changes: 2 additions & 2 deletions app/controllers/account/trades_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def create

if entry = @builder.save
entry.sync_account_later
redirect_to account_path(@account), notice: t(".success")
redirect_to @account, notice: t(".success")
else
flash[:alert] = t(".failure")
redirect_back_or_to account_path(@account)
redirect_back_or_to @account
end
end

Expand Down
25 changes: 21 additions & 4 deletions app/controllers/account/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ def index
end

def update
@entry.update!(entry_params)
prev_amount = @entry.amount
prev_date = @entry.date

@entry.update!(entry_params.except(:origin))
@entry.sync_account_later if prev_amount != @entry.amount || prev_date != @entry.date

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) }
format.turbo_stream do
render turbo_stream: turbo_stream.replace(
@entry,
partial: "account/entries/entry",
locals: entry_locals.merge(entry: @entry)
)
end
end
end

private

def set_account
@account = Current.family.accounts.find(params[:account_id])
end
Expand All @@ -30,10 +39,18 @@ def set_entry
@entry = @account.entries.find(params[:id])
end

def entry_locals
{
selectable: entry_params[:origin].present?,
show_balance: entry_params[:origin] == "account",
origin: entry_params[:origin]
}
end

def entry_params
params.require(:account_entry)
.permit(
:name, :date, :amount, :currency, :excluded, :notes, :entryable_type, :nature,
:name, :date, :amount, :currency, :excluded, :notes, :entryable_type, :nature, :origin,
entryable_attributes: [
:id,
:category_id,
Expand Down
26 changes: 22 additions & 4 deletions app/controllers/account/transfers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
class Account::TransfersController < ApplicationController
layout :with_sidebar

before_action :set_transfer, only: :destroy
before_action :set_transfer, only: %i[destroy show update]

def new
@transfer = Account::Transfer.new
end

def show
end

def create
from_account = Current.family.accounts.find(transfer_params[:from_account_id])
to_account = Current.family.accounts.find(transfer_params[:to_account_id])
Expand All @@ -27,18 +30,33 @@ def create
end
end

def update
@transfer.update_entries!(transfer_update_params)
redirect_back_or_to transactions_url, notice: t(".success")
end

def destroy
@transfer.destroy_and_remove_marks!
@transfer.destroy!
redirect_back_or_to transactions_url, notice: t(".success")
end

private

def set_transfer
@transfer = Account::Transfer.find(params[:id])
record = Account::Transfer.find(params[:id])

unless record.entries.all? { |entry| Current.family.accounts.include?(entry.account) }
raise ActiveRecord::RecordNotFound
end

@transfer = record
end

def transfer_params
params.require(:account_transfer).permit(:from_account_id, :to_account_id, :amount, :date, :name)
params.require(:account_transfer).permit(:from_account_id, :to_account_id, :amount, :date, :name, :excluded)
end

def transfer_update_params
params.require(:account_transfer).permit(:excluded, :notes)
end
end
2 changes: 1 addition & 1 deletion app/controllers/account/valuations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create
redirect_back_or_to account_valuations_path(@account), notice: t(".success")
else
flash[:alert] = @entry.errors.full_messages.to_sentence
redirect_to account_path(@account)
redirect_to @account
end
end

Expand Down
48 changes: 2 additions & 46 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class AccountsController < ApplicationController
layout :with_sidebar

include Filterable
before_action :set_account, only: %i[edit show destroy sync update]
before_action :set_account, only: %i[sync]

def index
@institutions = Current.family.institutions
@accounts = Current.family.accounts.ungrouped.alphabetically
end

def summary
@period = Period.from_param(params[:period])
snapshot = Current.family.snapshot(@period)
@net_worth_series = snapshot[:net_worth_series]
@asset_series = snapshot[:asset_series]
Expand All @@ -22,45 +22,6 @@ def list
render layout: false
end

def new
@account = Account.new(currency: Current.family.currency)
@account.accountable = Accountable.from_type(params[:type])&.new if params[:type].present?
@account.accountable.address = Address.new if @account.accountable.is_a?(Property)

if params[:institution_id]
@account.institution = Current.family.institutions.find_by(id: params[:institution_id])
end
end

def show
end

def edit
@account.accountable.build_address if @account.accountable.is_a?(Property) && @account.accountable.address.blank?
end

def update
@account.update_with_sync!(account_params)
redirect_back_or_to account_path(@account), notice: t(".success")
end

def create
@account = Current.family
.accounts
.create_with_optional_start_balance! \
attributes: account_params.except(:start_date, :start_balance),
start_date: account_params[:start_date],
start_balance: account_params[:start_balance]
@account.sync_later

redirect_back_or_to account_path(@account), notice: t(".success")
end

def destroy
@account.destroy!
redirect_to accounts_path, notice: t(".success")
end

def sync
unless @account.syncing?
@account.sync_later
Expand All @@ -73,12 +34,7 @@ def sync_all
end

private

def set_account
@account = Current.family.accounts.find(params[:id])
end

def account_params
params.require(:account).permit(:name, :accountable_type, :mode, :balance, :start_date, :start_balance, :currency, :subtype, :is_active, :institution_id)
end
end
60 changes: 60 additions & 0 deletions app/controllers/concerns/accountable_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module AccountableResource
extend ActiveSupport::Concern

included do
layout :with_sidebar
before_action :set_account, only: [ :show, :edit, :update, :destroy ]
end

class_methods do
def permitted_accountable_attributes(*attrs)
@permitted_accountable_attributes = attrs if attrs.any?
@permitted_accountable_attributes ||= [ :id ]
end
end

def new
@account = Current.family.accounts.build(
currency: Current.family.currency,
accountable: accountable_type.new,
institution_id: params[:institution_id]
)
end

def show
end

def edit
end

def create
@account = Current.family.accounts.create_and_sync(account_params.except(:return_to))
redirect_to account_params[:return_to].presence || @account, notice: t(".success")
end

def update
@account.update_with_sync!(account_params.except(:return_to))
redirect_back_or_to @account, notice: t(".success")
end

def destroy
@account.destroy!
redirect_to accounts_path, notice: t(".success")
end

private
def accountable_type
controller_name.classify.constantize
end

def set_account
@account = Current.family.accounts.find(params[:id])
end

def account_params
params.require(:account).permit(
:name, :is_active, :balance, :subtype, :currency, :institution_id, :accountable_type, :return_to,
accountable_attributes: self.class.permitted_accountable_attributes
)
end
end
23 changes: 0 additions & 23 deletions app/controllers/concerns/filterable.rb

This file was deleted.

49 changes: 10 additions & 39 deletions app/controllers/credit_cards_controller.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
class CreditCardsController < ApplicationController
before_action :set_account, only: :update

def create
account = Current.family
.accounts
.create_with_optional_start_balance! \
attributes: account_params.except(:start_date, :start_balance),
start_date: account_params[:start_date],
start_balance: account_params[:start_balance]

account.sync_later
redirect_to account, notice: t(".success")
end

def update
@account.update_with_sync!(account_params)
redirect_to @account, notice: t(".success")
end

private

def set_account
@account = Current.family.accounts.find(params[:id])
end

def account_params
params.require(:account)
.permit(
:name, :balance, :institution_id, :mode, :start_date, :start_balance, :currency, :accountable_type,
accountable_attributes: [
:id,
:available_credit,
:minimum_payment,
:apr,
:annual_fee,
:expiration_date
]
)
end
include AccountableResource

permitted_accountable_attributes(
:id,
:available_credit,
:minimum_payment,
:apr,
:annual_fee,
:expiration_date
)
end
3 changes: 3 additions & 0 deletions app/controllers/cryptos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CryptosController < ApplicationController
include AccountableResource
end
3 changes: 3 additions & 0 deletions app/controllers/depositories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class DepositoriesController < ApplicationController
include AccountableResource
end
3 changes: 3 additions & 0 deletions app/controllers/investments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class InvestmentsController < ApplicationController
include AccountableResource
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ class Issue::ExchangeRateProviderMissingsController < ApplicationController

def update
Setting.synth_api_key = exchange_rate_params[:synth_api_key]
@issue.issuable.sync_later
redirect_back_or_to account_path(@issue.issuable)
account = @issue.issuable
account.sync_later
redirect_back_or_to account
end

private
Expand Down
Loading