diff --git a/app/helpers/scenarios_helper.rb b/app/helpers/scenarios_helper.rb new file mode 100644 index 0000000..e371015 --- /dev/null +++ b/app/helpers/scenarios_helper.rb @@ -0,0 +1,7 @@ +module ScenariosHelper + CHART_COLORS = ["#E07B39", "#D9632B", "#C99A2E", "#B8842B", "#E0954F", "#C76A28"].freeze + + def allocation_chart_color(index) + CHART_COLORS[index % CHART_COLORS.length] + end +end diff --git a/app/models/allocation/one_time.rb b/app/models/allocation/one_time.rb index e78a180..2df868a 100644 --- a/app/models/allocation/one_time.rb +++ b/app/models/allocation/one_time.rb @@ -12,6 +12,13 @@ def one_time? true end + def share_percentage + total = scenario.one_time_giving_amount + return 0 if total.zero? + + (amount.to_i / total.to_f * 100).round + end + private def within_total_giving_amount diff --git a/app/models/allocation/ongoing.rb b/app/models/allocation/ongoing.rb index a7e3b13..6f83ce1 100644 --- a/app/models/allocation/ongoing.rb +++ b/app/models/allocation/ongoing.rb @@ -1,4 +1,6 @@ class Allocation::Ongoing < Allocation + PERPETUITY_PAYOUT_RATE = 0.05 + validates :percentage, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 100 } @@ -7,6 +9,10 @@ def dollar_amount (percentage.to_i / 100.0 * scenario.ongoing_giving_amount).round end + def perpetuity_annual_amount + (dollar_amount * PERPETUITY_PAYOUT_RATE).round + end + def ongoing? true end diff --git a/app/views/scenarios/show.html.erb b/app/views/scenarios/show.html.erb index a36636c..4c19d39 100644 --- a/app/views/scenarios/show.html.erb +++ b/app/views/scenarios/show.html.erb @@ -37,49 +37,97 @@ + <% ongoing_total = @scenario.ongoing_percentage_total %> + <% ongoing_amount = @scenario.ongoing_giving_amount %> + <% one_time_amount = @scenario.one_time_giving_amount %> + <% total_amount = @scenario.total_giving_amount.to_i %>

Allocation summary

-

Set your total above to see a full breakdown.

+ <% if total_amount.positive? %> +

+ On going <%= number_to_currency(ongoing_amount, precision: 0) %> + + One-time <%= number_to_currency(one_time_amount, precision: 0) %> + = Total <%= number_to_currency(total_amount, precision: 0) %> +

+ <% else %> +

Set your total above to see a full breakdown.

+ <% end %>

On going giving

-

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

+

<%= number_to_currency(ongoing_amount, precision: 0) %>

<% if @scenario.ongoing_allocations.any? %> -
-
-

One time giving

- <% if @scenario.one_time_allocations.any? %> + <% if @scenario.one_time_allocations.any? %> +
+
+

+ One time giving + ADD-ON +

+ <%= number_to_currency(one_time_amount, precision: 0) %> +
    <% @scenario.one_time_allocations.each do |allocation| %> -
  • - <%= allocation.display_label %> - <%= number_to_currency(allocation.amount, precision: 0) %> +
  • + + + <%= allocation.display_label %> + + + <%= allocation.share_percentage %>% + <%= number_to_currency(allocation.amount, precision: 0) %> +
  • <% end %>
- <% else %> -

No one-time allocations yet.

- <% end %> -
+
+ <% end %> + + <% if total_amount.positive? %> +
+ On going <%= number_to_currency(ongoing_amount, precision: 0) %> + + One-time <%= number_to_currency(one_time_amount, precision: 0) %> + = Total <%= number_to_currency(total_amount, precision: 0) %> +
+ <% end %>
diff --git a/test/models/allocation/one_time_test.rb b/test/models/allocation/one_time_test.rb new file mode 100644 index 0000000..8b6e232 --- /dev/null +++ b/test/models/allocation/one_time_test.rb @@ -0,0 +1,12 @@ +require "test_helper" + +class Allocation::OneTimeTest < ActiveSupport::TestCase + test "share_percentage is the allocation's share of one-time giving" do + assert_equal 100, allocations(:education_grant).share_percentage + end + + test "share_percentage is zero when there is no one-time giving" do + allocation = Allocation::OneTime.new(amount: 500, scenario: scenarios(:two_boston)) + assert_equal 0, allocation.share_percentage + end +end diff --git a/test/models/allocation/ongoing_test.rb b/test/models/allocation/ongoing_test.rb new file mode 100644 index 0000000..c0302b2 --- /dev/null +++ b/test/models/allocation/ongoing_test.rb @@ -0,0 +1,11 @@ +require "test_helper" + +class Allocation::OngoingTest < ActiveSupport::TestCase + test "dollar_amount is the percentage of ongoing giving" do + assert_equal 1500, allocations(:greatest_need).dollar_amount + end + + test "perpetuity_annual_amount is 5% of the dollar amount" do + assert_equal 75, allocations(:greatest_need).perpetuity_annual_amount + end +end