From a80b7ee4729bf551ecb8d3e11e5ecfb04485aeb5 Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Mon, 4 Jun 2012 14:18:03 +0200 Subject: [PATCH] deleting the user sets the auhorship of associated cost object to the deleted user --- app/models/cost_object.rb | 10 +++ spec/models/user_deletion_spec.rb | 107 ++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 spec/models/user_deletion_spec.rb diff --git a/app/models/cost_object.rb b/app/models/cost_object.rb index 6de68b3bd5..4fc715efda 100644 --- a/app/models/cost_object.rb +++ b/app/models/cost_object.rb @@ -27,6 +27,9 @@ class CostObject < ActiveRecord::Base validates_length_of :subject, :maximum => 255 validates_length_of :subject, :minimum => 1 + User.before_destroy do |user| + replace_author_with_deleted_user user + end def before_validation self.author_id = User.current.id if self.new_record? @@ -150,4 +153,11 @@ class CostObject < ActiveRecord::Base return "issue cost_object" end + private + + def self.replace_author_with_deleted_user(user) + substitute = DeletedUser.first + + self.update_all ['author_id = ?', substitute.id], ['author_id = ?', user.id] + end end diff --git a/spec/models/user_deletion_spec.rb b/spec/models/user_deletion_spec.rb new file mode 100644 index 0000000000..635fc8bbee --- /dev/null +++ b/spec/models/user_deletion_spec.rb @@ -0,0 +1,107 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe User, "#destroy" do + let(:user) { Factory.create(:user) } + let(:user2) { Factory.create(:user) } + let(:substitute_user) { DeletedUser.first } + + before do + user + user2 + end + + shared_examples_for "updated journalized associated object" do + before do + User.current = user2 + associations.each do |association| + associated_instance.send(association.to_s + "=", user2) + end + associated_instance.save! + + User.current = user # in order to have the content journal created by the user + associated_instance.reload + associations.each do |association| + associated_instance.send(association.to_s + "=", user) + end + associated_instance.save! + + user.destroy + associated_instance.reload + end + + it { associated_class.find_by_id(associated_instance.id).should == associated_instance } + it "should replace the user on all associations" do + associations.each do |association| + associated_instance.send(association).should == substitute_user + end + end + it { associated_instance.journals.first.user.should == user2 } + it "should update first journal changes" do + associations.each do |association| + associated_instance.journals.first.changes[association.to_s + "_id"].last.should == user2.id + end + end + it { associated_instance.journals.last.user.should == substitute_user } + it "should update second journal changes" do + associations.each do |association| + associated_instance.journals.last.changes[association.to_s + "_id"].last.should == substitute_user.id + end + end + end + + shared_examples_for "created journalized associated object" do + before do + User.current = user # in order to have the content journal created by the user + associations.each do |association| + associated_instance.send(association.to_s + "=", user) + end + associated_instance.save! + + User.current = user2 + associated_instance.reload + associations.each do |association| + associated_instance.send(association.to_s + "=", user2) + end + associated_instance.save! + + user.destroy + associated_instance.reload + end + + it { associated_class.find_by_id(associated_instance.id).should == associated_instance } + it "should keep the current user on all associations" do + associations.each do |association| + associated_instance.send(association).should == user2 + end + end + it { associated_instance.journals.first.user.should == substitute_user } + it "should update the first journal" do + associations.each do |association| + associated_instance.journals.first.changes[association.to_s + "_id"].last.should == substitute_user.id + end + end + it { associated_instance.journals.last.user.should == user2 } + it "should update the last journal" do + associations.each do |association| + associated_instance.journals.last.changes[association.to_s + "_id"].first.should == substitute_user.id + associated_instance.journals.last.changes[association.to_s + "_id"].last.should == user2.id + end + end + end + + describe "WHEN the user updated a cost object" do + let(:associations) { [:author] } + let(:associated_instance) { Factory.build(:variable_cost_object) } + let(:associated_class) { CostObject } + + it_should_behave_like "updated journalized associated object" + end + + describe "WHEN the user created a cost object" do + let(:associations) { [:author] } + let(:associated_instance) { Factory.build(:variable_cost_object) } + let(:associated_class) { CostObject } + + it_should_behave_like "created journalized associated object" + end +end