Sha256: b761964a2e2172c6a654e0c83e80480573b0f4b380955e76b06c4898cda658ec

Contents?: true

Size: 959 Bytes

Versions: 7

Compression:

Stored size: 959 Bytes

Contents

# http://coderrr.wordpress.com/2008/04/22/building-the-right-class-with-sti-in-rails/

module ActiveRecord::StiInstantiation
  module ActMacro
    def instantiates_with_sti
      include InstanceMethods
      extend ClassMethods
      instantiates_with_sti?
    end

    def instantiates_with_sti?
      included_modules.include?(ActiveRecord::StiInstantiation::InstanceMethods)
    end
  end

  module InstanceMethods
  end

  module ClassMethods
    def new(*args, &block)
      options = args.first.is_a?(Hash) ? args.first : {}
      type = options[:type] || options['type']
      klass = type.is_a?(Class) ? type : type.constantize if type

      if type and klass != self
        raise "STI instantiation error: class #{klass.name} should be a descendant of #{self.name}" unless klass < self
        klass.new(*args, &block)
      else
        super
      end
    end
  end
end

ActiveRecord::Base.send :extend, ActiveRecord::StiInstantiation::ActMacro

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
adva-0.1.4 lib/rails_ext/active_record/sti_instantiation.rb
adva-0.1.3 lib/rails_ext/active_record/sti_instantiation.rb
adva-0.1.2 lib/rails_ext/active_record/sti_instantiation.rb
adva-0.1.1 lib/rails_ext/active_record/sti_instantiation.rb
adva-0.1.0 lib/rails_ext/active_record/sti_instantiation.rb
adva_cms-0.0.1 lib/rails_ext/active_record/sti_instantiation.rb
adva-0.0.1 adva_cms/lib/rails_ext/active_record/sti_instantiation.rb