Module: AwesomeAdminLayout::RecognizePath

Included in:
AwesomeAdminLayout
Defined in:
lib/awesome_admin_layout/recognize_path.rb

Instance Method Summary collapse

Instance Method Details

#active_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
# File 'lib/awesome_admin_layout/recognize_path.rb', line 5

def active_url?(url)
  return false unless AwesomeAdminLayout.request
  same_url? AwesomeAdminLayout.request.fullpath, url
end

#recognize_path(path, options = {}) ⇒ Object

TODO: Add tests



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/awesome_admin_layout/recognize_path.rb', line 11

def recognize_path(path, options = {})
  recognized_path = Rails.application.routes.recognize_path(path, options)
  # We have a route that catches everything and sends it to 'errors#not_found', you might
  # need to rescue ActionController::RoutingError
rescue ActionController::RoutingError
  # The main app didn't recognize the path, try the engines...
  Rails::Engine.subclasses.each do |engine|
    engine_instance = engine.instance
    # Find the route to the engine, e.g. '/blog' -> Blog::Engine (a.k.a. "mount")
    engine_class = engine_instance.class
    engine_route = Rails.application.routes.routes.find { |r| app_class_for(r) == engine_class }
    next unless engine_route

    # The engine won't recognize the "mount", so strip it off the path,
    # e.g. '/blog/posts/new'.gsub(%r(^/blog), '') #=> '/posts/new', which will be recognized by the engine
    path_for_engine = path.gsub(/^#{engine_route.path.spec}/, '')

    begin
      recognized_path = engine_instance.routes.recognize_path(path_for_engine, options)
      break
    rescue ActionController::RoutingError => e
      Rails.logger.debug "[#{engine}] ActionController::RoutingError: #{e.message}"
    end
  end

  recognized_path
end