Commit dc4ccf34aba0f09727009b2cc983dc3a4b039c69

Authored by Antonio Terceiro
1 parent 28a544fd

Cleanup after destroying environment and people

app/models/environment.rb
... ... @@ -696,4 +696,11 @@ class Environment < ActiveRecord::Base
696 696 self.save!
697 697 end
698 698  
  699 + after_destroy :destroy_templates
  700 + def destroy_templates
  701 + [enterprise_template, community_template, person_template].compact.each do |template|
  702 + template.destroy
  703 + end
  704 + end
  705 +
699 706 end
... ...
app/models/person.rb
... ... @@ -12,6 +12,11 @@ class Person < Profile
12 12 Friendship.find(:all, :conditions => { :friend_id => person.id}).each { |friendship| friendship.destroy }
13 13 end
14 14  
  15 + after_destroy :destroy_user
  16 + def destroy_user
  17 + self.user.destroy if self.user
  18 + end
  19 +
15 20 # Sets the identifier for this person. Raises an exception when called on a
16 21 # existing person (since peoples' identifiers cannot be changed)
17 22 def identifier=(value)
... ...
app/sweepers/article_sweeper.rb
... ... @@ -18,10 +18,12 @@ protected
18 18 a.touch
19 19 end
20 20 end
21   - blocks = (article.profile.blocks + article.profile.environment.blocks).select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}}
  21 + blocks = article.profile.blocks
  22 + blocks += article.profile.environment.blocks if article.profile.environment
  23 + blocks = blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}}
22 24 blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)}
23 25 env = article.profile.environment
24   - if env.portal_community == article.profile
  26 + if env && (env.portal_community == article.profile)
25 27 expire_fragment(env.portal_news_cache_key)
26 28 end
27 29 end
... ...
test/unit/create_enterprise_test.rb
... ... @@ -93,8 +93,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase
93 93  
94 94 should 'actually create an enterprise when finishing the task and associate the task requestor as its owner through the "user" association' do
95 95  
96   - Environment.destroy_all
97   - environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain', :is_default => true)
  96 + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain')
98 97 region = Region.create!(:name => 'My region', :environment_id => environment.id)
99 98 validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id)
100 99 region.validators << validator
... ... @@ -170,8 +169,7 @@ class CreateEnterpriseTest &lt; Test::Unit::TestCase
170 169 end
171 170  
172 171 should 'validate that eveything is ok but the validator (target)' do
173   - Environment.destroy_all
174   - environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain', :is_default => true)
  172 + environment = Environment.create!(:name => "My environment", :contact_email => 'test@localhost.localdomain')
175 173 region = Region.create!(:name => 'My region', :environment_id => environment.id)
176 174 validator = Organization.create!(:name => "My organization", :identifier => 'myorg', :environment_id => environment.id)
177 175 region.validators << validator
... ...
test/unit/environment_test.rb
... ... @@ -272,7 +272,21 @@ class EnvironmentTest &lt; Test::Unit::TestCase
272 272 assert_equal boxes - env_boxes, Box.count
273 273 assert_equal blocks - env_blocks, Block.count
274 274 end
275   -
  275 +
  276 + should 'destroy templates' do
  277 + env = fast_create(Environment)
  278 + templates = [mock, mock, mock]
  279 + templates.each do |item|
  280 + item.expects(:destroy)
  281 + end
  282 +
  283 + env.stubs(:person_template).returns(templates[0])
  284 + env.stubs(:community_template).returns(templates[1])
  285 + env.stubs(:enterprise_template).returns(templates[2])
  286 +
  287 + env.destroy
  288 + end
  289 +
276 290 should 'have boxes and blocks upon creation' do
277 291 Environment.any_instance.stubs(:create_templates) # avoid creating templates, it's expensive
278 292 environment = Environment.create!(:name => 'a test environment')
... ...
test/unit/person_test.rb
... ... @@ -263,6 +263,13 @@ class PersonTest &lt; Test::Unit::TestCase
263 263 assert_not_includes p2.friends(true), p1
264 264 end
265 265  
  266 + should 'destroy use when person is destroyed' do
  267 + person = create_user('testuser').person
  268 + assert_difference User, :count, -1 do
  269 + person.destroy
  270 + end
  271 + end
  272 +
266 273 should 'return info name instead of name when info is setted' do
267 274 p = create_user('ze_maria').person
268 275 assert_equal 'ze_maria', p.name
... ...