diff --git a/app/assets/stylesheets/application.tailwind.css b/app/assets/stylesheets/application.tailwind.css index 8529f781..d09dd635 100644 --- a/app/assets/stylesheets/application.tailwind.css +++ b/app/assets/stylesheets/application.tailwind.css @@ -49,6 +49,10 @@ .form-field__submit { @apply w-full p-3 text-center text-white bg-black rounded-lg hover:bg-gray-700; } + + input:checked + label + .toggle-switch-dot { + transform: translateX(100%); + } } /* Small, single purpose classes that should take precedence over other styles */ diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 7a726dc6..392c8bd5 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -15,6 +15,30 @@ class AccountsController < ApplicationController @valuation_series = @account.valuations.to_series(@account) end + def edit + end + + def update + @account = Current.family.accounts.find(params[:id]) + + if @account.update(account_params.except(:accountable_type)) + + @account.sync_later if account_params[:is_active] == "1" + + respond_to do |format| + format.html { redirect_to accounts_path, notice: t(".success") } + format.turbo_stream do + render turbo_stream: [ + turbo_stream.append("notification-tray", partial: "shared/notification", locals: { type: "success", content: t(".success") }), + turbo_stream.replace("account_#{@account.id}", partial: "accounts/account", locals: { account: @account }) + ] + end + end + else + render "edit", status: :unprocessable_entity + end + end + def create @account = Current.family.accounts.build(account_params.except(:accountable_type)) @account.accountable = Accountable.from_type(account_params[:accountable_type])&.new @@ -29,6 +53,6 @@ class AccountsController < ApplicationController private def account_params - params.require(:account).permit(:name, :accountable_type, :balance, :currency, :subtype) + params.require(:account).permit(:name, :accountable_type, :balance, :currency, :subtype, :is_active) end end diff --git a/app/models/account.rb b/app/models/account.rb index 3f4442a9..0a280f77 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -7,6 +7,8 @@ class Account < ApplicationRecord has_many :valuations has_many :transactions + scope :active, -> { where(is_active: true) } + delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy before_create :check_currency @@ -17,10 +19,15 @@ class Account < ApplicationRecord Trend.new(current: last.balance, previous: first.balance, type: classification) end + def self.by_provider + # TODO: When 3rd party providers are supported, dynamically load all providers and their accounts + [ { name: "Manual accounts", accounts: all.order(balance: :desc).group_by(&:accountable_type) } ] + end + # TODO: We will need a better way to encapsulate large queries & transformation logic, but leaving all in one spot until # we have a better understanding of the requirements def self.by_group(period = Period.all) - ranked_balances_cte = joins(:balances) + ranked_balances_cte = active.joins(:balances) .select(" account_balances.account_id, account_balances.balance, diff --git a/app/models/family.rb b/app/models/family.rb index 32261214..e514a783 100644 --- a/app/models/family.rb +++ b/app/models/family.rb @@ -4,15 +4,15 @@ class Family < ApplicationRecord has_many :transactions, through: :accounts def net_worth - accounts.sum("CASE WHEN classification = 'asset' THEN balance ELSE -balance END") + accounts.active.sum("CASE WHEN classification = 'asset' THEN balance ELSE -balance END") end def assets - accounts.where(classification: "asset").sum(:balance) + accounts.active.where(classification: "asset").sum(:balance) end def liabilities - accounts.where(classification: "liability").sum(:balance) + accounts.active.where(classification: "liability").sum(:balance) end def net_worth_series(period = nil) diff --git a/app/views/accounts/_account.html.erb b/app/views/accounts/_account.html.erb new file mode 100644 index 00000000..745c9549 --- /dev/null +++ b/app/views/accounts/_account.html.erb @@ -0,0 +1,23 @@ +<%= turbo_frame_tag dom_id(account) do %> +
"> + <%= account.name %> +
+"> + <%= format_currency account.balance %> +
+ <%= form_with model: account, method: :patch, html: { class: "flex items-center", data: { turbo_frame: "_top" } } do |form| %> +No accounts yet
+Add an account either via connection, importing or entering manually.
+ <%= link_to new_account_path, class: "w-fit flex text-white text-sm font-medium items-center gap-1 bg-gray-900 rounded-lg p-2", data: { turbo_frame: "modal" } do %> + <%= lucide_icon("plus", class: "w-5 h-5") %> + <%= t('.new_account') %> + <% end %>- <%= format_currency account.converted_balance %> -
<%= to_accountable_title(Accountable.from_type(group)) %>
+ · +<%= accounts.count %>
+<%= format_currency accounts.sum(&:balance) %>
+