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.

lib/spree/special_configuration.rb

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

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

config/application.rb

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 ...
    end
  end
end

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.

config/initializers/special_configuration.rb

module Spree
  SpreeDemo::Config = Spree::SpecialConfiguration.new
end

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

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.

models/spree/profile

class Spree::Profile

  # Code ...

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

  # Even more code ...
end

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

0 Shares:
You May Also Like
Read More

Setting Up EasyPost on Solidus

Reading Time: 5 minutes Solidus provides a flexible system to calculate shipping by accommodating a wide range of shipment pricing: from simple flat…