Get Rid of Your Spree Preferences Headaches Once and for All

Reading Time: 2 minutes

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.

Note: this is the way is commonly done in spree extensions.


module Spree
  class SpecialConfiguration < Spree::Preferences::Configuration
    preference: :name, :string
    preference: :special_url, :string
    preference: :host, :string, default: ''

And make sure it is loaded into the environment if it isn't loaded yet


module SpreeDemo
  class Application < Rails::Application
    # Some code here ...

    config.to_prepare do | config |
      # More code here ...

      config.autoload_paths += %W(#{config.root}/lib/spree/)

      # Yet some more code ...

Initializing the special values

To set the initial values, we need to create an initializer (duuh!!) where we will be setting them. We can either use the default ones we've defined or assign new ones.

According to your app requirements, you could assign them from ENV by using Rails Secrets or Figaro if using Heroku, for example.


module Spree
  SpreeDemo::Config =

SpreeDemo::Config[:name] = 'mumoc'
SpreeDemo::Config[:special_url] = "#{}/#{}"

And, last but not least:

Using the special configuration values

To make use of our configuration, we just request them as indicated by Spree guides.


class Spree::Profile

  # Code ...

  def some_awesome_method
    url = SpreeDemo::Config.special_url
    # More code ...

  # Even more code ...

Wrapping up

It's quite simple to setup wide-site preferences however, as Spree documentation lacks for "practical" example, it can become confusing.

Obviously, as each application has its own requirements, it is important to consider whether this type of configuration is feasible or not.

Questions, comments and suggestions are always welcome:

Thanks for reading.

twitter: mumoc |
github: mumoc |
skype: mumo.carlos

You May Also Like
Read More

3 ecommerce platforms for retailers

Reading Time: 2 minutes Non-store retailing continues strengthening worldwide, and according to Euromonitor analyst, Amanda Bourlier, in 2021 sales for more than…
Read More

Magento TDD with PHPSpec

Reading Time: 4 minutes Today, we are implementing the MageTest/MageSpec Module based on PHPSpec toolset. In this implementation, we have two different…
Read More

VTEX vs Shopify

Reading Time: 4 minutes VTEX and Shopify are two of the most popular eCommerce platforms. I will explain some of the differences…