Skip to content

Commit

Permalink
Add Local Development Demo Data (maybe-finance#502)
Browse files Browse the repository at this point in the history
* Clean up seeds, add development demo data

* Handle liability account display and sync

* Fix tests
  • Loading branch information
zachgoll authored Feb 29, 2024
1 parent dbf575c commit 14641d1
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 62 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ cd maybe
cp .env.example .env
bin/setup
bin/dev

# Optionally, load demo data
rake demo_data:reset
```

And visit http://localhost:3000 to see the app. You can use the following credentials to log in (generated by DB seed):

Email: `[email protected]`
Password: `password`
- Email: `[email protected]`
- Password: `password`

For further instructions, see guides below.

Expand Down
7 changes: 4 additions & 3 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ def sidebar_link_to(name, path, options = {})
end

# Styles to use when displaying a change in value
def trend_styles(trend)
def trend_styles(trend, mode: :asset)
puts mode == :liability ? "it is a liability" : "it is an asset"
bg_class, text_class, symbol, icon = case trend.direction
when "up"
[ "bg-green-500/5", "text-green-500", "+", "arrow-up" ]
mode == :liability ? [ "bg-red-500/5", "text-red-500", "+", "arrow-up" ] : [ "bg-green-500/5", "text-green-500", "+", "arrow-up" ]
when "down"
[ "bg-red-500/5", "text-red-500", "-", "arrow-down" ]
mode == :liability ? [ "bg-green-500/5", "text-green-500", "-", "arrow-down" ] : [ "bg-red-500/5", "text-red-500", "-", "arrow-down" ]
when "flat"
[ "bg-gray-500/5", "text-gray-500", "", "minus" ]
else
Expand Down
15 changes: 15 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ class Account < ApplicationRecord
delegate :type_name, to: :accountable
before_create :check_currency

def classification
classifications = {
"Account::Depository" => :asset,
"Account::Investment" => :asset,
"Account::Property" => :asset,
"Account::Vehicle" => :asset,
"Account::OtherAsset" => :asset,
"Account::Loan" => :liability,
"Account::Credit" => :liability,
"Account::OtherLiability" => :liability
}

classifications[accountable_type]
end

def balance_series(period)
filtered_balances = balances.in_period(period).order(:date)
return nil if filtered_balances.empty?
Expand Down
2 changes: 2 additions & 0 deletions app/models/account/balance_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def daily_balances(start_date = nil)
oldest_entry = [ valuations.first, transactions.first ].compact.min_by(&:date)

net_transaction_flows = transactions.sum(&:amount)
net_transaction_flows *= -1 if @account.classification == :liability
implied_start_balance = oldest_entry.is_a?(Valuation) ? oldest_entry.value : @account.balance + net_transaction_flows

prior_balance = implied_start_balance
Expand All @@ -21,6 +22,7 @@ def daily_balances(start_date = nil)
current_balance = valuation.value
else
current_day_net_transaction_flows = transactions.select { |t| t.date == date }.sum(&:amount)
current_day_net_transaction_flows *= -1 if @account.classification == :liability
current_balance = prior_balance - current_day_net_transaction_flows
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/accounts/_account_history.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<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: valuation_series } %>
<%= render partial: "accounts/account_valuation_list", locals: { valuation_series: valuation_series, classification: account.classification } %>
<% end %>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/accounts/_account_valuation_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%# locals: (valuation_series:) %>
<%# locals: (valuation_series:, classification:) %>
<% valuation_series.with_index do |valuation_item, index| %>
<% valuation, trend = valuation_item.values_at(:value, :trend) %>
<% valuation_styles = trend_styles(valuation_item[:trend]) %>
<% valuation_styles = trend_styles(valuation_item[:trend], mode: classification) %>
<%= turbo_frame_tag dom_id(valuation) do %>
<div class="p-4 flex items-center">
<div class="w-16">
Expand Down
2 changes: 1 addition & 1 deletion app/views/accounts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= turbo_stream_from @account %>
<% balance_trend_styles = @balance_series.nil? ? {} : trend_styles(@balance_series[:trend]) %>
<% balance_trend_styles = @balance_series.nil? ? {} : trend_styles(@balance_series[:trend], mode: @account.classification) %>
<div class="space-y-4">
<div class="flex justify-between items-center">
<div class="flex items-center gap-3">
Expand Down
54 changes: 1 addition & 53 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,8 @@
# This file should ensure the existence of records required to run the application in every environment (production,
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Example:
#
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
# MovieGenre.find_or_create_by!(name: genre_name)
# end

# https://github.com/rails/rails/issues/29112#issuecomment-320653056
ApplicationRecord.reset_column_information

# Create the default user
family = Family.create_or_find_by(name: "The Maybe Family")
puts "Family created: #{family.name}"
user = User.create_or_find_by(email: "[email protected]") do |u|
u.first_name = "Josh"
u.last_name = "Maybe"
u.password = "password"
u.password_confirmation = "password"
u.family_id = family.id
end
puts "User created: #{user.email} for family: #{family.name}"

# Create default currency
Currency.find_or_create_by(iso_code: "USD", name: "United States Dollar")

checking_account = Account::Depository.new
account = Account.create_or_find_by(
name: "Seed Checking Account",
accountable: checking_account,
family: family,
balance: 5000
)
puts "Account created: #{account.name}"

valuations = [
{ date: 1.year.ago.to_date, value: 4200 },
{ date: 250.days.ago.to_date, value: 4500 },
{ date: 200.days.ago.to_date, value: 4444.96 }
]

account.valuations.upsert_all(valuations, unique_by: :index_valuations_on_account_id_and_date)

puts "Valuations created: #{valuations.count}"

transactions = [
{ date: Date.today - 27, amount: 7.56, name: "Starbucks" },
{ date: Date.today - 18, amount: -500, name: "Paycheck" },
{ date: Date.today - 18, amount: 18.20, name: "Walgreens" },
{ date: Date.today - 13, amount: 34.20, name: "Chipotle" },
{ date: Date.today - 9, amount: -200, name: "Birthday check" },
{ date: Date.today - 5, amount: 85.00, name: "Amazon stuff" }
]
transactions.each do |t|
account.transactions.find_or_create_by(t)
end

puts "Transactions created: #{transactions.count}"
puts 'Run the following command to create demo data: `rake demo_data:reset`' if Rails.env.development?
Loading

0 comments on commit 14641d1

Please sign in to comment.