* 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 existspull/6827/head
parent
72b7922e72
commit
b69d6dca86
@ -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 |
@ -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 |
Loading…
Reference in new issue