Statham

Statham is ActiveRecord library for easier access to attributes stored in JSON- serialized columns. It supports text (blob) or JSON type columns on MySQL, Sqlite and PostgreSQL.

Installation

Add this line to your application's Gemfile:

gem 'statham'

And then execute:

$ bundle

Usage

Mix in Statham::Document in models you want:

class Post < ActiveRecord::Base
  include Statham::Document
end

Do migration:

change_table :post do |t|
  t.text :metadata
end

Define schema for serialization:

class Post < ActiveRecord::Base
  include Statham::Document

  statham :metadata do |json|
    json.attribute :transporter, type: :string, default: 'frank'
    json.attribute :italian_job, type: :integer
    json.attribute :the_expandables, type: :boolean
  end
end

That's it.

The attributes defined in the JSON behaves like regular attributes. Thus You can use them on forms and views:

<%= form_for @post do |f| %>
  <%= f.checkbox :the_expandables %>
  <%= f.input :transporter %>
  <%= f.input :italian_job %>
<% end %>

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request