diff --git a/app/models/environment.rb b/app/models/environment.rb index d736363..1d1a472 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -168,7 +168,7 @@ class Environment < ActiveRecord::Base # One Environment can be reached by many domains has_many :domains, :as => :owner - has_many :profiles + has_many :profiles, :dependent => :destroy has_many :organizations has_many :enterprises @@ -783,13 +783,6 @@ class Environment < ActiveRecord::Base self.save! end - after_destroy :destroy_templates - def destroy_templates - [enterprise_template, inactive_enterprise_template, community_template, person_template].compact.each do |template| - template.destroy - end - end - after_create :create_default_licenses def create_default_licenses License.create!(:name => 'CC (by)', :url => 'http://creativecommons.org/licenses/by/3.0/legalcode', :environment => self) diff --git a/db/migrate/20131121162641_remove_orphan_profiles.rb b/db/migrate/20131121162641_remove_orphan_profiles.rb new file mode 100644 index 0000000..0d5b0af --- /dev/null +++ b/db/migrate/20131121162641_remove_orphan_profiles.rb @@ -0,0 +1,10 @@ +class RemoveOrphanProfiles < ActiveRecord::Migration + def self.up + profiles = Profile.joins('LEFT JOIN environments ON profiles.environment_id=environments.id').where('environments.id IS NULL') + profiles.map(&:destroy) + end + + def self.down + say 'This migration can not be reverted.' + end +end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 580ff4c..505c6c4 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -293,21 +293,6 @@ class EnvironmentTest < ActiveSupport::TestCase assert_equal blocks - env_blocks, Block.count end - should 'destroy templates' do - env = fast_create(Environment) - templates = [mock, 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.stubs(:inactive_enterprise_template).returns(templates[3]) - - 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/profile_test.rb b/test/unit/profile_test.rb index 5da1312..6b79c2b 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1888,4 +1888,11 @@ class ProfileTest < ActiveSupport::TestCase assert !profile.may_display_location_to?(user) end + should 'destroy profile if its environment is destroyed' do + environment = fast_create(Environment) + profile = fast_create(Profile, :environment_id => environment.id) + + environment.destroy + assert_raise(ActiveRecord::RecordNotFound) {profile.reload} + end end -- libgit2 0.21.2