diff --git a/app/models/cost_entry.rb b/app/models/cost_entry.rb index c2e14388d3..baed0ddb73 100644 --- a/app/models/cost_entry.rb +++ b/app/models/cost_entry.rb @@ -30,9 +30,15 @@ class CostEntry < ActiveRecord::Base errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project) errors.add :user_id, :activerecord_error_invalid unless (user == User.current) || (User.current.allowed_to? :book_costs, project) + begin + spent_on.to_date + rescue Exception + errors.add :spent_on, :activerecord_error_invalid + end end def before_save + self.spent_on &&= spent_on.to_date update_costs issue.save end diff --git a/app/models/rate.rb b/app/models/rate.rb index 964a4ad511..1fddbd74af 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -1,5 +1,6 @@ class Rate < ActiveRecord::Base validates_numericality_of :rate, :allow_nil => false, :message => :activerecord_error_invalid + validates_format_of :valid_from, :with => /^\d{4}-\d{2}-\d{2}/ def self.clean_currency(value) if value && value.is_a?(String) @@ -10,4 +11,15 @@ class Rate < ActiveRecord::Base value end end + + def validate + valid_from.to_date + rescue Exception + errors.add :valid_from, :activerecord_error_invalid + end + + def before_save + self.valid_from &&= valid_from.to_date + end + end \ No newline at end of file diff --git a/spec/models/cost_entry_spec.rb b/spec/models/cost_entry_spec.rb index e7c55e9c49..a6dfbb6716 100644 --- a/spec/models/cost_entry_spec.rb +++ b/spec/models/cost_entry_spec.rb @@ -44,6 +44,18 @@ describe CostEntry do @example.rate.should_not == rates("cheap_one") @example.costs.should == cheap.rate end + + it "should update cost if a new rate is added in between" do + @example.cost_type = cost_types("umbrella") + @example.spent_on = 3.days.ago + @example.units = 1 + @example.save! + @example.costs.should == rates("cheap_three").rate + cheap = CostRate.create! :valid_from => 3.days.ago.to_date, :rate => 1.0, :cost_type => cost_types("umbrella") + @example.reload + @example.rate.should_not == rates("cheap_three") + @example.costs.should == cheap.rate + end it "should update cost if a spent_on changes" do @example.units = 1