diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb
index 5e7322ad..36e45b01 100644
--- a/app/controllers/transactions_controller.rb
+++ b/app/controllers/transactions_controller.rb
@@ -49,16 +49,20 @@ class TransactionsController < ApplicationController
end
def new
- @transaction = Transaction.new
+ @transaction = Transaction.new.tap do |txn|
+ if params[:account_id]
+ txn.account = Current.family.accounts.find(params[:account_id])
+ end
+ end
end
def edit
end
def create
- account = Current.family.accounts.find(params[:transaction][:account_id])
-
- @transaction = account.transactions.build(transaction_params)
+ @transaction = Current.family.accounts
+ .find(params[:transaction][:account_id])
+ .transactions.build(transaction_params.merge(amount: amount))
respond_to do |format|
if @transaction.save
@@ -118,6 +122,18 @@ class TransactionsController < ApplicationController
@transaction = Transaction.find(params[:id])
end
+ def amount
+ if nature.income?
+ transaction_params[:amount].to_d * -1
+ else
+ transaction_params[:amount].to_d
+ end
+ end
+
+ def nature
+ params[:transaction][:nature].to_s.inquiry
+ end
+
# Only allow a list of trusted parameters through.
def transaction_params
params.require(:transaction).permit(:name, :date, :amount, :currency, :notes, :excluded, :category_id)
diff --git a/app/helpers/forms_helper.rb b/app/helpers/forms_helper.rb
index ac138976..ea7f24db 100644
--- a/app/helpers/forms_helper.rb
+++ b/app/helpers/forms_helper.rb
@@ -2,4 +2,19 @@ module FormsHelper
def form_field_tag(&)
tag.div class: "form-field", &
end
+
+ def radio_tab_tag(form:, name:, value:, label:, icon:, checked: false, disabled: false)
+ form.label name, for: form.field_id(name, value), class: "group has-[:disabled]:cursor-not-allowed" do
+ concat radio_tab_contents(label:, icon:)
+ concat form.radio_button(name, value, checked:, disabled:, class: "hidden")
+ end
+ end
+
+ private
+ def radio_tab_contents(label:, icon:)
+ tag.div(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
+ concat lucide_icon(icon, class: "w-5 h-5")
+ concat tag.span(label, class: "group-has-[:checked]:font-semibold")
+ end
+ end
end
diff --git a/app/models/account.rb b/app/models/account.rb
index 550e0279..b1d6aec1 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -17,6 +17,7 @@ class Account < ApplicationRecord
scope :active, -> { where(is_active: true) }
scope :assets, -> { where(classification: "asset") }
scope :liabilities, -> { where(classification: "liability") }
+ scope :alphabetically, -> { order(:name) }
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
diff --git a/app/models/transaction/category.rb b/app/models/transaction/category.rb
index 614ec82b..97439f68 100644
--- a/app/models/transaction/category.rb
+++ b/app/models/transaction/category.rb
@@ -6,6 +6,8 @@ class Transaction::Category < ApplicationRecord
before_update :clear_internal_category, if: :name_changed?
+ scope :alphabetically, -> { order(:name) }
+
COLORS = %w[#e99537 #4da568 #6471eb #db5a54 #df4e92 #c44fe9 #eb5429 #61c9ea #805dee #6ad28a]
UNCATEGORIZED_COLOR = "#737373"
diff --git a/app/views/accounts/_transactions.html.erb b/app/views/accounts/_transactions.html.erb
index cc050643..ad040ff7 100644
--- a/app/views/accounts/_transactions.html.erb
+++ b/app/views/accounts/_transactions.html.erb
@@ -1,8 +1,8 @@
-<%# locals: (transactions:)%>
+<%# locals: (account:, transactions:)%>
Transactions
- <%= link_to new_transaction_path, class: "flex gap-1 font-medium items-center bg-gray-50 text-gray-900 p-2 rounded-lg" do %>
+ <%= link_to new_transaction_path(account_id: account), class: "flex gap-1 font-medium items-center bg-gray-50 text-gray-900 p-2 rounded-lg", data: { turbo_frame: :modal } do %>
<%= lucide_icon("plus", class: "w-5 h-5 text-gray-900") %>
New transaction
<% end %>
diff --git a/app/views/accounts/show.html.erb b/app/views/accounts/show.html.erb
index 48be2c56..7ef90757 100644
--- a/app/views/accounts/show.html.erb
+++ b/app/views/accounts/show.html.erb
@@ -71,7 +71,7 @@
<%= render partial: "accounts/account_history", locals: { account: @account, valuations: @valuation_series } %>
- <%= render partial: "accounts/transactions", locals: { transactions: @account.transactions.order(date: :desc) } %>
+ <%= render partial: "accounts/transactions", locals: { account: @account, transactions: @account.transactions.order(date: :desc) } %>
diff --git a/app/views/shared/_modal.html.erb b/app/views/shared/_modal.html.erb
index 511991e0..75174faf 100644
--- a/app/views/shared/_modal.html.erb
+++ b/app/views/shared/_modal.html.erb
@@ -1,6 +1,6 @@
<%# locals: (content:) -%>
<%= turbo_frame_tag "modal" do %>
-