Git Product home page Git Product logo

class-table-inheritance's Introduction

Change log

  • Now you can inherits from and to modules like inherits_from 'Module::Model', see the the name of field must be module_model_id:integer thanks for Marc Remolt (https://github.com/mremolt).
  • Unit test

Note about version

If you are using Rails 2.3.8 or other version < 3, you have to use the version 1.1.x of this plugin, for Rails 3 you need to use the version 1.2.x or master of this plugin.

ClassTableInheritance 1.3.0

This is an ActiveRecord plugin designed to allow simple multiple table (class) inheritance.

This plugin was inspired by: inherits_from plugin => http://github.com/rwl4/inherits_from and Multiple Table Inheritance with ActiveRecord => http://mediumexposure.com/multiple-table-inheritance-active-record/

How to install

gem install class-table-inheritance

Example

Migrations

  create_table :products do |t|
    t.string :description, :null => false
	t.string :subtype # Only if you need access of both side see example
    t.decimal :price
    t.timestamps
  end
  
  create_table :books, :inherits => :product do |t|
    t.string :author, :null => false
  end
  
  create_table :videos, :inherits => :product do |t|
    t.string :year, :null => false
    t.string :genre, :null => false
  end

Models

  class Product < ActiveRecord::Base
	acts_as_superclass # only if you want top-down access.
  end

  class Book < ActiveRecord::Base
    inherits_from :product
  end

  class Video < ActiveRecord::Base
    inherits_from :product
  end

  book = Book.find(1)
  book.name => "Agile Development with Rails"
  book.author => "Dave Thomas"
  book.price => 19.00
  
  video = Video.find(2)
  video.name => "Inseption"
  video.year => "2010"
  video.genre => "SCI-FI" 
  video.price => 22.00
  
  book = Book.new
  book.name = "Hamlet"
  book.author = "Shakespeare, William"
  book.price => 14.00
  book.save

Module inheritance

Migrations

create_table :mod_users do |t|
  t.string :name, :null => false
end  

create_table :managers, :inherits => 'Mod::User' do |t|
  t.string :salary, :null => false
end

Models

class Mod::User < ActiveRecord::Base
end  

class Manager < ActiveRecord::Base
  inherits_from 'Mod::User'  
end

Top-down access (Polymorphic)

if you want to access product and get field in the subclass do you need to create a field subtype:string in superclass and ad acts_as_superclass in superclass and now you can do like this.

  product = Product.find 1  # This is a Book instance.
  product.author

  product = Product.find 2 # This is a Video instance.
  product.genre

if you need help contanct me: bfscordeiro (at) gmail.com .

Copyright (c) 2010 Bruno Cordeiro, released under the MIT license

class-table-inheritance's People

Contributors

brunofrank avatar mremolt avatar tvdeyen avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.