Add typed configuration options to Spree models using model preferences, including supported types, default values, and reading or writing stored values.
Model preferences allow you to easily extend Spree models with configuration options. Thanks to this you can store useful information on Spree models, eg.
To define a model preference, you need to add them to your model class.Make sure to generate a migration to add the preferences column to the table. This column will store the preferences in a serialized format.
class Spree::User < ApplicationRecord # ... you existing code ... # include the preferable module include Spree::Preferences::Preferable # define the preferences preference :language, :string, default: "English" preference :sms_marketing, :boolean, default: falseend
This will add a language preference to the User model. The preference will be stored in the newly created preferences column of the spree_users table. You can add more preferences to the model in the same way.
Once preferences have been defined for a model, they can be accessed either using the shortcut methods that are generated for each preference or the generic methods that are not specific to a particular preference.
This hash will contain the value for every preference that has been defined for the model instance, whether the value is the default or one that has been previously stored.