Sha256: 6ae250766bc355aa9ad3a2230b2f02a2c494a3e165d5e2a28fec2cf48cd6e80c

Contents?: true

Size: 1.79 KB

Versions: 13

Compression:

Stored size: 1.79 KB

Contents

require "#{File.dirname(__FILE__)}/abstract_note"

module Footnotes
  module Notes
    class RoutesNote < AbstractNote
      def initialize(controller)
        @controller = controller
        @parsed_routes = parse_routes
      end

      def legend
        "Routes for #{@controller.class.to_s}"
      end

      def content
        mount_table(@parsed_routes.unshift([:path, :name, :options, :requirements]))
      end

      protected
        def parse_routes
          routes_with_name = ActionController::Routing::Routes.named_routes.to_a.flatten

          return ActionController::Routing::Routes.filtered_routes(:controller => @controller.controller_name).collect do |route|
            # Catch routes name if exists
            i = routes_with_name.index(route)
            name = i ? routes_with_name[i-1].to_s : ''

            # Catch segments requirements
            req = {}
            route.segments.each do |segment|
              next unless segment.is_a?(ActionController::Routing::DynamicSegment) && segment.regexp
              req[segment.key.to_sym] = segment.regexp
            end

            [escape(name), route.segments.join, route.requirements.reject{|key,value| key == :controller}.inspect, req.inspect]
          end
        end
    end
  end

  module Extensions
    module Routes
      # Filter routes according to the filter sent
      #
      def filtered_routes(filter = {})
        return [] unless filter.is_a?(Hash)
        return routes.reject do |r| 
          filter_diff = filter.diff(r.requirements)
          route_diff  = r.requirements.diff(filter)
          (filter_diff == filter) || (filter_diff != route_diff)
        end
      end
    end
  end
end

if Footnotes::Notes::RoutesNote.included?
  ActionController::Routing::RouteSet.send :include, Footnotes::Extensions::Routes
end

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
ceritium-rails-footnotes-3.4.1.1 lib/rails-footnotes/notes/routes_note.rb
ceritium-rails-footnotes-3.4.1 lib/rails-footnotes/notes/routes_note.rb
ceritium-rails-footnotes-3.4 lib/rails-footnotes/notes/routes_note.rb
josevalim-rails-footnotes-3.3.1 lib/notes/routes_note.rb
josevalim-rails-footnotes-3.3.2 lib/notes/routes_note.rb
josevalim-rails-footnotes-3.4.1 lib/rails-footnotes/notes/routes_note.rb
josevalim-rails-footnotes-3.4 lib/rails-footnotes/notes/routes_note.rb
josevalim-rails-footnotes-3.5.0 lib/rails-footnotes/notes/routes_note.rb
lexrupy-rails-footnotes-3.3.2 lib/rails-footnotes/notes/routes_note.rb
lexrupy-rails-footnotes-3.4.1 lib/rails-footnotes/notes/routes_note.rb
lexrupy-rails-footnotes-3.4 lib/rails-footnotes/notes/routes_note.rb
scharfie-rails-footnotes-3.5.0 lib/rails-footnotes/notes/routes_note.rb
scharfie-rails-footnotes-3.4.2 lib/rails-footnotes/notes/routes_note.rb