diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 1e7634c7..1554ab84 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -2,34 +2,35 @@ class AccountsController < ApplicationController before_action :authenticate_user! def new - end - - def new_bank - @account = Depository.new - end - - def new_credit - @account = Credit.new + if params[:type].blank? || Account.accountable_types.include?("Account::#{params[:type]}") + @account = if params[:type].blank? + Account.new + else + Account.new(accountable_type: "Account::#{params[:type]}") + end + else + head :not_found + end end def show end def create - @account = account_type_class.new(account_params) - @account.family = current_family + @account = Account.new(account_params.merge(family: current_family)) + @account.accountable = account_params[:accountable_type].constantize.new if @account.save - redirect_to root_path + redirect_to accounts_path, notice: "New account created successfully" else - render :new + render "new", status: :unprocessable_entity end end private def account_params - params.require(:account).permit(:name, :balance, :type, :subtype) + params.require(:account).permit(:name, :accountable_type, :balance, :subtype) end def account_type_class diff --git a/app/models/account.rb b/app/models/account.rb index b3d34d28..63bfe8d3 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,9 +1,14 @@ class Account < ApplicationRecord - # VALID_ACCOUNT_TYPES = %w[Investment Depository Credit Loan Property Vehicle OtherAsset OtherLiability].freeze - belongs_to :family - delegated_type :accountable, types: %w[ Credit Depository Investment Loan OtherAsset OtherLiability Property Vehicle], dependent: :destroy + delegated_type :accountable, types: %w[ Account::Credit Account::Depository Account::Investment Account::Loan Account::OtherAsset Account::OtherLiability Account::Property Account::Vehicle], dependent: :destroy - scope :depository, -> { where(type: "Depository") } + delegate :icon, :type_name, :color, to: :accountable + + # Class method to get a representative instance of each accountable type + def self.accountable_type_instances + accountable_types.map do |type| + type.constantize.new + end + end end diff --git a/app/models/account/credit.rb b/app/models/account/credit.rb index 06e91afc..8dbdc745 100644 --- a/app/models/account/credit.rb +++ b/app/models/account/credit.rb @@ -1,3 +1,18 @@ class Account::Credit < ApplicationRecord include Accountable + + def icon + "icon-credit-card.svg" + end + + def type_name + "Credit Card" + end + + def color + { + background: "bg-[#E6F6FA]", + text: "text-[#189FC7]" + } + end end diff --git a/app/models/account/depository.rb b/app/models/account/depository.rb index b7d4cb30..5f4c9fd6 100644 --- a/app/models/account/depository.rb +++ b/app/models/account/depository.rb @@ -1,3 +1,18 @@ class Account::Depository < ApplicationRecord include Accountable + + def icon + "icon-bank-accounts.svg" + end + + def type_name + "Bank Accounts" + end + + def color + { + background: "bg-[#EAF4FF]", + text: "text-[#3492FB]" + } + end end diff --git a/app/models/account/investment.rb b/app/models/account/investment.rb index 57bdfcf8..ae3da945 100644 --- a/app/models/account/investment.rb +++ b/app/models/account/investment.rb @@ -1,3 +1,18 @@ class Account::Investment < ApplicationRecord include Accountable + + def icon + "icon-bank-accounts.svg" + end + + def type_name + "Investments" + end + + def color + { + background: "bg-[#EDF7F4]", + text: "text-[#1BD5A1]" + } + end end diff --git a/app/models/account/loan.rb b/app/models/account/loan.rb index 71c72d92..ba8cd645 100644 --- a/app/models/account/loan.rb +++ b/app/models/account/loan.rb @@ -1,3 +1,18 @@ class Account::Loan < ApplicationRecord include Accountable + + def icon + "icon-bank-accounts.svg" + end + + def type_name + "Loan" + end + + def color + { + background: "bg-[#EDF7F4]", + text: "text-[#1BD5A1]" + } + end end diff --git a/app/models/account/other_asset.rb b/app/models/account/other_asset.rb index b4f638da..e00c9f51 100644 --- a/app/models/account/other_asset.rb +++ b/app/models/account/other_asset.rb @@ -1,3 +1,18 @@ class Account::OtherAsset < ApplicationRecord include Accountable + + def icon + "icon-bank-accounts.svg" + end + + def type_name + "Other Asset" + end + + def color + { + background: "bg-[#EDF7F4]", + text: "text-[#1BD5A1]" + } + end end diff --git a/app/models/account/other_liability.rb b/app/models/account/other_liability.rb index 2467c08d..c9c51ead 100644 --- a/app/models/account/other_liability.rb +++ b/app/models/account/other_liability.rb @@ -1,3 +1,18 @@ class Account::OtherLiability < ApplicationRecord include Accountable + + def icon + "icon-bank-accounts.svg" + end + + def type_name + "Other Liability" + end + + def color + { + background: "bg-[#EDF7F4]", + text: "text-[#1BD5A1]" + } + end end diff --git a/app/models/account/property.rb b/app/models/account/property.rb index 0088dfcd..105ac600 100644 --- a/app/models/account/property.rb +++ b/app/models/account/property.rb @@ -1,3 +1,18 @@ class Account::Property < ApplicationRecord include Accountable + + def icon + "icon-real-estate.svg" + end + + def type_name + "Real Estate" + end + + def color + { + background: "bg-[#FEF0F7]", + text: "text-[#F03695]" + } + end end diff --git a/app/models/account/vehicle.rb b/app/models/account/vehicle.rb index fd640f2a..6a43f4a6 100644 --- a/app/models/account/vehicle.rb +++ b/app/models/account/vehicle.rb @@ -1,3 +1,18 @@ class Account::Vehicle < ApplicationRecord include Accountable + + def icon + "icon-bank-accounts.svg" + end + + def type_name + "Vehicle" + end + + def color + { + background: "bg-[#EDF7F4]", + text: "text-[#1BD5A1]" + } + end end diff --git a/app/models/depository.rb b/app/models/depository.rb deleted file mode 100644 index bacbb9e6..00000000 --- a/app/models/depository.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Depository < Account -end diff --git a/app/views/accounts/_account_type.html.erb b/app/views/accounts/_account_type.html.erb new file mode 100644 index 00000000..6d654ce9 --- /dev/null +++ b/app/views/accounts/_account_type.html.erb @@ -0,0 +1,9 @@ +
+ <%= number_to_currency account.balance %> +
+