Rails STI resouce routes routing

After hours of Googling and an ill timed browser reload while typing this the first time, I finally happened upon RoR ticket 10454 which details the problem I am having with inferred routes and STI models. I modified it to supposedly support any level of model nesting. Seems to work now. I inserted it into my environment.rb presently.

module ActionController
  module PolymorphicRoutes
    def build_named_route_call(records, namespace, inflection, options = {})
      records = Array.new([extract_record(records)]) unless records.is_a?(Array)

      # No STI
      #base_segment = "#{RecordIdentifier.send!("#{inflection}_class_name", records.pop)}_"

      # Hack
      record = records.pop.class
      until (record.superclass == ::ActiveRecord::Base)
        record = record.superclass
      end

      base_segment = "#{RecordIdentifier.send!("#{inflection}_class_name", record)}_"

      method_root = records.reverse.inject(base_segment) do |string, name|
      segment = "#{RecordIdentifier.send!("singular_class_name", name)}_"
      segment << string
    end

    action_prefix(options) + namespace + method_root + routing_type(options)
    end
  end
end

Post a Comment

Your email is never shared. Required fields are marked *

*
*