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

3 Comments

  1. Posted 10/20/2008 at 4:26 pm | Permalink

    For rails 2.1, this code doesn’t quite work. I have tweaked it, and this seems to work for me. Just throw this into a file in your config/initializers folder.

    —–8<————–
    module ActionController
    module PolymorphicRoutes
    def build_named_route_call(records, namespace, inflection, options = {})
    unless records.is_a?(Array)
    record = extract_record(records)
    route = ”
    else
    record = records.pop
    route = records.inject(”") do |string, parent|
    if parent.is_a?(Symbol) || parent.is_a?(String)
    string << “#{parent}_”
    else
    string << “#{RecordIdentifier.send!(”singular_class_name”, parent)}_”
    end
    end
    end

    if record.is_a?(Symbol) || record.is_a?(String)
    route << “#{record}_”
    else
    record = record.class unless record.is_a?(Class)
    until (record.superclass == ::ActiveRecord::Base)
    record = record.superclass
    end
    route <8————–

    Thanks for this post, it got me 90% there!

  2. Posted 10/20/2008 at 4:27 pm | Permalink

    The comment ate that code. I’ll try one more time

    module ActionController
    module PolymorphicRoutes
    def build_named_route_call(records, namespace, inflection, options = {})
    unless records.is_a?(Array)
    record = extract_record(records)
    route = ”
    else
    record = records.pop
    route = records.inject(”") do |string, parent|
    if parent.is_a?(Symbol) || parent.is_a?(String)
    string << “#{parent}_”
    else
    string << “#{RecordIdentifier.send!(”singular_class_name”, parent)}_”
    end
    end
    end

    if record.is_a?(Symbol) || record.is_a?(String)
    route << “#{record}_”
    else
    record = record.class unless record.is_a?(Class)
    until (record.superclass == ::ActiveRecord::Base)
    record = record.superclass
    end
    route << “#{RecordIdentifier.send!(”#{inflection}_class_name”, record)}_”
    end

    action_prefix(options) + namespace + route + routing_type(options).to_s
    end
    end
    end

  3. Posted 10/20/2008 at 6:17 pm | Permalink

    I need to figure out how to enable some tags for comments…

Post a Comment

Your email is never shared. Required fields are marked *

*
*