Commit acd66894e08e9174c1efbcc31cb8332d17b6a48b
1 parent
04962ef3
Exists in
master
and in
29 other branches
environment: destroy child profiles when destroyed
Also includes migration to remove already orphaned profiles. (ActionItem2906)
Showing
4 changed files
with
18 additions
and
23 deletions
Show diff stats
app/models/environment.rb
@@ -168,7 +168,7 @@ class Environment < ActiveRecord::Base | @@ -168,7 +168,7 @@ class Environment < ActiveRecord::Base | ||
168 | 168 | ||
169 | # One Environment can be reached by many domains | 169 | # One Environment can be reached by many domains |
170 | has_many :domains, :as => :owner | 170 | has_many :domains, :as => :owner |
171 | - has_many :profiles | 171 | + has_many :profiles, :dependent => :destroy |
172 | 172 | ||
173 | has_many :organizations | 173 | has_many :organizations |
174 | has_many :enterprises | 174 | has_many :enterprises |
@@ -783,13 +783,6 @@ class Environment < ActiveRecord::Base | @@ -783,13 +783,6 @@ class Environment < ActiveRecord::Base | ||
783 | self.save! | 783 | self.save! |
784 | end | 784 | end |
785 | 785 | ||
786 | - after_destroy :destroy_templates | ||
787 | - def destroy_templates | ||
788 | - [enterprise_template, inactive_enterprise_template, community_template, person_template].compact.each do |template| | ||
789 | - template.destroy | ||
790 | - end | ||
791 | - end | ||
792 | - | ||
793 | after_create :create_default_licenses | 786 | after_create :create_default_licenses |
794 | def create_default_licenses | 787 | def create_default_licenses |
795 | License.create!(:name => 'CC (by)', :url => 'http://creativecommons.org/licenses/by/3.0/legalcode', :environment => self) | 788 | License.create!(:name => 'CC (by)', :url => 'http://creativecommons.org/licenses/by/3.0/legalcode', :environment => self) |
@@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
1 | +class RemoveOrphanProfiles < ActiveRecord::Migration | ||
2 | + def self.up | ||
3 | + profiles = Profile.joins('LEFT JOIN environments ON profiles.environment_id=environments.id').where('environments.id IS NULL') | ||
4 | + profiles.map(&:destroy) | ||
5 | + end | ||
6 | + | ||
7 | + def self.down | ||
8 | + say 'This migration can not be reverted.' | ||
9 | + end | ||
10 | +end |
test/unit/environment_test.rb
@@ -293,21 +293,6 @@ class EnvironmentTest < ActiveSupport::TestCase | @@ -293,21 +293,6 @@ class EnvironmentTest < ActiveSupport::TestCase | ||
293 | assert_equal blocks - env_blocks, Block.count | 293 | assert_equal blocks - env_blocks, Block.count |
294 | end | 294 | end |
295 | 295 | ||
296 | - should 'destroy templates' do | ||
297 | - env = fast_create(Environment) | ||
298 | - templates = [mock, mock, mock, mock] | ||
299 | - templates.each do |item| | ||
300 | - item.expects(:destroy) | ||
301 | - end | ||
302 | - | ||
303 | - env.stubs(:person_template).returns(templates[0]) | ||
304 | - env.stubs(:community_template).returns(templates[1]) | ||
305 | - env.stubs(:enterprise_template).returns(templates[2]) | ||
306 | - env.stubs(:inactive_enterprise_template).returns(templates[3]) | ||
307 | - | ||
308 | - env.destroy | ||
309 | - end | ||
310 | - | ||
311 | should 'have boxes and blocks upon creation' do | 296 | should 'have boxes and blocks upon creation' do |
312 | Environment.any_instance.stubs(:create_templates) # avoid creating templates, it's expensive | 297 | Environment.any_instance.stubs(:create_templates) # avoid creating templates, it's expensive |
313 | environment = Environment.create!(:name => 'a test environment') | 298 | environment = Environment.create!(:name => 'a test environment') |
test/unit/profile_test.rb
@@ -1888,4 +1888,11 @@ class ProfileTest < ActiveSupport::TestCase | @@ -1888,4 +1888,11 @@ class ProfileTest < ActiveSupport::TestCase | ||
1888 | assert !profile.may_display_location_to?(user) | 1888 | assert !profile.may_display_location_to?(user) |
1889 | end | 1889 | end |
1890 | 1890 | ||
1891 | + should 'destroy profile if its environment is destroyed' do | ||
1892 | + environment = fast_create(Environment) | ||
1893 | + profile = fast_create(Profile, :environment_id => environment.id) | ||
1894 | + | ||
1895 | + environment.destroy | ||
1896 | + assert_raise(ActiveRecord::RecordNotFound) {profile.reload} | ||
1897 | + end | ||
1891 | end | 1898 | end |