svenfuchs / routing-filter

routing-filter wraps around the complex beast that the Rails routing system is, allowing for unseen flexibility and power in Rails URL recognition and generation.
http://www.artweb-design.de
MIT License
464 stars 84 forks source link

I'm not able to make a custom filter work #63

Open bgvo opened 8 years ago

bgvo commented 8 years ago

Hi there.

I'm trying to build a custom filter for our dynamic (and multilingual) URLs. For the moment I'm just playing around to see what can I do and then design them all.

Now, one of our URLs is '/courses/search_results?course_type=5'

course_type 5 corresponds to 'Course'. I'm trying to convert the above URL to something like '/courses/search_results/Course' but I cannot make it work. This is what I've tried:

module RoutingFilter
  class Theme < Filter
    THEME_SEGMENT = %r((Course)?$)

    def around_recognize(path, env, &block)
      course_type = extract_segment!(THEME_SEGMENT, path)
      yield.tap do |params|
        params[:course_type] = CourseType.find_by_name(course_type) if course_type
      end
    end

    def around_generate(params, &block)
      course_type_id = params.delete(:course_type)
      course_type_name = CourseType.find(course_type_id).name if course_type_id
      yield.tap do |result|
        append_segment!(result, "#{course_type_name}") if course_type_name
      end
    end

  end
end

I tried to debug from extract_segment! definition:

      def extract_segment!(pattern, path)
        path.sub!(pattern) { $2 || '' }
        path.replace('/') if path.empty?
        $1
      end

This is how pattern and path (after calling sub!) look like:

pattern: /\/courses\/search_results\/(Course)/ path: "/courses/search_results/"

As I understand, that URL ("/courses/search_results/") should be recognized, but instead I got this error:

image

Thanks!