Get Rid of Your Spree Preferences Headaches Once and for All

A few days ago, while doing Code Review in one of the Spree projects from here at Crowd, I made the recommendation to create a “global” configuration object to centralize certain values that were being both assigned and used in class variables along different models (OMGosh !!!).

When asked for a reference on how to do it, my first impulse was to give the Spree Preferences Guide link. After a couple of minutes, I was required to do a pair programming session to help them directly with this since the documentation seems to not be so clear. Curious, because I remembered that the guide was quite clear about this, and yet, after reviewing it, I realized that it could lead to confusion.

That’s why, if you ever find yourself in a similar situation at some point, this little guide will be of great help.

Creating the configuration object

Well, you must first create the object that will contain our special configuration. My recommendation is to create it under the namespace /lib/spree because it seems the most logical and meaningful place.

Following the Spree guide, we are told that we must create our object that inherits from Spree::Preferences::Configuration where we can set preferences as we wish.