Serializing and scoping Mongoid criteria

So, while working on a project, I ran into a snag. I’ve got a partial which renders a list of images, and I want that partial to be auto-updated. That partial is used in multiple controllers and actions and is passed lots of different arbitrary data sets. How do you manage auto-updating that partial, and additionally, gain the ability to access that list’s criteria in other areas? I also have a ‘grid view’ of the images – I want to be able to pass my image list along to that and have it render it, but I still want to paginate (and on the grid view, pagination is done by different amounts).

Mongoid lets us build (much as ARel lets us do in ActiveRecord) criteria up, which consist of a few things, but mostly a selector (“Which records?”) and options (“How do you want them?”). So the answer is actually pretty straightforward – we serialize these objects and then use them to build our base criteria, on which we can then do pagination. Neat, right?

So, how do we do this? Easy, actually. Continue reading Serializing and scoping Mongoid criteria