This extension will allow Rails STI to work a normalized type column.
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.rvmrc
Gemfile
README
Rakefile
VERSION
has_normalized_sti.gemspec
init.rb

README

has_normalized_sti
==================

This extension will allow Rails STI to work a normalized type column.

For example

  create_table :people, :force => true do |t|
    t.string  :full_name
    t.integer :type_id
  end

  create_table :person_types, :force => true do |t|
    t.string :type_name
  end

  class Person < ActiveRecord::Base
    has_normalized_sti
  end

  class PersonType < ActiveRecord::Base
  end

  after calling has_normalized_sti:
  * type - returns the name of the class of the type just as regular STI
  * type= - set the type to something specific like regular STI
  * normal_type - the Type object through the relation


Configuration options are:
  * type_class_name - belong_to this model for the type storage
    (default: #{class_name}Type)
  * foreign_key - specifies the column for id of the type (default: type_id)
  * type_column - specifies the column name for the type string on the types table
    (default: type_name)

Override example

  create_table :people, :force => true do |t|
    t.string  :full_name
    t.integer :special_type_id
  end

  create_table :person_types, :force => true do |t|
    t.string :special_person_type
  end

  class SpecialPerson < ActiveRecord::Base
    set_table_name :people
    has_normalized_sti :type_class_name => 'SpecialPersonType', :type_column => 'special_person_type', :foreign_key => 'special_type_id'
  end

  class SpecialPersonType < ActiveRecord::Base
    set_table_name :person_types
  end