Skip to content

Commit f0b9512

Browse files
committed
Merge pull request #604 from cerebris/pr/518
Check that destroys actually succeed.
2 parents ce1f2e1 + 7dbb0ab commit f0b9512

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

lib/jsonapi/resource.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ def _save
197197
end
198198

199199
def _remove
200-
@model.destroy
201-
200+
unless @model.destroy
201+
fail JSONAPI::Exceptions::ValidationErrors.new(self)
202+
end
202203
:completed
203204
end
204205

test/controllers/controller_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,14 @@ def test_update_bad_attributes
16531653
assert_response :bad_request
16541654
end
16551655

1656+
def test_delete_with_validation_error
1657+
post = Post.create!(title: "can't destroy me", author: Person.first)
1658+
delete :destroy, { id: post.id }
1659+
1660+
assert_equal "can't destroy me", json_response['errors'][0]['title']
1661+
assert_response :unprocessable_entity
1662+
end
1663+
16561664
def test_delete_single
16571665
initial_count = Post.count
16581666
delete :destroy, {id: '4'}

test/fixtures/active_record.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ class Post < ActiveRecord::Base
282282

283283
validates :author, presence: true
284284
validates :title, length: { maximum: 35 }
285+
286+
before_destroy :destroy_callback
287+
288+
def destroy_callback
289+
if title == "can't destroy me"
290+
errors.add(:title, "can't destroy me")
291+
return false
292+
end
293+
end
285294
end
286295

287296
class SpecialPostTag < ActiveRecord::Base

0 commit comments

Comments
 (0)