Module: RailsExtend::Routes

Extended by:
Routes
Included in:
Routes
Defined in:
lib/rails_extend/routes.rb

Instance Method Summary collapse

Instance Method Details

#actions(cached = true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rails_extend/routes.rb', line 10

def actions(cached = true)
  return @actions if cached && defined? @actions

  @actions = routes_wrapper(cached).group_by(&->(i){ i[:business] }).transform_values! do |businesses|
    businesses.group_by(&->(i){ i[:namespace] }).transform_values! do |namespaces|
      namespaces.group_by(&->(i){ i[:controller] }).transform_values! do |controllers|
        controllers.each_with_object({}) { |i, h| h.merge! i[:action] => i }
      end
    end
  end
end

#blank_routes_wrapper(cached = true) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/rails_extend/routes.rb', line 59

def blank_routes_wrapper(cached = true)
  return @blank_routes_wrapper if cached && defined?(@blank_routes_wrapper)

  @blank_routes_wrapper = Rails.application.routes.routes.map do |route|
    next unless (route.defaults[:controller].blank? || route.defaults[:action].blank?)
    detail(route)
  end.compact
end

#businesses(cached = true) ⇒ Object



38
39
40
41
42
# File 'lib/rails_extend/routes.rb', line 38

def businesses(cached = true)
  return @businesses if cached && defined? @businesses

  @businesses = routes_wrapper(cached).group_by(&->(i){ i[:business] })
end

#controller_paths(cached = true) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/rails_extend/routes.rb', line 30

def controller_paths(cached = true)
  return @controller_paths if cached && defined? @controller_paths

  @controller_paths = routes_wrapper(cached).group_by(&->(i){ i[:controller] }).transform_values! do |controllers|
    controllers[0].slice(:business, :namespace)
  end
end

#controllers(cached = true) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rails_extend/routes.rb', line 22

def controllers(cached = true)
  return @controllers if cached && defined? @controllers

  @controllers = routes_wrapper(cached).group_by(&->(i){ i[:controller] }).transform_values! do |controllers|
    controllers.each_with_object({}) { |i, h| h.merge! i[:action] => i }
  end
end

#namespaces(cached = true) ⇒ Object



44
45
46
47
48
# File 'lib/rails_extend/routes.rb', line 44

def namespaces(cached = true)
  return @namespaces if cached && defined? @namespaces

  @namespaces = routes_wrapper(cached).group_by(&->(i){ i[:namespace] })
end

#routes_wrapper(cached = true) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/rails_extend/routes.rb', line 50

def routes_wrapper(cached = true)
  return @routes_wrapper if cached && defined?(@routes_wrapper)

  @routes_wrapper = Rails.application.routes.routes.map do |route|
    next if (route.defaults[:controller].blank? || route.defaults[:action].blank?)
    detail(route)
  end.compact
end

#verbs(controller, action) ⇒ Object



6
7
8
# File 'lib/rails_extend/routes.rb', line 6

def verbs(controller, action)
  routes_wrapper.select { |i| i[:controller] == controller.to_s && i[:action] == action.to_s }.map { |i| i[:verb] }.uniq
end