Skip to content

Converts ActiveRecord::Base#new into a subclass-aware factory method for tables using STI

Notifications You must be signed in to change notification settings

arvanasse/sti_factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# StiFactory
# 
# StiFactory converts the #new method of an ActiveRecord class into a subclass-aware factory method.
#    The factory returns a new instance of the superclass or subclass based on the presence of the
#    inheritance_column in the attribute list when #new is called.  If the value of the
#    inheritance_column attribute is a valid subclass of the superclass then a new instance of the
#    subclass will be returned, otherwise a new instance of the superclass will be returned.
#
# The intention for this is to allow users to select the subclass in a form and allow the corresponding
#    controller to build or create an instance of the subclass without having to know that STI is 
#    involved.
#
#
# Example:
#    class Vehicle < ActiveRecord::Base
#      self.inheritance_column = 'vehicle_type'
#      has_sti_factory
#    end
#
#    class Car < Vehicle; end
#    class Truck < Vehicle; end
#    class MonsterTruck < Truck; end
#
#    Vehicle.new
#    #<Vehicle id: nil, vehicle_type: nil, ...>
#
#    Vehicle.new :vehicle_type => 'Truck'
#    #<Truck id: nil, vehicle_type: 'Truck', ...>
#
#    Vehicle.new :vehicle_type => 'MonsterTruck'
#    #<MonsterTruck id: nil, vehicle_type: 'MonsterTruck', ...>
#
############################################################################


Koinonia::StiFactory
- should provide an array of subclass names
- should include the base class name in the list of subclass names

Koinonia::StiFactory with the default inheritance column when instantiating a new object
- should return the subclass named in the type attribute if it is a valid subclass
- should return the specified class if no type attribute is supplied
- should return the specified class if the supplied type attribute is a valid subclass of the base class

Koinonia::StiFactory with the default inheritance column when creating a new object
- should persist an instance of the subclass named in the type attribute
- should persist an instance of the specified class if no type attribute is supplied
- should persist the specified class if the value supplied in the type attribute is not a valid subclass of the base class

Koinonia::StiFactory with a non-standard inheritance column when instantiating a new object
- should return the subclass named in the type attribute if it is a valid subclass
- should return the specified class if no type attribute is supplied
- should return the specified class if the supplied type attribute is a valid subclass of the base class

Koinonia::StiFactory with a non-standard inheritance column when creating a new object
- should persist an instance of the subclass named in the type attribute
- should persist an instance of the specified class if no type attribute is supplied
- should persist the specified class if the value supplied in the type attribute is not a valid subclass of the base class

About

Converts ActiveRecord::Base#new into a subclass-aware factory method for tables using STI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages