Sha256: 24e315b964ce9d7b50fb2d96f507ccd4891f65606e04f3d8067c2a714ed2d36c

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Docushin
  class RouteSet
    attr_accessor :routes

    def initialize
      @routes = []
      Rails.application.routes.routes.each do |route|
        # next if route.app.is_a?(ActionDispatch::Routing::Mapper::Constraints)
        next if route.app.is_a?(Sprockets::Environment)
        next if route.app == Docushin::Engine

        if (rack_app = discover_rack_app(route.app)) && rack_app.respond_to?(:routes)
          rack_app.routes.routes.each do |rack_route|
            add_route Route.new(rack_route)
          end if rack_app.routes.respond_to?(:routes)
        end

        add_route Route.new(route)
      end
    end

    def add_route(route)
      if Docushin.path_regexp.nil?
        @routes << route
      else
        @routes << route if route.path.match(Docushin.path_regexp)
      end
    end

    def discover_rack_app(app)
      class_name = app.class.name.to_s
      if class_name == "ActionDispatch::Routing::Mapper::Constraints"
        discover_rack_app(app.app)
      elsif class_name !~ /^ActionDispatch::Routing/
        app
      end
    end

    def find(hash)
      @routes.each do |route|
        return route if route.id == hash
      end
      nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
docushin-0.0.2 lib/docushin/route_set.rb