diff --git a/app/helpers/contracts_helper.rb b/app/helpers/contracts_helper.rb index 47114af..16d0e28 100644 --- a/app/helpers/contracts_helper.rb +++ b/app/helpers/contracts_helper.rb @@ -111,4 +111,12 @@ module ContractsHelper options end + + # Given a deliverable and period, validate the period + # TODO: could use a better name + def validate_period(deliverable, period) + if deliverable.current_date && deliverable.within_period_range?(period) + return period + end + end end diff --git a/app/models/retainer_deliverable.rb b/app/models/retainer_deliverable.rb index 0a675d6..8d65443 100644 --- a/app/models/retainer_deliverable.rb +++ b/app/models/retainer_deliverable.rb @@ -47,6 +47,17 @@ class RetainerDeliverable < HourlyDeliverable date_range.include?(date) end + # period in the format of "%Y-%m" or "%B %Y" + def within_period_range?(period) + begin + # both valid formats work by adding a day to the end like -01 + date = Date.parse(period.to_s + "-01") + within_date_range?(date) + rescue ArgumentError + return false + end + end + def months month_acc = [] diff --git a/app/views/deliverables/_details_row.html.erb b/app/views/deliverables/_details_row.html.erb index c4d6aea..d35f604 100644 --- a/app/views/deliverables/_details_row.html.erb +++ b/app/views/deliverables/_details_row.html.erb @@ -1,4 +1,4 @@ -<% period ||= nil %> +<% validated_period = validate_period(deliverable, period) %> @@ -16,7 +16,7 @@
@@ -52,28 +52,28 @@ Labor - <%= h(format_value_field_for_contracts(deliverable.labor_budget_spent(period))) %> - <%= h(format_value_field_for_contracts(deliverable.labor_budget_total(period))) %> + <%= h(format_value_field_for_contracts(deliverable.labor_budget_spent(validated_period))) %> + <%= h(format_value_field_for_contracts(deliverable.labor_budget_total(validated_period))) %> TODO: Release 2 / TODO hrs Overhead - <%= h(format_value_field_for_contracts(deliverable.overhead_spent(period))) %> - <%= h(format_value_field_for_contracts(deliverable.overhead_budget_total(period))) %> + <%= h(format_value_field_for_contracts(deliverable.overhead_spent(validated_period))) %> + <%= h(format_value_field_for_contracts(deliverable.overhead_budget_total(validated_period))) %> TODO: Release 2 / TODO hrs <%# TODO: Release 2, Fixed %> <%# TODO: Release 2, Markup %> Profit - <%= h(format_value_field_for_contracts(deliverable.profit_left(period))) %> - <%= h(format_value_field_for_contracts(deliverable.profit_budget(period))) %> + <%= h(format_value_field_for_contracts(deliverable.profit_left(validated_period))) %> + <%= h(format_value_field_for_contracts(deliverable.profit_budget(validated_period))) %> Total: - <%= h(format_value_field_for_contracts(deliverable.total_spent(period))) %> - <%= h(format_value_field_for_contracts(deliverable.total(period))) %> + <%= h(format_value_field_for_contracts(deliverable.total_spent(validated_period))) %> + <%= h(format_value_field_for_contracts(deliverable.total(validated_period))) %> TODO: Release 2 diff --git a/test/unit/helpers/contracts_helper_test.rb b/test/unit/helpers/contracts_helper_test.rb new file mode 100644 index 0000000..92df052 --- /dev/null +++ b/test/unit/helpers/contracts_helper_test.rb @@ -0,0 +1,31 @@ +require 'test_helper' + +class ContractsHelperTest < ActionView::TestCase + context "#validate_period" do + should "with a HourlyDeliverable should return nil" do + assert_equal nil, validate_period(HourlyDeliverable.new, '2010-01') + end + + should "with a FixedDeliverable should return nil" do + assert_equal nil, validate_period(FixedDeliverable.new, '2010-01') + end + + context "with a RetainerDeliverable" do + should "return nil when there period is not within the Deliverable's date range" do + retainer = RetainerDeliverable.new(:start_date => Date.new(2011,1,1), + :end_date => Date.new(2012,1,1)) + + assert_equal nil, validate_period(retainer, '2010-01') + end + + should "return the period when it's within the Deliverable's date range" do + retainer = RetainerDeliverable.new(:start_date => Date.new(2001,1,1), + :end_date => Date.new(2003,1,1)) + + assert_equal '2001-02', validate_period(retainer, '2001-02') + end + + end + end + +end