My favorites | Sign in
Project Logo
                
Feeds:
People details
Project owners:
  bkeene

HasMagicColumns

Extends ActiveRecord models and adds "magic" columns and attributes to a model. Model classes or instances can maintain completely unique and separate data and are able to access this data through the standard ActiveRecord style interface.

This is useful in multi-user or multi-account applications where each user can "customize" the attributes they would like to use without managing multiple schema or installations.

This plugin was written by and maintained by Brandon Keene, a professional Rails developer working in San Francisco, CA.

Installation

You need to create table definitions for included column and attribute models.

Generate the migration using the included task:

  rake has_magic_columns:db:create

Run the standard migrate task to update your schema:

  rake db:migrate

You're ready to start using Has Magic Columns!

- Brandon Keene

Example

class Person < ActiveRecord::Base
  has_magic_columns
end

# Create a Person
@bob = Person.create(:email => "bob@example.com")

# Add some MagicColumns
@bob.magic_columns << MagicColumn.create(:name => "first_name")
@bob.magic_columns << MagicColumn.create(:name => "last_name")
@bob.magic_columns << MagicColumn.create(:name => "birthday", :datatype => "date")

# Give @bob some magic...
@bob.first_name = "Bob"
@bob.last_name = "Magic!"
@bob.birthday = Date.today

# Save @bob like normal
@bob.save

# Find @bob and inspect him
@bob = Person.find(@bob.id)
@bob.first_name	#=> "Bob"
@bob.last_name	#=> "Magic!"
@bob.birthday	#=> #<Date: 4908497/2,0,2299161>

Inherit from "Template" Columns

A child can "inherit" magic columns from its parent. You can use container models to provide a column template for contained objects. For example, an Account has_magic_columns and has_many :people. A Person inherits magic columns from its account:

class Person < ActiveRecord::Base
  belongs_to :account
  has_magic_columns :inherit => :account
end

Children people now all share the columns of their parent account. You don't need to use these magic columns on the Account of course. It's just a convenient and logical way to provide column templating.









Hosted by Google Code