Add model preferences

Solidus comes with many model-specific preferences. They are configured to have default values that are appropriate for typical stores. Preferences can be set on any model that inherits from Spree::Base .

Note that model preferences apply only to the current model. To learn more about application-wide preferences, see the App configuration article.

We recommend keeping preferences to the minimum necessary. However, if your store requires additional preferences, you can create custom ones that have any number of arguments.

Define new preferences

You can define preferences for a model within the model itself:

Ruby
    
      module MyStore
  class SubscriptionRules < Spree::Base
    preference :hot_salsa, :boolean
    preference :dark_chocolate, :boolean, default: true
    preference :color, :string
  end
end

    
  

For each preference you define, a data type should be provided. The available types are:

  • boolean
  • string
  • password
  • integer
  • text
  • array
  • hash

An optional default value may be defined. (See the :dark_chocolate preference in the block above.) This is the value used unless another value has been set.

Add columns for your preferences

In order for your new preferences to persist, you need to add a column to the relevant model using a migration:

Ruby
    
      class AddPreferencesToSubscriptionRules < ActiveRecord::Migration[5.0]
  def change
    add_column :my_store_subscription_rules, :preferences, :text
  end
end

    
  

Your new preferences column should be the type text.

Then, you can run the migration:

Bash
    
      bundle exec rails db:migrate

    
  

Access your preferences

Now you can access their values from the model they are set on:

Ruby
    
      MyStore::SubscriptionRules.find(1).preferences
# => {:hot_salsa => nil, :dark_chocolate => true, :color => "grey"}

    
  

Or, the value of a specific preference:

Ruby
    
      MyStore::SubscriptionRules.find(1).preferences.fetch(:dark_chocolate)
# => true

    
  

Feedback

Solidus is an open source platform supported by the community. We encourage everyone using Solius to contribute back to the documentation and the code.

If you’re interested in contributing to the docs, get started with the contributing guidelines. If you see something that needs fixing and can’t do it yourself, please send us an email.