Sha256: 7239e20c6a31d161099e12bf699d723d9f5e5e22fcb9c93cab63e2972a03573f

Contents?: true

Size: 1.18 KB

Versions: 10

Compression:

Stored size: 1.18 KB

Contents

require 'active_support/concern'

module Ardm
  module ActiveRecord
    module Inheritance
      extend ActiveSupport::Concern

      included do
        # ActiveRecord would prefer that you not use the column "type"
        # for anything other than single table inheritance.
        # The solution is to point ActiveRecord elsewhere.
        unless respond_to?(:inheritance_column=)
          class_attribute :inheritance_column
        end
        self.inheritance_column = "override-active-record-default-sti-column-type"
      end

      module ClassMethods
        def new(attrs={}, *a, &b)
          type = attrs && attrs.stringify_keys[inheritance_column.to_s]
          if type && type != name && type != self
            #puts "STI found for #{type} #{self}"
            con = type.is_a?(Class) ? type : type.constantize
            if con < self
              con.new(attrs, *a, &b)
            else
              raise "Tried to create subclass from #{type} (from key #{inheritance_column}) that is not a subclass of #{name}."
            end
          else
            #puts "No STI found for #{self} (#{attrs.inspect})"
            super
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ardm-0.2.7 lib/ardm/active_record/inheritance.rb
ardm-0.2.6 lib/ardm/active_record/inheritance.rb
ardm-0.2.5 lib/ardm/active_record/inheritance.rb
ardm-0.2.4 lib/ardm/active_record/inheritance.rb
ardm-0.2.3 lib/ardm/active_record/inheritance.rb
ardm-0.2.2 lib/ardm/active_record/inheritance.rb
ardm-0.2.1 lib/ardm/active_record/inheritance.rb
ardm-0.2.0 lib/ardm/active_record/inheritance.rb
ardm-0.1.0 lib/ardm/active_record/inheritance.rb
ardm-0.0.1 lib/ardm/active_record/inheritance.rb