确定 Journey::Path::Pattern 是否匹配当前页面 [英] Determine if Journey::Path::Pattern matches current page

查看:33
本文介绍了确定 Journey::Path::Pattern 是否匹配当前页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用这篇文章中概述的方法url_for 一起确定当前路径是否在已安装的引擎中,但我很难弄清楚如何使用 Journey::Path::Pattern(这是另一篇文章中概述的 mounted_pa​​th 方法返回的内容).

I'm trying to use the method outlined this post in conjunction with url_for to determine if the current path is in a mounted engine, but I'm having a hard time figuring out how exactly to use Journey::Path::Pattern (which is what is returned by the mounted_path method outlined in the other post).

class Rails::Engine
  def self.mounted_path
    route = Rails.application.routes.routes.detect do |route|
      route.app == self
    end
    route && route.path
  end
end

除了 官方文档,这不是特别有用.我确定解决方案相对简单,我正在尝试编写的辅助方法的要点如下:

There doesn't seem to be too much discussion on it anywhere, aside from the official documentation, which wasn't particularly helpful. I'm sure the solution is relatively simple, and the gist of the helper method I'm trying to write is below:

def in_engine? engine
  current_url.include?(engine.mounted_path)
end

<小时>

我的一些引擎作为子域挂载,有些引擎挂载在应用程序本身中,这使我无法简单地检查当前子域是否与挂载路径相同,或使用 path_for.>

Some of my engines are mounted as subdomains and some are mounted within the app itself, preventing me from simply checking if the current subdomain is the same as the mounted path, or using path_for.

推荐答案

不完全是解决方案,但可能是有用的线索.

Not exactly a solution, but maybe a useful lead.

我发现你的问题很有趣,所以我开始深入研究 rails 源代码......多么可怕但有启发性的旅行 :D

I found your question interesting, so I started delving deep inside rails source... what a scary, yet instructive trip :D

原来 Rails 的路由器有一个 recognize method 接受 request 作为参数,并生成与请求匹配的路由.

Turns out that Rails' router has a recognize method that accepts a request as argument, and yields the routes that match the request.

由于路由有一个 app 方法,您可以将其与引擎进行比较,并且您可以访问 request 对象(它考虑了 http 方法,子域等),如果您了解如何直接访问路由器实例,您应该能够按照以下方式做一些事情:

As the routes have an app method you can compare to your engine, and as you can have access to the request object (which takes into account the http method, subdomain, etc), if you find out how to have direct access to the router instance, you should be able to do something along the lines of :

  def in_engine?(engine)
    router.recognize(request) do |route,*| 
      return true if route.app == engine
    end
    false
  end

编辑

我想我发现了,但是我手头没有 Rails 应用程序来测试这个:(

I think i found out, but it's late here in I have no rails app at hand to test this :(

  def in_engine?(engine)
    # get all engine routes. 
    # (maybe possible to do this directly with the engine, dunno)
    engine_routes = Rails.application.routes.set.select do |route| 
      route.app == engine
    end
    !!engine_routes.detect{ |route| route.matches?(request) }
  end

编辑

此外,也许更简单的解决方法是这样做:

also, maybe a simpler workaround would be to do this :

在您的主应用中

  class ApplicationController < ActionController::Base
    def in_engine?(engine)
      false
    end
    helper_method :in_engine?
  end

然后在您引擎的应用程序控制器中

  def in_engine?(engine)
    engine == ::MyEngine
  end
  helper_method :in_engine?

这篇关于确定 Journey::Path::Pattern 是否匹配当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆