A zero-configuration recursive Hash for storing a tree of options in a serialized ActiveRecord column. Includes self aware hooks that delegate dirty/changed state to your configs owner. Read my article A Lesson In Recursion In Ruby if you are interested in how this library works.
Install the gem with bundler. We follow a semantic versioning format that tracks ActiveRecord's minor version. So this means to use the latest 3.2.x version of StoreConfigurable with any ActiveRecord 3.2 version.
gem 'store_configurable', '~> 4.0.0'
4.0.x versions target both Rails 4.0 and 4.1 only.
To use StoreConfigurable, you must create a
_config column in the model's table. Make sure that you declare this column as a text type, so there's plenty of room.
class AddStoreConfigurableField < ActiveRecord::Migration def up add_column :users, :_config, :text end def down remove_column :users, :_config end end
Next declare that your model uses StoreConfigurable with the
class User < ActiveRecord::Base store_configurable end
config method is your gateway to StoreConfigurable and unlike ActiveRecord's new Store object in 3.2, there is no configuration needed to start using it for any property. It will dynamically expand for every property or namespace. This allows you or other plugins' configurations to be grouped in logical nodes. All examples below assume that StoreConfigurable is being used on a User instance as shown in the setup above.
@user.config.remember_me = true @user.config.sortable_tables.column = 'created_at' @user.config.sortable_tables.direction = 'asc' @user.config.you.should.never.need.to.do.this.but.you.could.if.you.wanted.to = 'deep_value' @user.save
StoreConfigurable is smart enought to let your parent object know when it changes. It is not dumb either. It will only trigger changes if the values you set are different, are new, or change the configs state. Some examples assuming the saved record's data above.
@user = User.find(42) @user.config_changed? # => false @user.config.remember_me = true # Same value @user.config_changed? # => false @user.config.sortable_tables.column = 'updated_at' @user.config.sortable_tables.direction = 'desc' @user.config_changed? # => true
The StoreConfigurable data objects supports most
Hash methods with the exception of a few that rely on making a copy of the data, like
dup. This means you can delete whole branches of data or itterate over your data collection. Again, StoreConfigurable reports all changes to the owner object via ActiveRecord's dirty support.
@user.config.sortable_tables.delete # Deletes this node/namespace. @user.config.clear # Hash method to purge. @user.config_changed? # => true
Choose Your Style
You can choose to get or set config values via any method or hash key syntax you choose. It really does not matter! This means you can mix and match dot property notation, hash string or symbol syntax and it will just work.
@user.config.color = '#c1c1c1' @user.config['remember_me'] = true @user.config[:sortable_tables].direction = 'asc' @user.config.sortable_tables['column'] = 'updated_at' @user.config['color'] # => '#c1c1c1' @user.config[:color] # => '#c1c1c1' @user.config.remember_me # => true @user.config.sortable_tables[:direction] # => 'asc' @user.config[:sortable_tables][:column] # => 'updated_at'
StoreConfigurable persists your configuration data in YAML format to the
_config text column. We use Ruby's
YAML::Omap type on the backend so we can decouple our datastore from our proxy object manager. This means you can easily load this data via other means if you want to.
--- !omap - :remember_me: true - :sortable_tables: !omap - :column: created_at - :direction: asc - :you: !omap - :should: !omap - :never: !omap - :need: !omap - :to: !omap - :do: !omap - :this: deep_value
- Incorporate an option to compress serialized data. Gzip, MessagePack, Etc...
- StoreField - Similar approach but no dirty tracking and still requires manual key configs.
StoreConfigurable is fully tested with ActiveRecord 3.2 to 4.0 and upward. If you detect a problem, open up a github issue or fork the repo and help out. After you fork or clone the repository, the following commands will get you up and running on the test suite.
$ bundle install $ bundle exec appraisal update $ bundle exec appraisal rake test
We use the appraisal gem from Thoughtbot to help us generate the individual gemfiles for each ActiveSupport version and to run the tests locally against each generated Gemfile. The
rake appraisal test command actually runs our test suite against all ActiveRecord versions in our
Appraisal file. If you want to run the tests for a specific ActiveRecord version, use
rake -T for a list. For example, the following command will run the tests for Rails 3.2 only.
$ bundle exec appraisal activerecord40 rake test
- Released under the MIT license thanks to Decisiv, Inc.
- Copyright (c) 2014 Ken Collins