mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 15:49:39 +02:00
Vehicle view (#1117)
This commit is contained in:
parent
e856691c86
commit
359bceb58e
15 changed files with 259 additions and 8 deletions
42
app/controllers/vehicles_controller.rb
Normal file
42
app/controllers/vehicles_controller.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
class VehiclesController < 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!(account_params)
|
||||
@account.sync_later
|
||||
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, :start_date, :start_balance, :currency, :accountable_type,
|
||||
accountable_attributes: [
|
||||
:id,
|
||||
:make,
|
||||
:model,
|
||||
:year,
|
||||
:mileage_value,
|
||||
:mileage_unit
|
||||
]
|
||||
)
|
||||
end
|
||||
end
|
|
@ -29,6 +29,8 @@ module AccountsHelper
|
|||
case account.accountable_type
|
||||
when "Property"
|
||||
properties_path
|
||||
when "Vehicle"
|
||||
vehicles_path
|
||||
else
|
||||
accounts_path
|
||||
end
|
||||
|
@ -38,6 +40,8 @@ module AccountsHelper
|
|||
case account.accountable_type
|
||||
when "Property"
|
||||
property_path(account)
|
||||
when "Vehicle"
|
||||
vehicle_path(account)
|
||||
else
|
||||
account_path(account)
|
||||
end
|
||||
|
@ -51,7 +55,7 @@ module AccountsHelper
|
|||
transactions_tab = { key: "transactions", label: t("accounts.show.transactions"), path: account_path(account, tab: "transactions"), route: account_transactions_path(account) }
|
||||
trades_tab = { key: "trades", label: t("accounts.show.trades"), path: account_path(account, tab: "trades"), route: account_trades_path(account) }
|
||||
|
||||
return [ overview_tab, value_tab ] if account.property?
|
||||
return [ overview_tab, value_tab ] if account.property? || account.vehicle?
|
||||
return [ holdings_tab, cash_tab, trades_tab ] if account.investment?
|
||||
|
||||
[ value_tab, transactions_tab ]
|
||||
|
|
2
app/helpers/vehicles_helper.rb
Normal file
2
app/helpers/vehicles_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module VehiclesHelper
|
||||
end
|
|
@ -3,7 +3,7 @@ class Measurement
|
|||
|
||||
attr_reader :value, :unit
|
||||
|
||||
VALID_UNITS = %w[sqft sqm]
|
||||
VALID_UNITS = %w[sqft sqm mi km]
|
||||
|
||||
validates :unit, inclusion: { in: VALID_UNITS }
|
||||
validates :value, presence: true
|
||||
|
|
|
@ -15,10 +15,6 @@ class Property < ApplicationRecord
|
|||
first_valuation_amount
|
||||
end
|
||||
|
||||
def equity_value
|
||||
account.balance_money
|
||||
end
|
||||
|
||||
def trend
|
||||
TimeSeries::Trend.new(current: account.balance_money, previous: first_valuation_amount)
|
||||
end
|
||||
|
|
|
@ -1,3 +1,22 @@
|
|||
class Vehicle < ApplicationRecord
|
||||
include Accountable
|
||||
|
||||
attribute :mileage_unit, :string, default: "mi"
|
||||
|
||||
def mileage
|
||||
Measurement.new(mileage_value, mileage_unit) if mileage_value.present?
|
||||
end
|
||||
|
||||
def purchase_price
|
||||
first_valuation_amount
|
||||
end
|
||||
|
||||
def trend
|
||||
TimeSeries::Trend.new(current: account.balance_money, previous: first_valuation_amount)
|
||||
end
|
||||
|
||||
private
|
||||
def first_valuation_amount
|
||||
account.entries.account_valuations.order(:date).first&.amount_money || account.balance_money
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
<%# locals: (f:) %>
|
||||
|
||||
<div>
|
||||
<hr class="my-4">
|
||||
|
||||
<div class="space-y-2">
|
||||
<%= f.fields_for :accountable do |vehicle_form| %>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<%= vehicle_form.text_field :make, label: t(".make"), placeholder: t(".make_placeholder") %>
|
||||
<%= vehicle_form.text_field :model, label: t(".model"), placeholder: t(".model_placeholder") %>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<%= vehicle_form.text_field :year, label: t(".year"), placeholder: t(".year_placeholder") %>
|
||||
<%= vehicle_form.text_field :mileage_value, label: t(".mileage"), placeholder: t(".mileage_placeholder") %>
|
||||
<%= vehicle_form.select :mileage_unit,
|
||||
[["Miles", "mi"], ["Kilometers", "km"]],
|
||||
{ label: t(".mileage_unit") } %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
49
app/views/accounts/accountables/vehicle/_overview.html.erb
Normal file
49
app/views/accounts/accountables/vehicle/_overview.html.erb
Normal file
|
@ -0,0 +1,49 @@
|
|||
<%# locals: (account:) %>
|
||||
|
||||
<div class="grid grid-cols-3 gap-2">
|
||||
<div class="rounded-xl bg-white shadow-xs border border-alpha-black-25 p-4">
|
||||
<h4 class="text-gray-500 text-sm"><%= t(".make_model") %></h4>
|
||||
<p class="text-xl font-medium text-gray-900">
|
||||
<%= [account.vehicle.make, account.vehicle.model].compact.join(" ").presence || t(".unknown") %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl bg-white shadow-xs border border-alpha-black-25 p-4">
|
||||
<h4 class="text-gray-500 text-sm"><%= t(".year") %></h4>
|
||||
<p class="text-xl font-medium text-gray-900">
|
||||
<%= account.vehicle.year || t(".unknown") %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl bg-white shadow-xs border border-alpha-black-25 p-4">
|
||||
<h4 class="text-gray-500 text-sm flex items-center gap-1"><%= t(".mileage") %></h4>
|
||||
<p class="text-xl font-medium text-gray-900">
|
||||
<%= account.vehicle.mileage || t(".unknown") %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl bg-white shadow-xs border border-alpha-black-25 p-4">
|
||||
<h4 class="text-gray-500 text-sm"><%= t(".purchase_price") %></h4>
|
||||
<p class="text-xl font-medium text-gray-900">
|
||||
<%= format_money account.vehicle.purchase_price %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl bg-white shadow-xs border border-alpha-black-25 p-4">
|
||||
<h4 class="text-gray-500 text-sm"><%= t(".current_price") %></h4>
|
||||
<p class="text-xl font-medium text-gray-900">
|
||||
<%= format_money account.balance_money %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl bg-white shadow-xs border border-alpha-black-25 p-4">
|
||||
<h4 class="text-gray-500 text-sm"><%= t(".trend") %></h4>
|
||||
<div class="flex items-center gap-1" style="color: <%= account.vehicle.trend.color %>">
|
||||
<p class="text-xl font-medium">
|
||||
<%= account.vehicle.trend.value %>
|
||||
</p>
|
||||
|
||||
<p>(<%= account.vehicle.trend.percent %>%)</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
Loading…
Add table
Add a link
Reference in a new issue