Skip to content

Commit

Permalink
Merge pull request collectiveidea#284 from naked-apps/fix_dependent_r…
Browse files Browse the repository at this point in the history
…estrict_with_exception

Fix dependent: :restrict_with_exception
  • Loading branch information
parndt committed Oct 30, 2014
2 parents f972964 + 3b73d56 commit cc882de
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Added information to the README regarding indexes to be added for performance [bdarfler]
* Modified associate_parents to add child objects to the parent#children collection [Tiago Moraes]
* Fixed `dependent: :destroy` when called from associated object. #162 [Kuldeep Aggarwal]
* Fix `dependent: :restrict_with_exception` not allowing a delete to occur. [Brendan Kilfoil]

2.1.6
* Fixed rebuild! when there is a default_scope with order [Adrian Serafin]
Expand Down
1 change: 1 addition & 0 deletions lib/awesome_nested_set/model/prunable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def destroy_or_delete_descendants
end
elsif acts_as_nested_set_options[:dependent] == :restrict_with_exception
raise ActiveRecord::DeleteRestrictionError.new(:children) unless leaf?
return true
elsif acts_as_nested_set_options[:dependent] == :restrict_with_error
unless leaf?
record = self.class.human_attribute_name(:children).downcase
Expand Down
16 changes: 12 additions & 4 deletions spec/awesome_nested_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1201,10 +1201,18 @@ def check_structure(entries, structure)
expect(Category.where(parent_id: root.id)).to be_empty
end

it 'restrict_with_exception should raise exception' do
Category.acts_as_nested_set_options[:dependent] = :restrict_with_exception
root = Category.root
expect { root.destroy! }.to raise_error ActiveRecord::DeleteRestrictionError, 'Cannot delete record because of dependent children'
describe 'restrict_with_exception' do
it 'raises an exception' do
Category.acts_as_nested_set_options[:dependent] = :restrict_with_exception
root = Category.root
expect { root.destroy! }.to raise_error ActiveRecord::DeleteRestrictionError, 'Cannot delete record because of dependent children'
end

it 'deletes the leaf' do
Category.acts_as_nested_set_options[:dependent] = :restrict_with_exception
leaf = Category.last
assert_equal leaf, leaf.destroy
end
end

describe 'restrict_with_error' do
Expand Down
16 changes: 12 additions & 4 deletions spec/models/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -819,10 +819,18 @@ def check_structure(entries, structure)
expect(User.where(parent_uuid: root.uuid)).to be_empty
end

it 'restrict_with_exception should raise exception' do
User.acts_as_nested_set_options[:dependent] = :restrict_with_exception
root = User.root
expect { root.destroy! }.to raise_error ActiveRecord::DeleteRestrictionError, 'Cannot delete record because of dependent children'
describe 'restrict_with_exception' do
it 'raises an exception' do
User.acts_as_nested_set_options[:dependent] = :restrict_with_exception
root = User.root
expect { root.destroy! }.to raise_error ActiveRecord::DeleteRestrictionError, 'Cannot delete record because of dependent children'
end

it 'deletes the leaf' do
User.acts_as_nested_set_options[:dependent] = :restrict_with_exception
leaf = User.last
assert_equal leaf, leaf.destroy
end
end

describe 'restrict_with_error' do
Expand Down

0 comments on commit cc882de

Please sign in to comment.