Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/allocation/ongoing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ class Allocation::Ongoing < Allocation
presence: true,
numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }

def dollar_amount
(percentage.to_i / 100.0 * scenario.ongoing_giving_amount).round
end

def ongoing?
true
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/scenario.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ class Scenario < ApplicationRecord
def ongoing_percentage_total
ongoing_allocations.sum { |allocation| allocation.percentage.to_i }
end

def one_time_giving_amount
one_time_allocations.sum { |allocation| allocation.amount.to_i }
end

def ongoing_giving_amount
total_giving_amount.to_i - one_time_giving_amount
end
end
6 changes: 5 additions & 1 deletion app/views/scenarios/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@

<div class="mt-5">
<h3 class="text-[11px] font-semibold uppercase tracking-wide text-ink-faint">On going giving</h3>
<p class="mt-1 font-serif text-2xl font-medium text-ink"><%= number_to_currency(@scenario.ongoing_giving_amount, precision: 0) %></p>
<% if @scenario.ongoing_allocations.any? %>
<ul class="mt-3 space-y-2">
<% @scenario.ongoing_allocations.each do |allocation| %>
<li class="flex justify-between text-ink-soft">
<span><%= allocation.display_label %></span>
<span class="font-medium text-ink"><%= allocation.percentage %>%</span>
<span class="flex items-baseline gap-3">
<span class="font-medium text-ink"><%= allocation.percentage %>%</span>
<span class="text-ink-soft"><%= number_to_currency(allocation.dollar_amount, precision: 0) %></span>
</span>
</li>
<% end %>
</ul>
Expand Down
5 changes: 5 additions & 0 deletions test/models/allocation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class AllocationTest < ActiveSupport::TestCase
assert @scenario.one_time_allocations.new(option: "Just fits", amount: 5000).valid?
end

test "ongoing dollar_amount is its percentage of the scenario's ongoing giving" do
# scenario ongoing giving is 10000 total - 5000 one-time = 5000; greatest_need is 30%.
assert_equal 1500, allocations(:greatest_need).dollar_amount
end

test "kind predicates reflect the subclass" do
assert allocations(:greatest_need).ongoing?
assert_not allocations(:greatest_need).one_time?
Expand Down
13 changes: 13 additions & 0 deletions test/models/scenario_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ class ScenarioTest < ActiveSupport::TestCase
assert_equal 30, scenarios(:one_arlington).ongoing_percentage_total
end

test "one_time_giving_amount sums one-time allocation amounts" do
assert_equal 5000, scenarios(:one_arlington).one_time_giving_amount
end

test "ongoing_giving_amount is total giving minus one-time gifts" do
assert_equal 5000, scenarios(:one_arlington).ongoing_giving_amount
end

test "ongoing_giving_amount treats a missing total as zero" do
scenario = Scenario.new
assert_equal 0, scenario.ongoing_giving_amount
end

test "destroys dependent allocations" do
scenario = scenarios(:one_arlington)
assert_difference -> { Allocation.count }, -2 do
Expand Down
Loading