Skip to content

coffeeaddict/has_eav

Repository files navigation

has_eav

Description

Enabled EAV behaviour on ActiveRecord (or is at least supposed to).

This was inspired by github.com/visfleet/acts_as_eav_model but is a more minimized approach and is less automated.

EAV

Stands for Entity-Attribute-Value and is a database design pattern to unload attributes from a table.

From wikipedia:

Entity-attribute-value model (EAV) is a data model to describe entities where the number of attributes (properties, parameters) that can be used to describe them is potentially vast, but the number that will actually apply to a given entity is relatively modest. In mathematics, this model is known as a *sparse matrix*. EAV is also known as object-attribute-value model and *open schema*.

Author

name

Hartog de Mik (aka: coffeeaddict, coffeeaddict_nl)

email

name.downcase.gsub(“ ”,“.”)@gmail.com

source

github.com/coffeeaddict/has_eav

Installation

gem install has_eav

Setup

In your model

class Post < ActiveRecord::Base
  has_many :tags
  belongs_to :user
  has_eav :through => :post_attribute do
    eav_attribute :author_name
    eav_attribute :author_age, Integer
    eav_attribute :author_email
  end
end

Now, generate a model to hold the attributes

rails generate model PostAttribute post:references name:string value:string

And you are good to go.

Usage

Usage of EAV

For the general usage of EAV, thread wisely. Be careful and suspicious about your needs.

Usage of has_eav

Once you have setup +has_eav :through+ on your model, you can call the defined attributes as if they where present on your model. You should be able to treat your model as if the defined attributes where present in the database.

In contradiction to acts_as_eav_model, has_eav will not allow for an all open structure. You must define the attributes to be required.

STI

has_eav works in an STI setting, and you can specify more attributes in the inheriting class. Since the root class is responsible for the relationship to the attributes class, you should not specify any options in the STI class

class PostponedPost < Post
  has_eav do
    eav_attribute :release_date
  end
end

Instance eav_attributes

If you create a method instance_eav_attributes in your model, has_eav will recognize these attributes as well.

This might be useful for state machines or STI classes where you want to have different attributes based on the current state or type of the instance.

Changelog

1.1.1
  • Make sure the EAV attributes make it into the serializable_hash

  • Make sure the nilified attributes are removed from the DB

1.1.0
  • Casting of dates and times added

1.0.1
  • STI works

1.0.0
  • first release

Contributing to has_eav

  • 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.

TODO

  • Create a nice generator

  • Put type casting on instance eav attributes

Copyright © 2010 Hartog C. de Mik. See LICENSE.txt for further details.

About

Straight forward EAV behaviour for Rails3

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages