• Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A zero-configuration recursive Hash for storing a tree of options in a serialized ActiveRecord column.

StoreConfigurable

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.

Recursive Github Kitty

Build Status

Installation

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'

Our 4.0.x versions target both Rails 4.0 and 4.1 only.

Setup

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 store_configurable method.

class User < ActiveRecord::Base
  store_configurable
end

Usage

Our 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

Dirty Hooks

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

Hash Syntax

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'

Stored Data

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

Todo

  • Incorporate an option to compress serialized data. Gzip, MessagePack, Etc...

Other Solutions

  • StoreField - Similar approach but no dirty tracking and still requires manual key configs.

Contributing

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

License

  • Released under the MIT license thanks to Decisiv, Inc.
  • Copyright (c) 2014 Ken Collins

More Repositories

1

experts

Experts.js is the easiest way to create and deploy OpenAI's Assistants and link them together as Tools to create advanced Multi AI Agent Systems with expanded memory and attention to detail.
JavaScript
939
star
2

less-rails-bootstrap

The most popular front-end framework for developing responsive, mobile first projects on the web for Rails asset pipeline.
Ruby
561
star
3

minitest-spec-rails

🍱 Make Rails Use MiniTest::Spec!
Ruby
395
star
4

less-rails

👎 🚋 Less.js For Rails
Ruby
340
star
5

holy_grail_harness

🙏 A curated Rails application prototype that focuses on simple test patterns for Ruby & JavaScript!
Ruby
142
star
6

grouped_scope

GroupedScope: Has Many Associations IN (GROUPS)
Ruby
59
star
7

named_seeds

🆔🌱🌱 Replace ActiveRecord Fixtures With #{YourFactories}
Ruby
46
star
8

mini_shoulda

A minimal shoulda DSL built on top of MiniTest::Spec.
Ruby
30
star
9

pdf-writer

Pure Ruby tools for working with PDF documents.
Ruby
29
star
10

less-rails-bootstrap-test

Less Rails Bootstrap Test Project
Ruby
28
star
11

lambda-rag

LambdaRAG is a Retrieval Augmented Generation Chat AI Demo. Please read the full RAGs to Riches blog series.
JavaScript
24
star
12

mini_backtrace

MiniBacktrace allows you to take advantage of the Rails.backtrace_cleaner when using MiniTest.
Ruby
15
star
13

autotest_railsplugin

Use autotest on your rails plugin development.
Ruby
15
star
14

sprockets-blackcoffee

Render CoffeeScript files with the --bare option. Useful for JS spec helpers.
Ruby
13
star
15

mini_specunit

Make Test::Unit::TestCase a subclass of of MiniTest::Spec with this simple shim.
Ruby
12
star
16

llamafile-on-lambda

Serverless AI Inference with Gemma 2 using Mozilla's llamafile on AWS Lambda
JavaScript
11
star
17

named_scope

A thorough backport of NamedScope that can be used with Rails 1.2.6 and 2.0.4
Ruby
10
star
18

remote_shared_cache

A custom capistrano deploy strategy that creates a SVN export in a shared location.
7
star
19

holygrail_rails31

A Capybara-WebKit HolyGrail Example For Rails 3.1
Ruby
6
star
20

unremarkable-ideogram-assistant

Consistent On-Brand Artwork using Ideogram + OpenAI Assistants
JavaScript
5
star
21

iTunes-Loved-Alfred-Workflow

💞 🎵 Easily Toggle iTunes 12 Loved State
4
star
22

GemTracker

Track downloads for your ruby gems.
JavaScript
3
star
23

macports

MetaSkills MacPorts Repository
Tcl
2
star
24

current_fu

CurrentFu gives a standard interface to the current controller instance in your models with automatic delegation to #current_* named methods.
Ruby
2
star
25

metaskills.github.io

MetaSkills.net Blog http://metaskills.net/
HTML
2
star
26

rack-zombieshotgun

Shoot zombies in the head with rack 404 middleware.
Ruby
2
star
27

unremarkable-bespoke-ui

JavaScript
1
star
28

rails31_playground

Playing Around With Rails 3.1
Ruby
1
star
29

learn_to_program

757rb Learn To Program 2
Ruby
1
star
30

holygrail_rails23

A Capybara-WebKit HolyGrail Example For Rails 2.3
Ruby
1
star
31

quickie.js

Quickie.js - Prototype wrapper for QuickTime
JavaScript
1
star
32

757studio

Simple Rails Site For 757Studio.org
Ruby
1
star
33

github-readme-stats-lambda

Thanks! https://github.com/anuraghazra/github-readme-stats
JavaScript
1
star