forked from maybe-finance/maybe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Account namespace updates: part 5 (valuations) (maybe-finance#901)
* Move Valuation to Account namespace * Move account history to controller * Clean up valuation controller and views * Translations and cleanup * Remove unused scopes and methods * Pass brakeman
- Loading branch information
Showing
45 changed files
with
478 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class Account::ValuationsController < ApplicationController | ||
before_action :set_account | ||
before_action :set_valuation, only: %i[ show edit update destroy ] | ||
|
||
def new | ||
@valuation = @account.valuations.new | ||
end | ||
|
||
def show | ||
end | ||
|
||
def create | ||
@valuation = @account.valuations.build(valuation_params) | ||
|
||
if @valuation.save | ||
@valuation.sync_account_later | ||
redirect_to account_path(@account), notice: "Valuation created" | ||
else | ||
# 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] = @valuation.errors.full_messages.to_sentence | ||
redirect_to account_path(@account) | ||
end | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
if @valuation.update(valuation_params) | ||
@valuation.sync_account_later | ||
redirect_to account_path(@account), notice: t(".success") | ||
else | ||
# 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] = @valuation.errors.full_messages.to_sentence | ||
redirect_to account_path(@account) | ||
end | ||
end | ||
|
||
def destroy | ||
@valuation.destroy! | ||
@valuation.sync_account_later | ||
|
||
redirect_to account_path(@account), notice: t(".success") | ||
end | ||
|
||
private | ||
|
||
def set_account | ||
@account = Current.family.accounts.find(params[:account_id]) | ||
end | ||
|
||
def set_valuation | ||
@valuation = @account.valuations.find(params[:id]) | ||
end | ||
|
||
def valuation_params | ||
params.require(:account_valuation).permit(:date, :value, :currency) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module Account::TransfersHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Account::ValuationsHelper | ||
def valuation_icon(valuation) | ||
if valuation.first_of_series? | ||
"keyboard" | ||
elsif valuation.trend.direction.up? | ||
"arrow-up" | ||
elsif valuation.trend.direction.down? | ||
"arrow-down" | ||
else | ||
"minus" | ||
end | ||
end | ||
|
||
def valuation_style(valuation) | ||
color = valuation.first_of_series? ? "#D444F1" : valuation.trend.color | ||
|
||
<<-STYLE.strip | ||
background-color: color-mix(in srgb, #{color} 5%, white); | ||
border-color: color-mix(in srgb, #{color} 10%, white); | ||
color: #{color}; | ||
STYLE | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class Account::Valuation < ApplicationRecord | ||
include Monetizable | ||
|
||
monetize :value | ||
|
||
belongs_to :account | ||
|
||
validates :account, :date, :value, presence: true | ||
validates :date, uniqueness: { scope: :account_id } | ||
|
||
scope :chronological, -> { order(:date) } | ||
scope :reverse_chronological, -> { order(date: :desc) } | ||
|
||
def trend | ||
@trend ||= create_trend | ||
end | ||
|
||
def first_of_series? | ||
account.valuations.chronological.limit(1).pluck(:date).first == self.date | ||
end | ||
|
||
def last_of_series? | ||
account.valuations.reverse_chronological.limit(1).pluck(:date).first == self.date | ||
end | ||
|
||
def sync_account_later | ||
if destroyed? | ||
sync_start_date = previous_valuation&.date | ||
else | ||
sync_start_date = [ date_previously_was, date ].compact.min | ||
end | ||
|
||
account.sync_later(sync_start_date) | ||
end | ||
|
||
private | ||
|
||
def previous_valuation | ||
@previous_valuation ||= self.account | ||
.valuations | ||
.where("date < ?", date) | ||
.order(date: :desc) | ||
.first | ||
end | ||
|
||
def create_trend | ||
TimeSeries::Trend.new \ | ||
current: self.value, | ||
previous: previous_valuation&.value, | ||
favorable_direction: account.favorable_direction | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<%# locals: (valuation:) %> | ||
<%= form_with model: valuation, | ||
data: { turbo_frame: "_top" }, | ||
url: valuation.new_record? ? account_valuations_path(valuation.account) : account_valuation_path(valuation.account, valuation), | ||
builder: ActionView::Helpers::FormBuilder do |f| %> | ||
<div class="grid grid-cols-10 p-4 items-center"> | ||
<div class="col-span-7 flex items-center gap-4"> | ||
<div class="w-8 h-8 rounded-full p-1.5 flex items-center justify-center bg-gray-500/5"> | ||
<%= lucide_icon("pencil-line", class: "w-4 h-4 text-gray-500") %> | ||
</div> | ||
<div class="w-full flex items-center justify-between gap-2"> | ||
<%= f.date_field :date, required: "required", max: Date.today, class: "border border-alpha-black-200 bg-white rounded-lg shadow-xs min-w-[200px] px-3 py-1.5 text-gray-900 text-sm" %> | ||
<%= f.number_field :value, required: "required", placeholder: "0.00", step: "0.01", class: "bg-white border border-alpha-black-200 rounded-lg shadow-xs text-gray-900 text-sm px-3 py-1.5 text-right" %> | ||
<%= f.hidden_field :currency, value: valuation.account.currency %> | ||
</div> | ||
</div> | ||
|
||
<div class="col-span-3 flex gap-2 justify-end items-center"> | ||
<%= link_to t(".cancel"), account_valuations_path(valuation.account), class: "text-sm text-gray-900 hover:text-gray-800 font-medium px-3 py-1.5" %> | ||
<%= f.submit class: "bg-gray-50 rounded-lg font-medium px-3 py-1.5 cursor-pointer hover:bg-gray-100 text-sm" %> | ||
</div> | ||
</div> | ||
<% end %> |
Oops, something went wrong.