chaunce / acts_as_tableless

tableless activerecord objects that support associations and validations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ActsAsTableless

Create tableless activerecord objects that support associations and validations.

Install:

  1. add gem 'acts_as_tableless' to your gemfile

  2. run bundle install

  3. add acts_as_tableless to a activerecord model

Usage:

 class User < ActiveRecord::Base
   has_many :shapes
   has_many :user_colors
   has_many :colors, :through => :user_colors
   has_one :size
   belongs_to :number
   attr_accessible :name, :number_id, :letter_id
 end

 class Color < ActiveRecord::Base
   acts_as_tableless
   column :id, :integer
   column :name, :string
   attr_accessible :id, :name
   belongs_to :user_colors
 end

 class Letter < ActiveRecord::Base
   acts_as_tableless
   column :id, :integer
   column :name, :string
   column :user_id, :integer
   attr_accessible :id, :name, :user_id
   has_and_belongs_to_many :users
 end

 class Number < ActiveRecord::Base
   acts_as_tableless
   column :id, :integer
   column :name, :string
   attr_accessible :id, :name
   has_many :users
 end

 class Shape < ActiveRecord::Base
   acts_as_tableless
   column :id, :integer
   column :name, :string
   column :user_id, :integer
   attr_accessible :id, :name, :user_id
   belongs_to :user
 end

 class Size < ActiveRecord::Base
   acts_as_tableless
   column :id, :integer
   column :name, :string
   column :user_id, :integer
   attr_accessible :id, :name, :user_id
   belongs_to :user
 end

 class UserColor < ActiveRecord::Base
	  belongs_to :user
	  belongs_to :color
	  attr_accessible :user_id, :color_id
 end

Examples:

@user = User.create(:name => "User")

@user.shapes.create(:name => "Octagon") #=> #<Shape id: 1, name: "Octagon", user_id: 1>

@user.colors << Color.create([{:name => "Green"},{:name => "Blue"}]) #=> [#<Color id: 1, name: "Green">, #<Color id: 2, name: "Blue">]

@user.size.create(:name => "Large") #=> #<Size id: 1, name: "Large", user_id: 1>

@user.number = Number.create(name: "One") #=> #<Number id: 1, name: "One">

More stuff in the test file.

Notes:

This gem relies on String#singularize which is known to have some problems. To correct errors such as:

uninitialized constant Statu (NameError)

please add something similar to:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.singular("statuses", "status")
  inflect.singular("status", "status")
end

to your environment.rb file before the Application.initialize! line

Testing:

First setup the test database.

cd test/dummy
rake db:setup && db:test:prepare

Then from the root dir run the tests with Rake.

cd ../.. # from test/dummy
rake test

About

tableless activerecord objects that support associations and validations

License:MIT License


Languages

Language:Ruby 98.4%Language:JavaScript 1.6%