The personal finance app for everyone
Account namespace updates: part 5 (valuations) (#901)
Zach Gollwitzer12380dc8ad68
diff --git a/app/controllers/account/valuations_controller.rb b/app/controllers/account/valuations_controller.rbnew file mode 100644index 00000000..bdaba126--- /dev/null+++ b/app/controllers/account/valuations_controller.rb@@ -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+enddiff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rbindex a0576b3a..4aa41e54 100644--- a/app/controllers/accounts_controller.rb+++ b/app/controllers/accounts_controller.rb@@ -35,7 +35,6 @@ class AccountsController < ApplicationControllerdef show@balance_series = @account.series(period: @period)- @valuation_series = @account.valuations.to_seriesenddef editdiff --git a/app/controllers/valuations_controller.rb b/app/controllers/valuations_controller.rbdeleted file mode 100644index 1c2979ea..00000000--- a/app/controllers/valuations_controller.rb+++ /dev/null@@ -1,70 +0,0 @@-class ValuationsController < ApplicationController- before_action :set_valuation, only: %i[ edit update destroy ]- def create- @account = Current.family.accounts.find(params[:account_id])-- # TODO: placeholder logic until we have a better abstraction for trends- @valuation = @account.valuations.new(valuation_params.merge(currency: @account.currency))- if @valuation.save- @valuation.account.sync_later(@valuation.date)-- respond_to do |format|- format.html { redirect_to account_path(@account), notice: "Valuation created" }- format.turbo_stream- end- else- render :new, status: :unprocessable_entity- end- rescue ActiveRecord::RecordNotUnique- flash.now[:error] = "Valuation already exists for this date"- render :new, status: :unprocessable_entity- end-- def show- @valuation = Current.family.accounts.find(params[:account_id]).valuations.find(params[:id])- end-- def edit- end-- def update- sync_start_date = [ @valuation.date, Date.parse(valuation_params[:date]) ].compact.min- if @valuation.update(valuation_params)- @valuation.account.sync_later(sync_start_date)-- redirect_to account_path(@valuation.account), notice: "Valuation updated"- else- render :edit, status: :unprocessable_entity- end- rescue ActiveRecord::RecordNotUnique- flash.now[:error] = "Valuation already exists for this date"- render :edit, status: :unprocessable_entity- end-- def destroy- @account = @valuation.account- sync_start_date = @account.valuations.where("date < ?", @valuation.date).order(date: :desc).first&.date- @valuation.destroy!- @account.sync_later(sync_start_date)-- respond_to do |format|- format.html { redirect_to account_path(@account), notice: "Valuation deleted" }- format.turbo_stream- end- end-- def new- @account = Current.family.accounts.find(params[:account_id])- @valuation = @account.valuations.new- end-- private- # Use callbacks to share common setup or constraints between actions.- def set_valuation- @valuation = Valuation.find(params[:id])- end-- def valuation_params- params.require(:valuation).permit(:date, :value)- end-enddiff --git a/app/helpers/account/transfers_helper.rb b/app/helpers/account/transfers_helper.rbnew file mode 100644index 00000000..ba7a95ae--- /dev/null+++ b/app/helpers/account/transfers_helper.rb@@ -0,0 +1,2 @@+module Account::TransfersHelper+enddiff --git a/app/helpers/account/valuations_helper.rb b/app/helpers/account/valuations_helper.rbnew file mode 100644index 00000000..e1a93dde--- /dev/null+++ b/app/helpers/account/valuations_helper.rb@@ -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+enddiff --git a/app/helpers/menus_helper.rb b/app/helpers/menus_helper.rbindex 9576d54f..41907fa0 100644--- a/app/helpers/menus_helper.rb+++ b/app/helpers/menus_helper.rb@@ -6,6 +6,23 @@ module MenusHelperendend+ def contextual_menu_modal_action_item(label, url, icon: "pencil-line", turbo_frame: nil)+ link_to url, class: "flex items-center rounded-lg text-gray-900 hover:bg-gray-50 py-2 px-3 gap-2", data: { turbo_frame: } do+ concat(lucide_icon(icon, class: "shrink-0 w-5 h-5 text-gray-500"))+ concat(tag.span(label, class: "text-sm"))+ end+ end++ def contextual_menu_destructive_item(label, url, turbo_confirm: true, turbo_frame: nil)+ button_to url,+ method: :delete,+ class: "flex items-center w-full rounded-lg text-red-500 hover:bg-red-500/5 py-2 px-3 gap-2",+ data: { turbo_confirm: turbo_confirm, turbo_frame: } do+ concat(lucide_icon("trash-2", class: "shrink-0 w-5 h-5"))+ concat(tag.span(label, class: "text-sm"))+ end+ end+privatedef contextual_menu_icontag.button class: "flex hover:bg-gray-100 p-2 rounded", data: { menu_target: "button" } dodiff --git a/app/helpers/transfers_helper.rb b/app/helpers/transfers_helper.rbdeleted file mode 100644index 98355baf..00000000--- a/app/helpers/transfers_helper.rb+++ /dev/null@@ -1,2 +0,0 @@-module TransfersHelper-enddiff --git a/app/helpers/valuations_helper.rb b/app/helpers/valuations_helper.rbdeleted file mode 100644index 9fe5ad7f..00000000--- a/app/helpers/valuations_helper.rb+++ /dev/null@@ -1,2 +0,0 @@-module ValuationsHelper-enddiff --git a/app/models/account.rb b/app/models/account.rbindex dee4cae7..91b7532a 100644--- a/app/models/account.rb+++ b/app/models/account.rb@@ -29,6 +29,10 @@ class Account < ApplicationRecordbalances.where("date <= ?", date).order(date: :desc).first&.balanceend+ def favorable_direction+ classification == "asset" ? "up" : "down"+ end+# e.g. Wise, Revolut accounts that have transactions in multiple currenciesdef multi_currency?currencies = [ valuations.pluck(:currency), transactions.pluck(:currency) ].flatten.uniqdiff --git a/app/models/account/valuation.rb b/app/models/account/valuation.rbnew file mode 100644index 00000000..ddbb519c--- /dev/null+++ b/app/models/account/valuation.rb@@ -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+enddiff --git a/app/models/time_series/trend.rb b/app/models/time_series/trend.rbindex f62090fb..88fdd3a1 100644--- a/app/models/time_series/trend.rb+++ b/app/models/time_series/trend.rb@@ -1,16 +1,15 @@class TimeSeries::Trendinclude ActiveModel::Validations- attr_reader :current, :previous-- delegate :favorable_direction, to: :series+ attr_reader :current, :previous, :favorable_directionvalidate :values_must_be_of_same_type, :values_must_be_of_known_type- def initialize(current:, previous:, series: nil)+ def initialize(current:, previous:, series: nil, favorable_direction: nil)@current = current@previous = previous@series = series+ @favorable_direction = get_favorable_direction(favorable_direction)validate!end@@ -25,6 +24,17 @@ class TimeSeries::Trendend.inquiryend+ def color+ case direction+ when "up"+ favorable_direction.down? ? red_hex : green_hex+ when "down"+ favorable_direction.down? ? green_hex : red_hex+ else+ gray_hex+ end+ end+def valueif previous.nil?current.is_a?(Money) ? Money.new(0) : 0@@ -56,8 +66,21 @@ class TimeSeries::Trendendprivate+attr_reader :series+ def red_hex+ "#F13636" # red-500+ end++ def green_hex+ "#10A861" # green-600+ end++ def gray_hex+ "#737373" # gray-500+ end+def values_must_be_of_same_typeunless current.class == previous.class || [ previous, current ].any?(&:nil?)errors.add :current, "must be of the same type as previous"@@ -90,4 +113,9 @@ class TimeSeries::Trendobjendend++ def get_favorable_direction(favorable_direction)+ direction = favorable_direction.presence || series&.favorable_direction+ (direction.presence_in(TimeSeries::DIRECTIONS) || "up").inquiry+ endenddiff --git a/app/models/valuation.rb b/app/models/valuation.rbdeleted file mode 100644index 73a823e6..00000000--- a/app/models/valuation.rb+++ /dev/null@@ -1,13 +0,0 @@-class Valuation < ApplicationRecord- include Monetizable-- belongs_to :account- validates :account, :date, :value, presence: true- monetize :value-- scope :in_period, ->(period) { period.date_range.nil? ? all : where(date: period.date_range) }-- def self.to_series- TimeSeries.from_collection all, :value_money- end-enddiff --git a/app/views/account/valuations/_form.html.erb b/app/views/account/valuations/_form.html.erbnew file mode 100644index 00000000..6027bcdd--- /dev/null+++ b/app/views/account/valuations/_form.html.erb@@ -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 %>diff --git a/app/views/account/valuations/_valuation.html.erb b/app/views/account/valuations/_valuation.html.erbnew file mode 100644index 00000000..2d496e98--- /dev/null+++ b/app/views/account/valuations/_valuation.html.erb@@ -0,0 +1,50 @@+<%# locals: (valuation:) %>++<%= turbo_frame_tag dom_id(valuation) do %>+ <div class="p-4 grid grid-cols-10 items-center">+ <div class="col-span-5 flex items-center gap-4">+ <%= tag.div class: "w-8 h-8 rounded-full p-1.5 flex items-center justify-center", style: valuation_style(valuation).html_safe do %>+ <%= lucide_icon valuation_icon(valuation), class: "w-4 h-4" %>+ <% end %>++ <div class="text-sm">+ <%= tag.p valuation.date, class: "text-gray-900 font-medium" %>+ <%= tag.p valuation.first_of_series? ? t(".start_balance") : t(".value_update"), class: "text-gray-500" %>+ </div>+ </div>++ <div class="col-span-2 justify-self-end">+ <%= tag.p format_money(valuation.value_money), class: "font-medium text-sm text-gray-900" %>+ </div>++ <div class="col-span-2 justify-self-end font-medium text-sm" style="color: <%= valuation.trend.color %>">+ <% if valuation.trend.direction.flat? %>+ <%= tag.span t(".no_change"), class: "text-gray-500" %>+ <% else %>+ <%= tag.span format_money(valuation.trend.value) %>+ <%= tag.span "(#{valuation.trend.percent}%)" %>+ <% end %>+ </div>++ <div class="col-span-1 justify-self-end">+ <%= contextual_menu do %>+ <div class="w-48 p-1 text-sm leading-6 text-gray-900 bg-white shadow-lg shrink rounded-xl ring-1 ring-gray-900/5">+ <%= contextual_menu_modal_action_item t(".edit_entry"), edit_account_valuation_path(valuation.account, valuation) %>++ <%= contextual_menu_destructive_item t(".delete_entry"),+ account_valuation_path(valuation.account, valuation),+ turbo_frame: "_top",+ turbo_confirm: {+ title: t(".confirm_title"),+ body: t(".confirm_body_html"),+ accept: t(".confirm_accept")+ } %>+ </div>+ <% end %>+ </div>+ </div>++ <% unless valuation.last_of_series? %>+ <div class="h-px bg-alpha-black-50 ml-16 mr-4"></div>+ <% end %>+<% end %>diff --git a/app/views/account/valuations/edit.html.erb b/app/views/account/valuations/edit.html.erbnew file mode 100644index 00000000..afa3877d--- /dev/null+++ b/app/views/account/valuations/edit.html.erb@@ -0,0 +1,3 @@+<%= turbo_frame_tag dom_id(@valuation) do %>+ <%= render "form", valuation: @valuation %>+<% end %>diff --git a/app/views/account/valuations/index.html.erb b/app/views/account/valuations/index.html.erbnew file mode 100644index 00000000..4fa523bb--- /dev/null+++ b/app/views/account/valuations/index.html.erb@@ -0,0 +1,15 @@+<%= turbo_frame_tag dom_id(@account, "valuations") do %>+ <div class="grid grid-cols-10 items-center uppercase text-xs font-medium text-gray-500 px-4 py-2">+ <%= tag.p t(".date"), class: "col-span-5" %>+ <%= tag.p t(".value"), class: "col-span-2 justify-self-end" %>+ <%= tag.p t(".change"), class: "col-span-2 justify-self-end" %>+ <%= tag.div class: "col-span-1" %>+ </div>++ <div class="rounded-lg bg-white border-alpha-black-25 shadow-xs">+ <%= turbo_frame_tag dom_id(Account::Valuation.new) %>+ <% @account.valuations.reverse_chronological.each do |valuation| %>+ <%= render valuation %>+ <% end %>+ </div>+<% end %>diff --git a/app/views/account/valuations/new.html.erb b/app/views/account/valuations/new.html.erbnew file mode 100644index 00000000..29332c17--- /dev/null+++ b/app/views/account/valuations/new.html.erb@@ -0,0 +1,4 @@+<%= turbo_frame_tag dom_id(@valuation) do %>+ <%= render "form", valuation: @valuation %>+ <div class="h-px bg-alpha-black-50 ml-20 mr-4"></div>+<% end %>diff --git a/app/views/account/valuations/show.html.erb b/app/views/account/valuations/show.html.erbnew file mode 100644index 00000000..22ab5efe--- /dev/null+++ b/app/views/account/valuations/show.html.erb@@ -0,0 +1 @@+<%= render "valuation", valuation: @valuation %>diff --git a/app/views/accounts/_account_history.html.erb b/app/views/accounts/_account_history.html.erbdeleted file mode 100644index 19cb16ff..00000000--- a/app/views/accounts/_account_history.html.erb+++ /dev/null@@ -1,29 +0,0 @@-<%# locals: (account:, valuations:) %>-<div class="bg-white space-y-4 p-5 border border-alpha-black-25 rounded-xl shadow-xs">- <div class="flex justify-between items-center">- <h3 class="font-medium text-lg">History</h3>- <%= link_to new_account_valuation_path(account), data: { turbo_frame: dom_id(Valuation.new) }, class: "flex gap-1 font-medium items-center bg-gray-50 text-gray-900 p-2 rounded-lg" do %>- <%= lucide_icon("plus", class: "w-5 h-5 text-gray-900") %>- <span class="text-sm">New entry</span>- <% end %>- </div>- <div class="rounded-xl bg-gray-25 p-1">- <div class="flex flex-col rounded-lg space-y-1">- <div class="text-xs font-medium text-gray-500 uppercase flex items-center px-4 py-2">- <div class="w-16">date</div>- <div class="flex items-center justify-between grow">- <div></div>- <div>value</div>- </div>- <div class="w-56 text-right">change</div>- <div class="w-[72px]"></div>- </div>- <div class="rounded-lg bg-white border-alpha-black-25 shadow-xs">- <%= turbo_frame_tag dom_id(Valuation.new) %>- <%= turbo_frame_tag "valuations_list" do %>- <%= render partial: "accounts/account_valuation_list", locals: { valuation_series: valuations } %>- <% end %>- </div>- </div>- </div>-</div>diff --git a/app/views/accounts/_account_valuation_list.html.erb b/app/views/accounts/_account_valuation_list.html.erbdeleted file mode 100644index 7e56de14..00000000--- a/app/views/accounts/_account_valuation_list.html.erb+++ /dev/null@@ -1,57 +0,0 @@-<%# locals: (valuation_series:) %>-<% valuation_series.values.reverse_each.with_index do |valuation, index| %>- <% valuation_styles = trend_styles(valuation.trend) %>- <%= turbo_frame_tag dom_id(valuation.original) do %>- <div class="p-4 flex items-center">- <div class="w-16">- <div class="w-8 h-8 rounded-full p-1.5 flex items-center justify-center <%= valuation_styles[:bg_class] %>">- <%= lucide_icon(valuation_styles[:icon], class: "w-4 h-4 #{valuation_styles[:text_class]}") %>- </div>- </div>- <div class="flex items-center justify-between grow">- <div class="text-sm">- <p><%= valuation.date %></p>- <%# TODO: Add descriptive name of valuation %>- <p class="text-gray-500">Manually entered</p>- </div>- <div class="flex text-sm font-medium text-right"><%= format_money valuation.value %></div>- </div>- <div class="flex w-56 justify-end text-right text-sm font-medium">- <% if valuation.trend.value == 0 %>- <span class="text-gray-500">No change</span>- <% else %>- <span class="<%= valuation_styles[:text_class] %>"><%= valuation_styles[:symbol] %><%= format_money valuation.trend.value.abs %></span>- <span class="<%= valuation_styles[:text_class] %>">(<%= lucide_icon(valuation_styles[:icon], class: "w-4 h-4 align-text-bottom inline") %> <%= valuation.trend.percent %>%)</span>- <% end %>- </div>- <div class="relative w-[72px]" data-controller="menu">- <button- data-menu-target="button"- class="ml-auto flex items-center justify-center hover:bg-gray-50 w-8 h-8 rounded-lg">- <%= lucide_icon("more-horizontal", class: "w-5 h-5 text-gray-500") %>- </button>- <div- data-menu-target="content"- class="absolute hidden min-w-[200px] z-10 top-10 right-0 bg-white p-1 rounded-sm shadow-xs border border-alpha-black-25 w-fit">- <%= link_to edit_valuation_path(valuation.original),- class: "flex gap-1 items-center hover:bg-gray-50 rounded-md p-2" do %>- <%= lucide_icon("pencil-line", class: "w-5 h-5 text-gray-500 shrink-0") %>- <span class="text-gray-900 text-sm">Edit entry</span>- <% end %>- <%= link_to valuation_path(valuation.original),- data: { turbo_method: :delete,- turbo_confirm: { title: t(".confirm_title"),- body: t(".confirm_body_html"),- accept: t(".confirm_accept") } },- class: "text-red-600 flex gap-1 items-center hover:bg-gray-50 rounded-md p-2" do %>- <%= lucide_icon("trash-2", class: "w-5 h-5 shrink-0") %>- <span class="text-sm">Delete entry</span>- <% end %>- </div>- </div>- </div>- <% unless index == valuation_series.values.size - 1 %>- <div class="h-px bg-alpha-black-50 ml-20 mr-4"></div>- <% end %>- <% end %>-<% end %>diff --git a/app/views/accounts/show.html.erb b/app/views/accounts/show.html.erbindex 1e5187e3..38c76f6f 100644--- a/app/views/accounts/show.html.erb+++ b/app/views/accounts/show.html.erb@@ -56,11 +56,11 @@<div class="p-4 flex justify-between"><div class="space-y-2"><%= render partial: "shared/value_heading", locals: {- label: "Total Value",- period: @period,- value: @account.balance_money,- trend: @balance_series.trend- } %>+ label: "Total Value",+ period: @period,+ value: @account.balance_money,+ trend: @balance_series.trend+ } %></div><%= form_with url: account_path(@account), method: :get, class: "flex items-center gap-4", data: { controller: "auto-submit-form" } do %><%= render partial: "shared/period_select", locals: { value: @period.name } %>@@ -77,7 +77,23 @@</div><div class="min-h-[800px]"><div data-tabs-target="tab" id="account-history-tab">- <%= render partial: "accounts/account_history", locals: { account: @account, valuations: @valuation_series } %>+ <div class="bg-white space-y-4 p-5 border border-alpha-black-25 rounded-xl shadow-xs">+ <div class="flex items-center justify-between">+ <%= tag.h2 t(".valuations"), class: "font-medium text-lg" %>+ <%= link_to new_account_valuation_path(@account), data: { turbo_frame: dom_id(Account::Valuation.new) }, class: "flex gap-1 font-medium items-center bg-gray-50 text-gray-900 p-2 rounded-lg" do %>+ <%= lucide_icon("plus", class: "w-5 h-5 text-gray-900") %>+ <%= tag.span t(".new_entry"), class: "text-sm" %>+ <% end %>+ </div>++ <div class="rounded-xl bg-gray-25 p-1">+ <%= turbo_frame_tag dom_id(@account, "valuations"), src: account_valuations_path(@account) do %>+ <div class="p-5 flex justify-center items-center">+ <%= tag.p t(".loading_history"), class: "text-gray-500 animate-pulse text-sm" %>+ </div>+ <% end %>+ </div>+ </div></div><div data-tabs-target="tab" id="account-transactions-tab" class="hidden"><%= render partial: "accounts/transactions", locals: { account: @account, transactions: @account.transactions.order(date: :desc) } %>diff --git a/app/views/valuations/_form_row.html.erb b/app/views/valuations/_form_row.html.erbdeleted file mode 100644index 5ef34fc9..00000000--- a/app/views/valuations/_form_row.html.erb+++ /dev/null@@ -1,16 +0,0 @@-<%# locals (f:, form_icon:, submit_button_text:)%>-<div class="h-[72px] p-4 flex items-center">- <div class="w-16">- <div class="w-8 h-8 rounded-full p-1.5 flex items-center justify-center bg-gray-500/5">- <%= lucide_icon(form_icon, class: "w-4 h-4 text-gray-500") %>- </div>- </div>- <div class="flex items-center justify-between grow">- <%= 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" %>- </div>- <div class="w-[296px] flex gap-2 justify-end items-center">- <%= link_to "Cancel", account_path(@valuation.account), class: "text-sm text-gray-900 hover:text-gray-800 font-medium px-3 py-1.5" %>- <%= f.submit submit_button_text, class: "bg-gray-50 rounded-lg font-medium px-3 py-1.5 cursor-pointer hover:bg-gray-100 text-sm" %>- </div>-</div>diff --git a/app/views/valuations/create.html.erb b/app/views/valuations/create.html.erbdeleted file mode 100644index f9aa4ce8..00000000--- a/app/views/valuations/create.html.erb+++ /dev/null@@ -1,4 +0,0 @@-<div>- <h1 class="font-bold text-4xl">Valuations#create</h1>- <p>Find me in app/views/valuations/create.html.erb</p>-</div>diff --git a/app/views/valuations/create.turbo_stream.erb b/app/views/valuations/create.turbo_stream.erbdeleted file mode 100644index b56905ef..00000000--- a/app/views/valuations/create.turbo_stream.erb+++ /dev/null@@ -1,4 +0,0 @@-<%= turbo_stream.replace Valuation.new, body: turbo_frame_tag(dom_id(Valuation.new)) %>-<%= turbo_stream.append "notification-tray", partial: "shared/notification", locals: { type: "success", content: { body: "Valuation created" } } %>-<%= turbo_stream.replace "valuations_list", partial: "accounts/account_valuation_list", locals: { valuation_series: @account.valuations.to_series } %>-<%= turbo_stream.replace "sync_message", partial: "accounts/sync_message", locals: { is_syncing: true } %>diff --git a/app/views/valuations/destroy.html.erb b/app/views/valuations/destroy.html.erbdeleted file mode 100644index 1bbc64cf..00000000--- a/app/views/valuations/destroy.html.erb+++ /dev/null@@ -1,4 +0,0 @@-<div>- <h1 class="font-bold text-4xl">Valuations#destroy</h1>- <p>Find me in app/views/valuations/destroy.html.erb</p>-</div>diff --git a/app/views/valuations/destroy.turbo_stream.erb b/app/views/valuations/destroy.turbo_stream.erbdeleted file mode 100644index 40cfb7ab..00000000--- a/app/views/valuations/destroy.turbo_stream.erb+++ /dev/null@@ -1,4 +0,0 @@-<%= turbo_stream.remove @valuation %>-<%= turbo_stream.append "notification-tray", partial: "shared/notification", locals: { type: "success", content: { body: "Valuation deleted" } } %>-<%= turbo_stream.replace "valuations_list", partial: "accounts/account_valuation_list", locals: { valuation_series: @account.valuations.to_series } %>-<%= turbo_stream.replace "sync_message", partial: "accounts/sync_message", locals: { is_syncing: true } %>diff --git a/app/views/valuations/edit.html.erb b/app/views/valuations/edit.html.erbdeleted file mode 100644index d33a5454..00000000--- a/app/views/valuations/edit.html.erb+++ /dev/null@@ -1,8 +0,0 @@-<div>- <h1 class="font-bold text-4xl">Edit Valuation</h1>- <%= turbo_frame_tag dom_id(@valuation) do %>- <%= form_with model: @valuation, url: valuation_path(@valuation), html: { class: "" } do |f| %>- <%= render "form_row", f: f, form_icon: "pencil-line", submit_button_text: "Update" %>- <% end %>- <% end %>-</div>diff --git a/app/views/valuations/new.html.erb b/app/views/valuations/new.html.erbdeleted file mode 100644index 0aa59b24..00000000--- a/app/views/valuations/new.html.erb+++ /dev/null@@ -1,9 +0,0 @@-<div>- <h1 class="font-bold text-4xl">Add Valuation: <%= @account.name %></h1>- <%= turbo_frame_tag dom_id(Valuation.new) do %>- <%= form_with model: [@account, @valuation], url: account_valuations_path(@account), html: { class: "" } do |f| %>- <%= render "form_row", f: f, form_icon: "plus", submit_button_text: "Add" %>- <% end %>- <div class="h-px bg-alpha-black-50 ml-20 mr-4"></div>- <% end %>-</div>diff --git a/app/views/valuations/show.html.erb b/app/views/valuations/show.html.erbdeleted file mode 100644index 8ed05622..00000000--- a/app/views/valuations/show.html.erb+++ /dev/null@@ -1,4 +0,0 @@-<div>- <h1 class="font-bold text-4xl">Valuation: <%= @valuation.type %></h1>- <p>Find me in app/views/valuations/show.html.erb</p>-</div>diff --git a/app/views/valuations/update.html.erb b/app/views/valuations/update.html.erbdeleted file mode 100644index 52863970..00000000--- a/app/views/valuations/update.html.erb+++ /dev/null@@ -1,4 +0,0 @@-<div>- <h1 class="font-bold text-4xl">Valuations#update</h1>- <p>Find me in app/views/valuations/update.html.erb</p>-</div>diff --git a/config/locales/views/account/valuations/en.yml b/config/locales/views/account/valuations/en.ymlnew file mode 100644index 00000000..98713c98--- /dev/null+++ b/config/locales/views/account/valuations/en.yml@@ -0,0 +1,26 @@+---+en:+ account:+ valuations:+ destroy:+ success: Valuation deleted+ form:+ cancel: Cancel+ index:+ change: change+ date: date+ value: value+ update:+ success: Valuation updated+ valuation:+ confirm_accept: Delete entry+ confirm_body_html: "<p>Deleting this entry will remove it from the account’s+ history which will impact different parts of your account. This includes+ the net worth and account graphs.</p></br><p>The only way you’ll be able+ to add this entry back is by re-entering it manually via a new entry</p>"+ confirm_title: Delete Entry?+ delete_entry: Delete entry+ edit_entry: Edit entry+ no_change: No change+ start_balance: Starting balance+ value_update: Value updatediff --git a/config/locales/views/accounts/en.yml b/config/locales/views/accounts/en.ymlindex eaf049f1..51b048e2 100644--- a/config/locales/views/accounts/en.yml+++ b/config/locales/views/accounts/en.yml@@ -1,13 +1,6 @@---en:accounts:- account_valuation_list:- confirm_accept: Delete entry- confirm_body_html: "<p>Deleting this entry will remove it from the account’s- history which will impact different parts of your account. This includes the- net worth and account graphs.</p></br><p>The only way you’ll be able to add- this entry back is by re-entering it manually via a new entry</p>"- confirm_title: Delete Entry?create:success: New account created successfullydestroy:@@ -65,9 +58,12 @@ en:confirm_title: Delete account?edit: Editimport: Import transactions+ loading_history: Loading account history...+ new_entry: New entrysync_message_missing_rates: Since exchange rates haven't been synced, balancegraphs may not reflect accurate values.sync_message_unknown_error: An error has occurred during the sync.+ valuations: Value historysummary:new: New accountsync:diff --git a/config/routes.rb b/config/routes.rbindex 2f57f282..12c059c6 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -80,8 +80,10 @@ Rails.application.routes.draw dopost :syncend- resource :logo, only: :show, module: :account- resources :valuations, shallow: true+ scope module: :account do+ resource :logo, only: :show+ resources :valuations+ endendresources :institutions, except: %i[ index show ]diff --git a/db/migrate/20240620221801_rename_valuation_table.rb b/db/migrate/20240620221801_rename_valuation_table.rbnew file mode 100644index 00000000..d675f2cd--- /dev/null+++ b/db/migrate/20240620221801_rename_valuation_table.rb@@ -0,0 +1,5 @@+class RenameValuationTable < ActiveRecord::Migration[7.2]+ def change+ rename_table :valuations, :account_valuations+ end+enddiff --git a/db/schema.rb b/db/schema.rbindex f0f7d7d9..05378ced 100644--- a/db/schema.rb+++ b/db/schema.rb@@ -10,7 +10,7 @@## It's strongly recommended that you check this file into your version control system.-ActiveRecord::Schema[7.2].define(version: 2024_06_20_125026) do+ActiveRecord::Schema[7.2].define(version: 2024_06_20_221801) do# These are extensions that must be enabled in order to support this databaseenable_extension "pgcrypto"enable_extension "plpgsql"@@ -37,6 +37,17 @@ ActiveRecord::Schema[7.2].define(version: 2024_06_20_125026) dot.datetime "updated_at", null: falseend+ create_table "account_valuations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|+ t.uuid "account_id", null: false+ t.date "date", null: false+ t.decimal "value", precision: 19, scale: 4, null: false+ t.string "currency", default: "USD", null: false+ t.datetime "created_at", null: false+ t.datetime "updated_at", null: false+ t.index ["account_id", "date"], name: "index_account_valuations_on_account_id_and_date", unique: true+ t.index ["account_id"], name: "index_account_valuations_on_account_id"+ end+create_table "accounts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|t.string "subtype"t.uuid "family_id", null: false@@ -335,23 +346,13 @@ ActiveRecord::Schema[7.2].define(version: 2024_06_20_125026) dot.index ["family_id"], name: "index_users_on_family_id"end- create_table "valuations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|- t.uuid "account_id", null: false- t.date "date", null: false- t.decimal "value", precision: 19, scale: 4, null: false- t.string "currency", default: "USD", null: false- t.datetime "created_at", null: false- t.datetime "updated_at", null: false- t.index ["account_id", "date"], name: "index_valuations_on_account_id_and_date", unique: true- t.index ["account_id"], name: "index_valuations_on_account_id"- end-create_table "vehicles", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|t.datetime "created_at", null: falset.datetime "updated_at", null: falseendadd_foreign_key "account_balances", "accounts", on_delete: :cascade+ add_foreign_key "account_valuations", "accounts", on_delete: :cascadeadd_foreign_key "accounts", "families"add_foreign_key "accounts", "institutions"add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"@@ -367,5 +368,4 @@ ActiveRecord::Schema[7.2].define(version: 2024_06_20_125026) doadd_foreign_key "transactions", "categories", on_delete: :nullifyadd_foreign_key "transactions", "merchants"add_foreign_key "users", "families"- add_foreign_key "valuations", "accounts", on_delete: :cascadeenddiff --git a/test/controllers/account/valuations_controller_test.rb b/test/controllers/account/valuations_controller_test.rbnew file mode 100644index 00000000..dee25276--- /dev/null+++ b/test/controllers/account/valuations_controller_test.rb@@ -0,0 +1,71 @@+require "test_helper"++class Account::ValuationsControllerTest < ActionDispatch::IntegrationTest+ setup do+ sign_in @user = users(:family_admin)+ @valuation = account_valuations(:savings_one)+ @account = @valuation.account+ end++ test "get valuations for an account" do+ get account_valuations_url(@account)+ assert_response :success+ end++ test "new" do+ get new_account_valuation_url(@account)+ assert_response :success+ end++ test "should create valuation" do+ assert_difference("Account::Valuation.count") do+ post account_valuations_url(@account), params: {+ account_valuation: {+ value: 19800,+ date: Date.current+ }+ }+ end++ assert_equal "Valuation created", flash[:notice]+ assert_enqueued_with job: AccountSyncJob+ assert_redirected_to account_path(@account)+ end++ test "error when valuation already exists for date" do+ assert_difference("Account::Valuation.count", 0) do+ post account_valuations_url(@account), params: {+ account_valuation: {+ value: 19800,+ date: @valuation.date+ }+ }+ end++ assert_equal "Date has already been taken", flash[:error]+ assert_redirected_to account_path(@account)+ end++ test "should update valuation" do+ patch account_valuation_url(@account, @valuation), params: {+ account_valuation: {+ value: 19550,+ date: Date.current+ }+ }++ assert_equal "Valuation updated", flash[:notice]+ assert_enqueued_with job: AccountSyncJob+ assert_redirected_to account_path(@account)+ end++ test "should destroy valuation" do+ assert_difference("Account::Valuation.count", -1) do+ delete account_valuation_url(@account, @valuation)+ end++ assert_equal "Valuation deleted", flash[:notice]+ assert_enqueued_with job: AccountSyncJob+ assert_redirected_to account_path(@account)+ end+enddiff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex d2393f25..d125af09 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -44,7 +44,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestendtest "should create an account" do- assert_difference [ "Account.count", "Valuation.count" ], 1 do+ assert_difference [ "Account.count", "Account::Valuation.count" ], 1 dopost accounts_path, params: {account: {accountable_type: "Depository",@@ -60,7 +60,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestendtest "can add optional start date and balance to an account on create" do- assert_difference -> { Account.count } => 1, -> { Valuation.count } => 2 do+ assert_difference -> { Account.count } => 1, -> { Account::Valuation.count } => 2 dopost accounts_path, params: {account: {accountable_type: "Depository",diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rbindex 9e3cc9d9..fe861f64 100644--- a/test/controllers/transactions_controller_test.rb+++ b/test/controllers/transactions_controller_test.rb@@ -75,7 +75,7 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTestendassert_equal transaction_params[:amount].to_d, Transaction.order(created_at: :desc).first.amount- assert_equal flash[:notice], "New transaction created successfully"+ assert_equal "New transaction created successfully", flash[:notice]assert_enqueued_with(job: AccountSyncJob)assert_redirected_to transactions_urlenddiff --git a/test/controllers/valuations_controller_test.rb b/test/controllers/valuations_controller_test.rbdeleted file mode 100644index bd7e476e..00000000--- a/test/controllers/valuations_controller_test.rb+++ /dev/null@@ -1,74 +0,0 @@-require "test_helper"--class ValuationsControllerTest < ActionDispatch::IntegrationTest- setup do- sign_in @user = users(:family_admin)- @valuation = valuations(:savings_one)- @account = @valuation.account- end-- test "new" do- get new_account_valuation_url(@account)- assert_response :success- end-- test "should create valuation" do- assert_difference("Valuation.count") do- post account_valuations_url(@account), params: { valuation: { value: 1, date: Date.current, type: "Appraisal" } }- end-- assert_redirected_to account_path(@valuation.account)- end-- test "should create valuation with account's currency" do- foreign_account = accounts(:eur_checking)- post account_valuations_url(foreign_account), params: { valuation: { value: 1, date: Date.current, type: "Appraisal" } }- assert_equal foreign_account.currency, Valuation.order(created_at: :desc).first.currency- end-- test "create should sync account with correct start date" do- date = Date.current - 1.day-- assert_enqueued_with(job: AccountSyncJob, args: [ @account, date ]) do- post account_valuations_url(@account), params: { valuation: { value: 2, date:, type: "Appraisal" } }- end- end-- test "should update valuation" do- date = @valuation.date- patch valuation_url(@valuation), params: { valuation: { account_id: @valuation.account_id, value: 1, date:, type: "Appraisal" } }- assert_redirected_to account_path(@valuation.account)- end-- test "update should sync account with correct start date" do- new_date = @valuation.date - 1.day- assert_enqueued_with(job: AccountSyncJob, args: [ @account, new_date ]) do- patch valuation_url(@valuation), params: { valuation: { account_id: @valuation.account_id, value: @valuation.value, date: new_date, type: "Appraisal" } }- end-- new_date = @valuation.reload.date + 1.day- assert_enqueued_with(job: AccountSyncJob, args: [ @account, @valuation.date ]) do- patch valuation_url(@valuation), params: { valuation: { account_id: @valuation.account_id, value: @valuation.value, date: new_date, type: "Appraisal" } }- end- end-- test "should destroy valuation" do- assert_difference("Valuation.count", -1) do- delete valuation_url(@valuation)- end-- assert_redirected_to account_path(@account)- end-- test "destroy should sync account with correct start date" do- first, second = @account.valuations.order(:date).all-- assert_enqueued_with(job: AccountSyncJob, args: [ @account, first.date ]) do- delete valuation_url(second)- end-- assert_enqueued_with(job: AccountSyncJob, args: [ @account, nil ]) do- delete valuation_url(first)- end- end-enddiff --git a/test/fixtures/valuations.yml b/test/fixtures/account/valuations.ymlsimilarity index 100%rename from test/fixtures/valuations.ymlrename to test/fixtures/account/valuations.ymldiff --git a/test/models/transfer_test.rb b/test/models/account/transfer_test.rbsimilarity index 97%rename from test/models/transfer_test.rbrename to test/models/account/transfer_test.rbindex 0ca4be91..d176dacf 100644--- a/test/models/transfer_test.rb+++ b/test/models/account/transfer_test.rb@@ -1,6 +1,6 @@require "test_helper"-class TransferTest < ActiveSupport::TestCase+class Account::TransferTest < ActiveSupport::TestCasesetup do# Transfers can be posted on different dates@outflow = accounts(:checking).transactions.create! date: 1.day.ago.to_date, name: "Transfer to Savings", amount: 100, marked_as_transfer: truediff --git a/test/models/account/valuation_test.rb b/test/models/account/valuation_test.rbnew file mode 100644index 00000000..a7443237--- /dev/null+++ b/test/models/account/valuation_test.rb@@ -0,0 +1,39 @@+require "test_helper"++class Account::ValuationTest < ActiveSupport::TestCase+ setup do+ @valuation = account_valuations :savings_one+ @family = families :dylan_family+ end++ test "one valuation per day" do+ assert_equal 12.days.ago.to_date, account_valuations(:savings_one).date+ invalid_valuation = Account::Valuation.new date: 12.days.ago.to_date, value: 20000+ assert invalid_valuation.invalid?+ end++ test "triggers sync with correct start date when valuation is set to prior date" do+ prior_date = @valuation.date - 1+ @valuation.update! date: prior_date++ @valuation.account.expects(:sync_later).with(prior_date)+ @valuation.sync_account_later+ end++ test "triggers sync with correct start date when valuation is set to future date" do+ prior_date = @valuation.date+ @valuation.update! date: @valuation.date + 1++ @valuation.account.expects(:sync_later).with(prior_date)+ @valuation.sync_account_later+ end++ test "triggers sync with correct start date when valuation deleted" do+ prior_valuation = account_valuations :savings_two # 25 days ago+ current_valuation = account_valuations :savings_one # 12 days ago+ current_valuation.destroy!++ current_valuation.account.expects(:sync_later).with(prior_valuation.date)+ current_valuation.sync_account_later+ end+enddiff --git a/test/models/account_test.rb b/test/models/account_test.rbindex 10397231..36633ff6 100644--- a/test/models/account_test.rb+++ b/test/models/account_test.rb@@ -111,7 +111,7 @@ class AccountTest < ActiveSupport::TestCaseendtest "should destroy dependent valuations" do- assert_difference("Valuation.count", [email protected]) do+ assert_difference("Account::Valuation.count", [email protected]) do@account.destroyendenddiff --git a/test/models/time_series/trend_test.rb b/test/models/time_series/trend_test.rbindex 1ad73421..b05d13ad 100644--- a/test/models/time_series/trend_test.rb+++ b/test/models/time_series/trend_test.rb@@ -11,11 +11,13 @@ class TimeSeries::TrendTest < ActiveSupport::TestCasetest "up" dotrend = TimeSeries::Trend.new(current: 100, previous: 50)assert_equal "up", trend.direction+ assert_equal "#10A861", trend.colorendtest "down" dotrend = TimeSeries::Trend.new(current: 50, previous: 100)assert_equal "down", trend.direction+ assert_equal "#F13636", trend.colorendtest "flat" do@@ -25,6 +27,7 @@ class TimeSeries::TrendTest < ActiveSupport::TestCaseassert_equal "flat", trend1.directionassert_equal "flat", trend2.directionassert_equal "flat", trend3.direction+ assert_equal "#737373", trend1.colorendtest "infinitely up" dodiff --git a/test/models/valuation_test.rb b/test/models/valuation_test.rbdeleted file mode 100644index 59ba8292..00000000--- a/test/models/valuation_test.rb+++ /dev/null@@ -1,4 +0,0 @@-require "test_helper"--class ValuationTest < ActiveSupport::TestCase-end