-
-
Notifications
You must be signed in to change notification settings - Fork 357
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
Don't update a child destroyed via relation #261
Conversation
It's failing on Rails 3.2 due to them not having the right functions. I'll fix that up later. |
Thanks @brendon , will go through this today |
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.
Looks good.
Thanks @swanandp, have you had any experience with testing wether a callback is called or not with MiniTest? |
No, I mostly test side effects in such cases. |
Unfortunately there is no detectable side effect in this case because wether the item's siblings are shuffled or not can't be detected after the whole destroy process has run its course (since it happens as part of the callbacks). I'll keep investigating. |
Removes the need for an extra instance variable
Managed to fix things up a bit so that callback order isn't relied upon anymore. Also, we're now testing that the callback is called/not called via a mock. In Rails 3.2 we always shuffle because the functionality of knowing wether the parent initiated the destroy doesn't exist. |
@scottsherwood, all tests are passing now so I'm happy to merge this. Let me know once you've done your test this week and we can go from there :) |
Hi @brendon Sorry for the delay in checking this, after bundling commit 62f0a71 into my app, this is what I found: If you have two models involved TodoList -> HAS MANY -> TodoItem (The acts_as_list is on this final TodoItem class scoped by TodoList, like in your mocha tests) I my app I actually found that it reduced the sql queries down by 3 including the one which re-set the position. Which can easily cause mysql deadlocks due to trying to delete and update at the same time. So this works great :) If you have an 3 models involved TodoList -> HAS MANY -> TodoSection -> HAS MANY -> TodoItem ( The acts_as_list is on this final child class scoped by TodoSection) If you called TodoList.destroy when it gets down the chain to destroy the TodoItem, it does still do the position update sql before deleting the TodoItem. So as it is, there are some good efficiency gains here, but there are more to be had if wanted. (and if its possible to detect the destroy further up the chain) I've got the raw sql output should you want to see this. (Please just drop me an email.) Scott |
Hi @scottsherwood, no worries about the delay. Turns out the testing isn't working. When callbacks are executed, new objects are created for the children representing the records in the database. Those objects differ from the ones that I've already loaded into instance variables in the tests so I don't think there's a way to detect wether the callbacks are run as I can't see a way to hook into the objects that the destroy process is creating. Testing that a method is never invoked gives a false positive in this case because of course the other object never received the method call. Interesting about the grandparent problem. Theoretically it should work provided you're |
Hi @scottsherwood, upon further digging, I created a test that simulated the grandparent relationship with
I'm hoping it's just an implementation detail on your end that's stopping it from working. Can you show me the acts_as_list declarations? You need to be using the symbol or array method for specifying the scope. If you use anything else (e.g. a string) then we can't detect wether the If you're using a string to specify a non-id scope then you can also do this: |
The best we can do is test that they are called when individual items are deleted.
Ok, I've settled that we can't easily test if the callbacks are called in the dependent situation, but we can test at least that they are still called in the individual destruction situation which is the important case. Whether they are or not otherwise isn't too important other than in terms of extra SQL issued. I'm ready to merge once @scottsherwood confirms his info RE the grandparents. |
@scottsherwood, I'm happy with the testing for this one now. Let me know if you still have trouble. I've merged it into master and will release a new version once I hear from you :) |
@brendon I've looked into this further and confirm that the issue with the grandparent is on my-side and the updates look great. Thanks (I will drop you an email now) |
@scottsherwood, I've just released 0.9.3 with this change in it. |
@swanandp, I've cobbled together a solution. It's actually largely inspired from the Rails code where they don't update the counter cache if they know the child is being deleted via its parent's
dependent: :destroy
.I've become a bit stuck in how to test it. I want to test that the callback method either is or isn't called but apparently this isn't very straightforward in MiniTest. Do you have any suggestions?
@scottsherwood can you bundle this branch in your application and see if it works for you? If you're not the right Scott Sherwood, please let me know and I'll try the other one :)