diff --git a/app/models/fixed_budget.rb b/app/models/fixed_budget.rb index 783e560..ff04099 100644 --- a/app/models/fixed_budget.rb +++ b/app/models/fixed_budget.rb @@ -7,4 +7,27 @@ class FixedBudget < ActiveRecord::Base # Validations # Accessors + def markup_value + return 0 if budget.blank? || markup.blank? + + case + when percent_markup? + percent = markup.gsub('%','').to_f + return budget.to_f * (percent / 100) + when straight_markup? + markup.gsub('$','').gsub(',','').to_f + else + 0 # Invalid markup + end + + end + + def percent_markup? + markup && markup.match(/%/) + end + + def straight_markup? + markup && markup.match(/\$/) + end + end diff --git a/test/unit/fixed_budget_test.rb b/test/unit/fixed_budget_test.rb index 09c18d1..2ea7def 100644 --- a/test/unit/fixed_budget_test.rb +++ b/test/unit/fixed_budget_test.rb @@ -3,4 +3,31 @@ require File.dirname(__FILE__) + '/../test_helper' class FixedBudgetTest < ActiveSupport::TestCase should_belong_to :deliverable + context "#markup_value" do + setup do + @fixed_budget = FixedBudget.new(:budget => 1000) + end + + context "with no markup" do + should "be 0" do + assert_equal nil, @fixed_budget.markup + assert_equal 0, @fixed_budget.markup_value + end + end + + context "with a % markup" do + should "equal the budget times the %" do + @fixed_budget.markup = '50%' + assert_equal 500, @fixed_budget.markup_value + end + end + + context "with a $ markup" do + should "equal the $ markup (straight markup)" do + @fixed_budget.markup = '$4,000.57' + assert_equal 4000.57, @fixed_budget.markup_value + end + end + + end end