should update cost if a new rate is added in between

git-svn-id: https://dev.finn.de/svn/cockpit/trunk@196 7926756e-e54e-46e6-9721-ed318f58905e
pull/6827/head
rkh 15 years ago
parent 2fa7916426
commit 932daa29f8
  1. 6
      app/models/cost_entry.rb
  2. 12
      app/models/rate.rb
  3. 12
      spec/models/cost_entry_spec.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

@ -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

@ -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

Loading…
Cancel
Save