Class: RailsInfo::RoutesPresenter

Inherits:
Presenter
  • Object
show all
Defined in:
app/presenters/rails_info/routes_presenter.rb

Instance Method Summary collapse

Methods inherited from Presenter

#initialize, #subject=

Constructor Details

This class inherits a constructor from RailsInfo::Presenter

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsInfo::Presenter

Instance Method Details

#accordionObject



2
3
4
5
6
7
8
9
10
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
38
39
40
# File 'app/presenters/rails_info/routes_presenter.rb', line 2

def accordion
  routes = request.env['action_dispatch.routes'].routes.map do |route|
    {
      source: (route.verb.respond_to?(:source) ? route.verb.source : route.verb).gsub(/[$^]/, ''),
      spec: route.path.respond_to?(:spec) ? route.path.spec.to_s : route.path,
      name: route.name,
      requirements: route.requirements.inspect
    }
  end
  
  namespaced_routes = {}
  
  routes.each do |route|
    namespace = '/'
    
    unless route[:spec] == namespace
      spec = route[:spec].split('/')
      spec.shift
      namespace = spec.shift
    end
    
    namespaced_routes[namespace] ||= []
    namespaced_routes[namespace] << route 
  end
  
   :div, class: 'accordions' do
    html = ''
    
    namespaced_routes.each do |namespace, routes|
      html += (
        :h3, raw("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{namespace}")
      )
      table = render partial: 'rails_info/routes/table', locals: { routes: routes }
      html +=  :div, raw(table), style: "max-height:300px; overflow: auto"
    end  
    
    raw html
  end  
end