diff --git a/app/models/environment.rb b/app/models/environment.rb index 62bc523..9bb67f3 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -696,4 +696,11 @@ class Environment < ActiveRecord::Base self.save! end + after_destroy :destroy_templates + def destroy_templates + [enterprise_template, community_template, person_template].compact.each do |template| + template.destroy + end + end + end diff --git a/app/models/person.rb b/app/models/person.rb index 53bbcf7..e89005c 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -12,6 +12,11 @@ class Person < Profile Friendship.find(:all, :conditions => { :friend_id => person.id}).each { |friendship| friendship.destroy } end + after_destroy :destroy_user + def destroy_user + self.user.destroy if self.user + end + # Sets the identifier for this person. Raises an exception when called on a # existing person (since peoples' identifiers cannot be changed) def identifier=(value) diff --git a/app/sweepers/article_sweeper.rb b/app/sweepers/article_sweeper.rb index 352aae8..30b3232 100644 --- a/app/sweepers/article_sweeper.rb +++ b/app/sweepers/article_sweeper.rb @@ -18,10 +18,12 @@ protected a.touch end end - blocks = (article.profile.blocks + article.profile.environment.blocks).select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} + blocks = article.profile.blocks + blocks += article.profile.environment.blocks if article.profile.environment + blocks = blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} env = article.profile.environment - if env.portal_community == article.profile + if env && (env.portal_community == article.profile) expire_fragment(env.portal_news_cache_key) end end diff --git a/test/unit/create_enterprise_test.rb b/test/unit/create_enterprise_test.rb index 1e599e8..461c8af 100644 --- a/test/unit/create_enterprise_test.rb +++ b/test/unit/create_enterprise_test.rb @@ -93,8 +93,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase should 'actually create an enterprise when finishing the task and associate the task requestor as its owner through the "user" association' do - Environment.destroy_all - environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain', :is_default => true) + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain') region = Region.create!(:name => 'My region', :environment_id => environment.id) validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator @@ -170,8 +169,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase end should 'validate that eveything is ok but the validator (target)' do - Environment.destroy_all - environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain', :is_default => true) + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain') region = Region.create!(:name => 'My region', :environment_id => environment.id) validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index acb9b85..dd7263b 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -272,7 +272,21 @@ class EnvironmentTest < Test::Unit::TestCase assert_equal boxes - env_boxes, Box.count assert_equal blocks - env_blocks, Block.count end - + + should 'destroy templates' do + env = fast_create(Environment) + templates = [mock, mock, mock] + templates.each do |item| + item.expects(:destroy) + end + + env.stubs(:person_template).returns(templates[0]) + env.stubs(:community_template).returns(templates[1]) + env.stubs(:enterprise_template).returns(templates[2]) + + env.destroy + end + should 'have boxes and blocks upon creation' do Environment.any_instance.stubs(:create_templates) # avoid creating templates, it's expensive environment = Environment.create!(:name => 'a test environment') diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index e9556bd..5571b3a 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -263,6 +263,13 @@ class PersonTest < Test::Unit::TestCase assert_not_includes p2.friends(true), p1 end + should 'destroy use when person is destroyed' do + person = create_user('testuser').person + assert_difference User, :count, -1 do + person.destroy + end + end + should 'return info name instead of name when info is setted' do p = create_user('ze_maria').person assert_equal 'ze_maria', p.name -- libgit2 0.21.2