Project

meta_types

0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
DB-Configurable type description. Uses hstore on the models. A demo app is running at http://meta-types.herokuapp.com
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

MetaTypes¶ ↑

This is like hstore on steroids ;) It actually uses hstore for storage.

There is a demo app on heroku (code is here)

Mission Objective¶ ↑

As in the railscast mentioned above, you frequently get in the situation to store values in the database without knowing the exact structure of the required data in advance. This is one of the really cool features of NoSQL databases like MongoDB.

We often find that we need to configure the structure interactively. In traditional SQL databases, the EAV-Pattern (or antipattern, rather), is used, for example to configure product types in Magento. This leads to zillions of tables and to really ugly queries, which need to be aggressively cached for performance reasons.

Postgres offers THE SOLUTION to this problem, and we tried to make usage as simple as possible.

This is currently quite work-in-progress… Let’s see how things evolve :)

Usage¶ ↑

Gemfile¶ ↑

As always

gem 'meta_types'

or, for the brave:

gem 'meta_types', git: 'https://github.com/metaminded/meta_types'

in your Gemfile.

Migrations¶ ↑

Create a table that has a yourcoice_hstore column, in this case, we use properties_hstore

class CreateThings < ActiveRecord::Migration
  def change
    execute <<-SQL
      create table things(
        id      serial primary key,
        meta_type_id integer references meta_types(id),
        title    character varying,
        properties_hstore hstore,
        created_at timestamp without time zone,
        updated_at timestamp without time zone
      )
    SQL
  end
end

in the corresponding class, we declare that we want to mount the meta_type information with the name properties.

class Thing < ActiveRecord::Base
  attr_accessible :title, :meta_type

  meta_typed :properties

end

Then, get the necessary migrations:

$ rake meta_types:install:migrations

which installs the create table migrations for meta_type, meta_type_property and the mapping table meta_type_member.

Define the Meta Type¶ ↑

We then define a MetaType: (sid is string-id ;))

typ = MetaType.new(sid: 'address', title: 'My Fancy Address')
typ.meta_type_properties = [
  MetaTypeProperty.new(
    sid:               'age',
    label:             'Age',
    property_type_sid: 'integer',
    default_value:     '10'
  ),
  MetaTypeProperty.new(
    sid:               'name',
    label:             'Name',
    property_type_sid: 'string'
  ),
  MetaTypeProperty.new(
    sid:               'gender',
    label:             'Gender',
    property_type_sid: 'string',
    choices:           'male||female||other'
  ),
  MetaTypeProperty.new(
    sid:               'active',
    label:             'Active',
    property_type_sid: 'boolean',
    default_value:     'true'
  )
]

the available types for property_type_sid are

  • integer

  • string

  • text (multi-line stings, the only difference is the way that simple_form renders the input)

  • boolean

  • float

  • date

If the choices are set, simple_form can easily offer an select-box as input. The individual values are separated by ||.

Create meta_typed model¶ ↑

You can now use the defined meta_type to add the defined attributes to another model:

thing = Thing.new(title: "MetaTypes Rock!", meta_type: typ)
assert thing.save

the defined attributes are available on the properties pseudo-relation of the thing, e.g.:

thing.properties.age == 10
thing.properties.active == true

and using properties[sid], you can access the MetaPeroperty object encapsulating the actual data and attribute definition, e.g.:

thing.properties[:age].value == 10
thing.properties[:active].value == true
thing.properties[:active].label == 'Active'
thing.properties[:active].sid == 'active'

the properties can be set using

thing.properties.name = "Ryan Bates ;)"
thing.properties.gender = "male"

‘Mass Assignment’ also works so that you can construct a form like that:

<%= simple_form_for @thing do |f| %>
  <%= f.input :meta_type_id, as: :hidden %>
  <%= f.input :name %>
  <%= f.fields_for :properties do |ff| %>
    <% @thing.properties.each do |prop| %>
      <%= ff.input prop.sid, collection: prop.choices, label: prop.label %>
    <% end %>
  <% end %>
  <%= f.submit %>
<% end %>

or programmatically:

thing = Thing.new(
  title: "Har har",
  meta_type: typ,
  properties_attributes: {
    age:  125,
    name: 'Methusalix',
    gender: 'male',
    active: true
  }
)

The same works with update_attributes so that no special care needs to be taken in the controllers.

Querying/Finding¶ ↑

To allow easy finding, we add methods to the meta_typed model:

Thing.where_properties(age: 10)
Thing.where_properties_like(name: '%salix%')

They may of course be chained for they are just ARels on Thing

How is it done?¶ ↑

In the hstore column, the properties are saved in the form

sid -> value

where sid is the sid of the meta_type_property and the value (can only be a string in the db) is automatically converted.

Demo App (seriously!)¶ ↑

There is a demo app on heroku (code is here)

Bugs!¶ ↑

There are roughly 10^12 bugs in meta_types, although we do some testing (see test/). So if you hunt them, please let me know using the GitHub Bugtracker.

Contributing to meta_types¶ ↑

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

  • Feel free to send a pull request if you think others (me, for example) would like to have your change incorporated into future versions of meta_types.

License¶ ↑

Copyright © 2012 Peter Horn, metaminded UG

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.