-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
document.destroy now returns false if before_destroy callback returns fa... #290
base: master
Are you sure you want to change the base?
Conversation
@@ -90,8 +90,7 @@ def destroy! | |||
end | |||
|
|||
def destroy | |||
destroy! | |||
true | |||
(destroy!)==false ? false : true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If destroy!
returns false, this line seems unnecessary. Just return the value of destroy!
or !!destroy!
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. It's an unnecessary check - although it makes sure it only ever returns true or false and not somethings else (like a Ripple::Document).
I'm not sure if the behavior demonstrated by the spec is actually behavior we want. @seancribbs and I had a conversation a while back about callbacks, and we both disliked the fact that ActiveRecord halts the action if the callback returns false. This conflates command/query separation semantics and, in my experience, can be a subtle source of bugs. In ripple we decided we didn't want that behavior. Furthermore, I'm not sure that # @private
def destroy!(*args, &block)
run_callbacks(:destroy) do
super
end
end I'm not totally sure about this, but I think that |
@myronmarston you could be right about the destroy actually succeeding and maybe the ActiveRecord conventions are not the best solution. So how to implement some checks before allowing a destroy? I'm currently using (trying to use...) a before_destroy to prevent a document from getting destroyed if it has links to some other documents and using before_destroy seemed like an elegant solution to me. |
@tpjg -- you should be able to override |
I have written a different spec and it seems that the destroy is not succeeding, so it is as expected from a typical ActiveRecord implementation. it "destroy should return false and not destroy the document when a callback returns false" do
User.before_destroy { false }
u = User.create!(:email => 'nobody@domain.com')
u.destroy.should be false
User.find(u.key).should be_an_instance_of(User)
end |
I'm rendering different views in my rails controller depending on the outcome of @record.destroy. It could be just me, I'm still a relative riak/ripple newbie but it appears that when a callback returned false this didn't get through. I've changed a single line and wrote (my first) RSpec test for it.