1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-02 20:15:22 +02:00

Add Property Details View (#1116)

* Add backend for property account details

* Rubocop updates

* Add property form with details

* Revert "Rubocop updates"

This reverts commit 05b0b8f3a4.

* Bump brakeman to latest version

* Add overview section to property view

* Lint fixes
This commit is contained in:
Zach Gollwitzer 2024-08-23 08:47:08 -04:00 committed by GitHub
parent 4433488562
commit e856691c86
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 547 additions and 81 deletions

View file

@ -28,6 +28,8 @@ class Account < ApplicationRecord
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
accepts_nested_attributes_for :accountable
delegate :value, :series, to: :accountable
class << self
@ -51,27 +53,28 @@ class Account < ApplicationRecord
end
def create_with_optional_start_balance!(attributes:, start_date: nil, start_balance: nil)
account = self.new(attributes.except(:accountable_type))
account.accountable = Accountable.from_type(attributes[:accountable_type])&.new
transaction do
attributes[:accountable_attributes] ||= {} # Ensure accountable is created
account = new(attributes)
# Always build the initial valuation
account.entries.build \
date: Date.current,
amount: attributes[:balance],
currency: account.currency,
entryable: Account::Valuation.new
# Conditionally build the optional start valuation
if start_date.present? && start_balance.present?
# Always initialize an account with a valuation entry to begin tracking value history
account.entries.build \
date: start_date,
amount: start_balance,
date: Date.current,
amount: account.balance,
currency: account.currency,
entryable: Account::Valuation.new
end
account.save!
account
if start_date.present? && start_balance.present?
account.entries.build \
date: start_date,
amount: start_balance,
currency: account.currency,
entryable: Account::Valuation.new
end
account.save!
account
end
end
end

24
app/models/address.rb Normal file
View file

@ -0,0 +1,24 @@
class Address < ApplicationRecord
belongs_to :addressable, polymorphic: true
validates :line1, :locality, presence: true
validates :postal_code, presence: true, if: :postal_code_required?
def to_s
I18n.t("address.format",
line1: line1,
line2: line2,
county: county,
locality: locality,
region: region,
country: country,
postal_code: postal_code
)
end
private
def postal_code_required?
country.in?(%w[US CA GB])
end
end

20
app/models/measurement.rb Normal file
View file

@ -0,0 +1,20 @@
class Measurement
include ActiveModel::Validations
attr_reader :value, :unit
VALID_UNITS = %w[sqft sqm]
validates :unit, inclusion: { in: VALID_UNITS }
validates :value, presence: true
def initialize(value, unit)
@value = value.to_f
@unit = unit.to_s.downcase.strip
validate!
end
def to_s
"#{@value.to_i} #{@unit}"
end
end

View file

@ -1,3 +1,30 @@
class Property < ApplicationRecord
include Accountable
has_one :address, as: :addressable, dependent: :destroy
accepts_nested_attributes_for :address
attribute :area_unit, :string, default: "sqft"
def area
Measurement.new(area_value, area_unit) if area_value.present?
end
def purchase_price
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
private
def first_valuation_amount
account.entries.account_valuations.order(:date).first&.amount_money || account.balance_money
end
end