Skip to content

Commit

Permalink
Account namespace updates: part 4 (transfers, singular namespacing) (m…
Browse files Browse the repository at this point in the history
…aybe-finance#896)

* Move Transfer to Account namespace

* Fix partial resolution due to namespacing plurality

* Make category and tag controllers consistent with namespacing convention

* Update stale partial reference
  • Loading branch information
zachgoll authored Jun 20, 2024
1 parent dc3147c commit bddaab0
Show file tree
Hide file tree
Showing 50 changed files with 227 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Accounts::LogosController < ApplicationController
class Account::LogosController < ApplicationController
def show
@account = Current.family.accounts.find(params[:account_id])
render_placeholder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
class TransfersController < ApplicationController
class Account::TransfersController < ApplicationController
layout "with_sidebar"

before_action :set_transfer, only: :destroy

def new
@transfer = Transfer.new
@transfer = Account::Transfer.new
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])

@transfer = Transfer.build_from_accounts from_account, to_account, \
@transfer = Account::Transfer.build_from_accounts from_account, to_account, \
date: transfer_params[:date],
amount: transfer_params[:amount].to_d,
currency: transfer_params[:currency],
Expand All @@ -20,7 +20,10 @@ def create
if @transfer.save
redirect_to transactions_path, notice: t(".success")
else
render :new, status: :unprocessable_entity
# TODO: this is not an ideal way to handle errors and should eventually be improved.
# See: https://github.com/hotwired/turbo-rails/pull/367
flash[:error] = @transfer.errors.full_messages.to_sentence
redirect_to transactions_path
end
end

Expand All @@ -32,10 +35,10 @@ def destroy
private

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

def transfer_params
params.require(:transfer).permit(:from_account_id, :to_account_id, :amount, :currency, :date, :name)
params.require(:account_transfer).permit(:from_account_id, :to_account_id, :amount, :currency, :date, :name)
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Categories::DeletionsController < ApplicationController
class Category::DeletionsController < ApplicationController
layout "with_sidebar"

before_action :set_category
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Categories::DropdownsController < ApplicationController
class Category::DropdownsController < ApplicationController
before_action :set_from_params

def show
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Tags::DeletionsController < ApplicationController
class Tag::DeletionsController < ApplicationController
layout "with_sidebar"

before_action :set_tag
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Transactions::RowsController < ApplicationController
class Transaction::RowsController < ApplicationController
before_action :set_transaction, only: %i[ show update ]

def show
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/transaction/rules_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Transaction::RulesController < ApplicationController
layout "with_sidebar"

def index
end
end
6 changes: 0 additions & 6 deletions app/controllers/transactions/rules_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/transfer.rb → app/models/account/transfer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Transfer < ApplicationRecord
class Account::Transfer < ApplicationRecord
has_many :transactions, dependent: :nullify

validate :transaction_count, :from_different_accounts, :net_zero_flows, :all_transactions_marked
Expand Down
4 changes: 2 additions & 2 deletions app/models/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Transaction < ApplicationRecord
monetize :amount

belongs_to :account
belongs_to :transfer, optional: true
belongs_to :transfer, optional: true, class_name: "Account::Transfer"
belongs_to :category, optional: true
belongs_to :merchant, optional: true
has_many :taggings, as: :taggable, dependent: :destroy
Expand Down Expand Up @@ -70,7 +70,7 @@ def mark_transfers!
update_all marked_as_transfer: true

# Attempt to "auto match" and save a transfer if 2 transactions selected
Transfer.new(transactions: all).save if all.count == 2
Account::Transfer.new(transactions: all).save if all.count == 2
end

def daily_totals(transactions, period: Period.last_30_days, currency: Current.family.currency)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_with model: transfer do |f| %>
<%= form_with model: transfer, data: { turbo_frame: "_top" } do |f| %>
<section>
<fieldset class="bg-gray-50 rounded-lg p-1 grid grid-flow-col justify-stretch gap-x-2">
<%= link_to new_transaction_path(nature: "expense"), data: { turbo_frame: :modal }, class: "flex px-4 py-1 rounded-lg items-center space-x-2 justify-center text-gray-400" do %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<details class="group flex items-center text-gray-900 p-4 text-sm font-medium">
<summary class="flex items-center justify-between">
<div class="flex items-center gap-4">
<%= button_to transfer_path(transfer),
<%= button_to account_transfer_path(transfer),
method: :delete,
class: "flex items-center group/transfer",
data: {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/views/accounts/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<%= f.hidden_field :accountable_type %>
<%= f.text_field :name, placeholder: t(".name.placeholder"), required: "required", label: t(".name.label"), autofocus: true %>
<%= f.collection_select :institution_id, Current.family.institutions.alphabetically, :id, :name, { include_blank: t(".ungrouped"), label: t(".institution") } %>
<%= render "accounts/account/#{permitted_accountable_partial(@account.accountable_type)}", f: f %>
<%= render "accounts/accountables/#{permitted_accountable_partial(@account.accountable_type)}", f: f %>
<%= f.money_field :balance_money, label: t(".balance"), required: "required" %>

<div>
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<%= t(".no_categories") %>
</div>
<% @categories.each do |category| %>
<%= render partial: "categories/dropdowns/row", locals: { category: } %>
<%= render partial: "category/dropdowns/row", locals: { category: } %>
<% end %>
</div>
<hr>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/views/transactions/_date_group.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%# locals: (date:, group:) %>
<div class="bg-gray-25 rounded-xl p-1 w-full" data-bulk-select-target="group">
<div id="date-group-<%= date %>" class="bg-gray-25 rounded-xl p-1 w-full" data-bulk-select-target="group">
<div class="py-2 px-4 flex items-center justify-between font-medium text-xs text-gray-500">
<div class="flex pl-0.5 items-center gap-4">
<%= check_box_tag "#{date}_transactions_selection",
Expand Down
2 changes: 1 addition & 1 deletion app/views/transactions/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<fieldset class="bg-gray-50 rounded-lg p-1 grid grid-flow-col justify-stretch gap-x-2">
<%= radio_tab_tag form: f, name: :nature, value: :expense, label: t(".expense"), icon: "minus-circle", checked: params[:nature] == "expense" || params[:nature].nil? %>
<%= radio_tab_tag form: f, name: :nature, value: :income, label: t(".income"), icon: "plus-circle", checked: params[:nature] == "income" %>
<%= link_to new_transfer_path, data: { turbo_frame: :modal }, class: "flex px-4 py-1 rounded-lg items-center space-x-2 justify-center text-gray-400 group-has-[:checked]:bg-white group-has-[:checked]:text-gray-800 group-has-[:checked]:shadow-sm" do %>
<%= link_to new_account_transfer_path, data: { turbo_frame: :modal }, class: "flex px-4 py-1 rounded-lg items-center space-x-2 justify-center text-gray-400 group-has-[:checked]:bg-white group-has-[:checked]:text-gray-800 group-has-[:checked]:shadow-sm" do %>
<%= lucide_icon "arrow-right-left", class: "w-5 h-5" %>
<%= tag.span t(".transfer") %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/transactions/_selection_bar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
accept: t(".mark_transfers_confirm"),
}
} do |f| %>
<button type="button" data-bulk-select-scope-param="bulk_update" data-action="bulk-select#submitBulkRequest" class="p-1.5 group hover:bg-gray-700 flex items-center justify-center rounded-md" title="Mark as transfer">
<button id="bulk-transfer-btn" type="button" data-bulk-select-scope-param="bulk_update" data-action="bulk-select#submitBulkRequest" class="p-1.5 group hover:bg-gray-700 flex items-center justify-center rounded-md" title="Mark as transfer">
<%= lucide_icon "arrow-right-left", class: "w-5 group-hover:text-white" %>
</button>
<% end %>
Expand Down
28 changes: 28 additions & 0 deletions config/locales/views/account/transfers/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
en:
account:
transfers:
create:
success: Transfer created
destroy:
success: Transfer removed
form:
amount: Amount
date: Date
description: Description
description_placeholder: Transfer from Checking to Savings
expense: Expense
from: From
income: Income
select_account: Select account
submit: Create transfer
to: To
transfer: Transfer
new:
title: New transfer
transfer:
remove_body: This will NOT delete the underlying transactions. It will just
remove the transfer.
remove_confirm: Confirm
remove_title: Remove transfer?
transfer_name: Transfer from %{from_account} to %{to_account}
21 changes: 0 additions & 21 deletions config/locales/views/categories/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@ en:
categories:
create:
success: New transaction category created successfully
deletions:
create:
success: Transaction category deleted successfully
new:
category: Category
delete_and_leave_uncategorized: Delete "%{category_name}" and leave uncategorized
delete_and_recategorize: Delete "%{category_name}" and assign new category
delete_category: Delete category?
explanation: By deleting this category, every transaction that has the "%{category_name}"
category will be uncategorized. Instead of leaving them uncategorized, you
can also assign a new category below.
replacement_category_prompt: Select category
dropdowns:
row:
delete: Delete category
edit: Edit category
show:
add_new: Add new
clear: Clear
no_categories: No categories found
search_placeholder: Search
edit:
edit: Edit category
form:
Expand Down
15 changes: 15 additions & 0 deletions config/locales/views/category/deletions/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
en:
category:
deletions:
create:
success: Transaction category deleted successfully
new:
category: Category
delete_and_leave_uncategorized: Delete "%{category_name}" and leave uncategorized
delete_and_recategorize: Delete "%{category_name}" and assign new category
delete_category: Delete category?
explanation: By deleting this category, every transaction that has the "%{category_name}"
category will be uncategorized. Instead of leaving them uncategorized, you
can also assign a new category below.
replacement_category_prompt: Select category
12 changes: 12 additions & 0 deletions config/locales/views/category/dropdowns/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
en:
category:
dropdowns:
row:
delete: Delete category
edit: Edit category
show:
add_new: Add new
clear: Clear
no_categories: No categories found
search_placeholder: Search
15 changes: 15 additions & 0 deletions config/locales/views/tag/deletions/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
en:
tag:
deletions:
create:
deleted: Tag deleted
new:
delete_and_leave_uncategorized: Delete "%{tag_name}"
delete_and_recategorize: Delete "%{tag_name}" and assign new tag
delete_tag: Delete tag?
explanation: "%{tag_name} will be removed from transactions and other taggable
entities. Instead of leaving them untagged, you can also assign a new tag
below."
replacement_tag_prompt: Select tag
tag: Tag
12 changes: 0 additions & 12 deletions config/locales/views/tags/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ en:
tags:
create:
created: Tag created
deletions:
create:
deleted: Tag deleted
new:
delete_and_leave_uncategorized: Delete "%{tag_name}"
delete_and_recategorize: Delete "%{tag_name}" and assign new tag
delete_tag: Delete tag?
explanation: "%{tag_name} will be removed from transactions and other taggable
entities. Instead of leaving them untagged, you can also assign a new tag
below."
replacement_tag_prompt: Select tag
tag: Tag
edit:
edit: Edit tag
form:
Expand Down
27 changes: 0 additions & 27 deletions config/locales/views/transfers/en.yml

This file was deleted.

34 changes: 17 additions & 17 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,39 @@
end

resources :tags, except: %i[ show destroy ] do
resources :deletions, only: %i[ new create ], module: :tags
resources :deletions, only: %i[ new create ], module: :tag
end

namespace :category do
resource :dropdown, only: :show
end

resources :categories do
resources :deletions, only: %i[ new create ], module: :categories
collection do
resource :dropdown, only: :show, module: :categories, as: :category_dropdown
end
resources :deletions, only: %i[ new create ], module: :category
end

resources :merchants, only: %i[ index new create edit update destroy ]

namespace :transaction do
resources :rows, only: %i[ show update ]
resources :rules, only: %i[ index ]
end

resources :transactions do
collection do
post "bulk_delete"
get "bulk_edit"
post "bulk_update"
post "mark_transfers"
post "unmark_transfers"

scope module: :transactions, as: :transaction do
resources :rows, only: %i[ show update ]
resources :rules, only: %i[ index ]
end
end
end

resources :transfers, only: %i[ new create destroy ]
namespace :account do
resources :transfers, only: %i[ new create destroy ]
end

resources :accounts, shallow: true do
resources :accounts do
collection do
get :summary
get :list
Expand All @@ -77,11 +80,8 @@
post :sync
end

scope module: :accounts do
resource :logo, only: :show
end

resources :valuations
resource :logo, only: :show, module: :account
resources :valuations, shallow: true
end

resources :institutions, except: %i[ index show ]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240620125026_rename_transfer_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameTransferTable < ActiveRecord::Migration[7.2]
def change
rename_table :transfers, :account_transfers
end
end
Loading

0 comments on commit bddaab0

Please sign in to comment.