diff --git a/app/models/allocation/ongoing.rb b/app/models/allocation/ongoing.rb index a6fed9e..a7e3b13 100644 --- a/app/models/allocation/ongoing.rb +++ b/app/models/allocation/ongoing.rb @@ -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 diff --git a/app/models/scenario.rb b/app/models/scenario.rb index 17c6bad..418f8f2 100644 --- a/app/models/scenario.rb +++ b/app/models/scenario.rb @@ -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 diff --git a/app/views/scenarios/show.html.erb b/app/views/scenarios/show.html.erb index 7c70cec..a36636c 100644 --- a/app/views/scenarios/show.html.erb +++ b/app/views/scenarios/show.html.erb @@ -43,12 +43,16 @@

On going giving

+

<%= number_to_currency(@scenario.ongoing_giving_amount, precision: 0) %>

<% if @scenario.ongoing_allocations.any? %> diff --git a/test/models/allocation_test.rb b/test/models/allocation_test.rb index 62105e7..a4dcb2c 100644 --- a/test/models/allocation_test.rb +++ b/test/models/allocation_test.rb @@ -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? diff --git a/test/models/scenario_test.rb b/test/models/scenario_test.rb index d65b76b..bf3f4e2 100644 --- a/test/models/scenario_test.rb +++ b/test/models/scenario_test.rb @@ -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