GitHub - Davidslv/preferences: Spree Preferences Model extracted into a gem!

1 min read Original article ↗

I couldn't find anything as robust as their solution so I decided to extract it into a gem.

class YourModel < ActiveRecord::Base
  serialize  :preferences # create a column as text in your database

  preference :color, :string, default: 'red'
  preference :number_of_pokemons, :integer, default: 151
end

@model = YourModel.create(preferences: {number_of_pokemons: 649})
@model.preferred_number_of_pokemons # => 649
@model.preferred_color # => "red"