Skip to content
This repository has been archived by the owner on Apr 22, 2018. It is now read-only.

aurorasoft/json_fields

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON-Stored Fields

Allows for a group of “unimportant” fields to be stored as a JSON hash, rather than as real database columns.

This is useful in cases where you have many classes inheriting from a base class, but with drastically different fields which would clutter up the database.

Obviously this prevents you from searching for these records based upon this data.

However, it does enable you to dynamically add and remove fields from your models at runtime. For example, you can allow the user to specify extra fields for a model, and then get and set them at runtime using json_field_get() and json_field_set().

Installation

ruby script/plugin install git://github.com/aurorasoft/json_fields.git

Usage

Fields are defined using the following syntax :

has_json_field :field1, :field2, :field3

These fields can then be accessed as per usual, but are stored as a JSON hash in the “json_field” column (which you will need to add as a :text field).

Example for migration:
add_column :pages, :json_field, :text

If you don’t like the field named “json_field”, you can change it using :

set_json_field :another_field_here

Then, all data will be stored in the specified field.

Example

An entirely contrived example:

class TestBase < ActiveRecord::Base
end

class SimpleTest < TestBase
  has_json_field :your_name, :your_quest, :your_favourite_colour
end

class ComplexTest < TestBase
  has_json_field :square_root_of_pi, :distance_to_moon_in_meters
end

class ImpossibleTest < TestBase
  has_json_field :air_speed_velocity_of_an_unladen_swallow
end

Runtime-Defined Fields

In cases where you need to dynamically add and use fields at runtime (eg. custom fields) you can also set and get arbitrary fields using:

json_field_get(:field_name, 'foo') # => "foo"
json_field_get(:field_name)        # => "foo"

Caveats

  • Internally we use .to_json, which means that symbols will be converted to strings. As such, you may need to take care.

  • Any Rails code handling whether the instance has changed will likely not report changes to the JSON fields.

Licensing

Copyright © 2008-2009 Aurora Software (www.aurorasoft.com.au), released under the MIT license

About

Rails plugin to allow fields to be added dynamically (and at run time) which are stored in a single field as JSON data

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages