Sha256: 114dac37029351caa97011351d31927712d7549068fc6f0da24e0259682449ea

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module CanCanDry
  # Copiado de https://github.com/appirits/awesome_admin_layout
  # /lib/awesome_admin_layout/recognize_path.rb
  module PathRecognizer
    class << self
      def recognize(root_path, path, options = {})
        path = remove_root_path(root_path, path)
        Rails.application.routes.recognize_path(path, options)
      rescue ActionController::RoutingError
        Rails::Engine.subclasses.each do |engine|
          recognized_path = engine_recognize(engine, path, options)
          return recognized_path if recognized_path
        end
        raise "Path not recognized: \"#{path}\" (Options: #{options})"
      end

      private

      def remove_root_path(root_path, path)
        path = path.gsub(/\A#{Regexp.quote(root_path)}/, '')
        path.gsub(%r{\A/*}, '/')
      end

      def engine_recognize(engine, path, options)
        engine_path = path_for_engine(engine.instance.class, path)
        return unless engine_path

        begin
          return engine.instance.routes.recognize_path(engine_path, options)
        rescue ActionController::RoutingError => e
          Rails.logger.debug "[#{engine}] ActionController::RoutingError: #{e.message}"
        end
        nil
      end

      def path_for_engine(engine_class, path)
        engine_route = Rails.application.routes.routes.find { |r| app_class_for(r) == engine_class }
        return unless engine_route

        path.gsub(/^#{engine_route.path.spec}/, '')
      end

      def app_class_for(route)
        if Rails.version =~ /\A4\.2\./
          # for Rails 4.2
          route.app.app
        else
          # for Rails 4.1, 4.0, 3.2
          route.app
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
can_can_dry-0.5.0 lib/can_can_dry/path_recognizer.rb
can_can_dry-0.4.1 lib/can_can_dry/path_recognizer.rb
can_can_dry-0.4.0 lib/can_can_dry/path_recognizer.rb