diff --git a/app/models/account.rb b/app/models/account.rb index 5e0b6600..b50e9aa3 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -47,12 +47,12 @@ class Account def remove_memberships! self.sites.each do |site| - site.memberships.delete_if { |m| m.account_id == self._id } + membership = site.memberships.where(:account_id => self._id).first - if site.admin_memberships.empty? + if site.admin_memberships.size == 1 && membership.admin? raise I18n.t('errors.messages.needs_admin_account') else - site.save + membership.destroy end end end diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index b57d614a..c8f429a8 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -47,13 +47,14 @@ describe Account do end it 'should also delete memberships' do - Site.any_instance.stubs(:admin_memberships).returns(['junk']) + Site.any_instance.stubs(:admin_memberships).returns(['junk', 'dirt']) + @site_1.memberships.first.expects(:destroy) + @site_2.memberships.first.expects(:destroy) @account.destroy - @site_1.memberships.should be_empty - @site_2.memberships.should be_empty end it 'should raise an exception if account is the only remaining admin' do + @site_1.memberships.first.stubs(:admin?).returns(true) @site_1.stubs(:admin_memberships).returns(['junk']) lambda { @account.destroy