diff --git a/app/models/cost_entry.rb b/app/models/cost_entry.rb index ea0df1780b..b2797a09a2 100644 --- a/app/models/cost_entry.rb +++ b/app/models/cost_entry.rb @@ -4,6 +4,7 @@ class CostEntry < ActiveRecord::Base belongs_to :project belongs_to :issue belongs_to :user + include Costs::DeletedUserFallback belongs_to :cost_type belongs_to :cost_object belongs_to :rate, :class_name => "CostRate" diff --git a/app/models/labor_budget_item.rb b/app/models/labor_budget_item.rb index 324be7e935..cc9556dce1 100644 --- a/app/models/labor_budget_item.rb +++ b/app/models/labor_budget_item.rb @@ -1,6 +1,7 @@ class LaborBudgetItem < ActiveRecord::Base belongs_to :cost_object belongs_to :user + include Costs::DeletedUserFallback validates_length_of :comments, :maximum => 255, :allow_nil => true validates_presence_of :user diff --git a/app/models/rate.rb b/app/models/rate.rb index 54ffe987c9..81b1e1235b 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -3,6 +3,7 @@ class Rate < ActiveRecord::Base validate :validate_date_is_a_date belongs_to :user + include Costs::DeletedUserFallback belongs_to :project attr_accessible :rate, :project, :valid_from diff --git a/lib/costs/deleted_user_fallback.rb b/lib/costs/deleted_user_fallback.rb new file mode 100644 index 0000000000..089f50eeb3 --- /dev/null +++ b/lib/costs/deleted_user_fallback.rb @@ -0,0 +1,23 @@ +module Costs + module DeletedUserFallback + def self.included(base) + base.send(:include, InstanceMethods) + + base.class_eval do + alias_method_chain :user, :deleted_user_fallback + end + end + + module InstanceMethods + def user_with_deleted_user_fallback(force_reload = true) + associated_user = user_without_deleted_user_fallback(force_reload) + + if associated_user.nil? && read_attribute(:user_id).present? + associated_user = DeletedUser.first + end + + associated_user + end + end + end +end diff --git a/spec/models/cost_entry_spec.rb b/spec/models/cost_entry_spec.rb index c6470f74b6..d3286dc3f7 100644 --- a/spec/models/cost_entry_spec.rb +++ b/spec/models/cost_entry_spec.rb @@ -217,7 +217,18 @@ describe CostEntry do before { cost_type.deleted_at = Date.new } it { cost_entry.should_not be_valid } + describe :user do + describe "WHEN a non existing user is provided (i.e. the user has been deleted)" do + before do + cost_entry.save! + user.destroy end + + it { cost_entry.reload.user.should == DeletedUser.first } + end + + describe "WHEN an existing user is provided" do + it { cost_entry.user.should == user } end end end diff --git a/spec/models/default_hourly_rate_spec.rb b/spec/models/default_hourly_rate_spec.rb new file mode 100644 index 0000000000..59ac604f7a --- /dev/null +++ b/spec/models/default_hourly_rate_spec.rb @@ -0,0 +1,30 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe DefaultHourlyRate do + let(:project) { Factory.create(:project) } + let(:user) { Factory.create(:user) } + let(:rate) { Factory.build(:default_hourly_rate, :project => project, + :user => user) } + + describe :user do + describe "WHEN an existing user is provided" do + before do + rate.user = user + rate.save! + end + + it { rate.user.should == user } + end + + describe "WHEN a non existing user is provided (i.e. the user is deleted)" do + before do + rate.user = user + rate.save! + user.destroy + rate.reload + end + + it { rate.user.should == DeletedUser.first } + end + end +end diff --git a/spec/models/hourly_rate_spec.rb b/spec/models/hourly_rate_spec.rb new file mode 100644 index 0000000000..4cb1a800f5 --- /dev/null +++ b/spec/models/hourly_rate_spec.rb @@ -0,0 +1,30 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe HourlyRate do + let(:project) { Factory.create(:project) } + let(:user) { Factory.create(:user) } + let(:rate) { Factory.build(:hourly_rate, :project => project, + :user => user) } + + describe :user do + describe "WHEN an existing user is provided" do + before do + rate.user = user + rate.save! + end + + it { rate.user.should == user } + end + + describe "WHEN a non existing user is provided (i.e. the user is deleted)" do + before do + rate.user = user + rate.save! + user.destroy + rate.reload + end + + it { rate.user.should == DeletedUser.first } + end + end +end diff --git a/spec/models/labor_budget_item_spec.rb b/spec/models/labor_budget_item_spec.rb index 50323b3b0f..244ea33770 100644 --- a/spec/models/labor_budget_item_spec.rb +++ b/spec/models/labor_budget_item_spec.rb @@ -57,6 +57,32 @@ describe LaborBudgetItem do end end + describe :user do + describe "WHEN an existing user is provided" do + before do + item.save! + item.reload + item.update_attribute(:user_id, user.id) + item.reload + end + + it { item.user.should == user } + end + + describe "WHEN a non existing user is provided (i.e. the user has been deleted)" do + before do + item.save! + item.reload + item.update_attribute(:user_id, user.id) + user.destroy + item.reload + end + + it { item.user.should == DeletedUser.first } + it { item.user_id.should == user.id } + end + end + describe :valid? do describe "WHEN hours, cost_object and user are provided" do it "should be valid" do