After extracting some common functionality into a Rails plugin, I was quite pleased with the result. The code was now contained and could be used within a model with a single line. Awesome! Except, a problem cropped up that was quite strange. In production mode, we noticed that after modifying some objects to not be visible, they still appeared in the interface as visible objects. Turns out there was an insidious defect in the plugin code I extracted.
The plugin code in question had two named scopes: one for the visible objects and one for the non-visible. The system uses a timestamp to determine if an object is visible or not, so part of the conditions of these named scopes was to check if the object had been marked as not visible earlier than the current time.
So, you see the problem, right?
Since this code is contained within the plugin, it will be evaluated once in production mode, meaning that the time will remain whatever it was when the plugin was loaded. This will cause the strange behavior that we saw. By using a lambda, we can ensure that the code is evaluated every time it is run and not just once.
Note the use of double curly braces. The outer is for the lambda and the inner if for the hash. This is necessary for this code to work correctly.
Thanks to Chris Chandler for helping me to figure this out.
Have you seen similar strange behavior in your Rails plugins?
Need web application development, maintenance for your existing app, or a third party code review?
Velocity Labs can help.Hire us!