have a deleted_user_fallback mixin and cost_entry, labor_budget_item and rate using it

* deleted_user_fallback will return the deleted user upon asking for user when the user_id of the including model points to a user who no longer exists
pull/6827/head
Jens Ulferts 13 years ago
parent 72b7922e72
commit b69d6dca86
  1. 1
      app/models/cost_entry.rb
  2. 1
      app/models/labor_budget_item.rb
  3. 1
      app/models/rate.rb
  4. 23
      lib/costs/deleted_user_fallback.rb
  5. 11
      spec/models/cost_entry_spec.rb
  6. 30
      spec/models/default_hourly_rate_spec.rb
  7. 30
      spec/models/hourly_rate_spec.rb
  8. 26
      spec/models/labor_budget_item_spec.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"

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

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

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

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

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

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

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

Loading…
Cancel
Save