Sha256: a56232f15c8870c80d656e3276d0540dfd8b8120537f40a423124c7ae1053c0c

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

Adds an add_extra_data method to ActiveRecord that invisibly includes an extra data table. Use with STI to keep your database clean. It removes the need to have lots of nullable columns.

Usage
---------
Use in Rails 3 app. Add to bundler:
gem "has_extra_data"

Tests
---------
Uses rspec for testing.
Run tests with rspec spec/*

Todo
---------
* Automatically load extra data class with main class.
* Automatically load associations in main class.
* Automatically pass attributes through to main class.

Example
---------

Usage:

    Account.each do |account|
      puts account.balance
    end

    BankAccount.each do |bank_account|
      puts bank_account.balance
      puts bank_account.bank.name
    end

Classes:
    class Account < ActiveRecord::Base
      before_create :set_initial_balance
  
    private
      def set_initial_balance
        self.balance = 0
        true
      end
    end

    class BankAccount < Account
      has_extra_data do
        belongs_to :bank
      end
    end

    class CreditCard < Account
      has_extra_data
    end

    class Bank < ActiveRecord::Base
      has_one :bank_account
    end

Database Schema:

    create_table :accounts do |t|
      t.float :balance
    end

    create_table :bank_account_data do |t|
      t.integer :bank_account_id, :null => false
      t.integer :bank_id, :null => false
      t.string :account_number, :null => false
      t.string :sort_code, :null => false
    end

    create_table :credit_card_data do |t|
      t.integer :credit_card_id, :null => false
      t.string :credit_card_number, :null => false
      t.date :expiry_date, :null => false
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
super_sti-0.2.1 README