Skip to content

ninjaconcept/custom_attributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CustomAttributes
================

(alpha release)

1)
Adds custom attributes through a CustomAttribute Model, but behaves like standard AR attributes.
Has support some basic validations.

2)
CustomValueWithUnit supports composed attributes of value and unit. 
Has support for validation of unit compatibility. 

3)
a DSL for creating attributed models, which brings also support for nested Custom Objects, wich are mapped to plain 'AR-Row-Models'.
Brings a taste of document-based DBs like Mongo or CloudDB to ActiveRecord.


DSL Example 
===========


class Design < ActiveRecord::Base

  acts_as_custom_attribute_container
  
  has_custom :width, :height, :unit=>'cm'
  
  has_custom :colors do
    has_custom :name, :string
  end
  
  has_custom :designer do
    has_custom :name, :string
    has_custom :website, :string
    has_custom :biography, :string #todo: :text
  end
end



Example
=======

(alternatively you can use a more verbose form)

class Item < ActiveRecord::Base
  acts_as_custom_attribute_container
end

class CarpetItem < Item
  
  has_custom_attributes :width, :length, :height, :class=>CustomValueWithUnit, :unit=>'cm'
  has_custom_attribute :sqm, :class=>CustomValueWithUnit, :unit=>'m**2'
  has_custom_attribute :weight, :class=>CustomValueWithUnit, :unit=>'kg'

end

class ShirtItem < Item
  
  has_custom_attribute :size, :class=>CustomString
  has_custom_attribute :weight, :class=>CustomValueWithUnit, :unit=>'kg'

  validates_presence_of :size
  
end



Migrations ( sorry, no generator yet )
==========

create_table "custom_attributes", :force => true do |t|
  t.string   "name"
  t.integer  "customizable_id"
  t.string   "customizable_type"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.decimal  "value_value",       :precision => 20, :scale => 10
  t.string   "value_unit"
  t.string   "type"
  t.boolean  "value_boolean"
end

add_index "custom_attributes", ["customizable_id", "customizable_type", "name"], :name => "index_on_custom_attributes_id_and_type_and_name"
add_index "custom_attributes", ["customizable_id", "customizable_type"], :name => "index_on_custom_attributes_id_and_type"

create_table "custom_objects", :force => true do |t|
  t.string   "type"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "customizable_type"
  t.integer  "customizable_id"
end


Todo
====
More dedicated custom_xx tables, at the moment we waste some space in the db, but should be no problem for normal apps.


Credits
=======

We use Stick::Units for the CustomValueWithUnit part
http://stick.rubyforge.org/


Copyright (c) 2010 [Ninja Concept/Marco Sehrer], released under the MIT license

About

plugin for dynamic attributes on your ActiveRecord models.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages