Class: Monolith::RoutesController::Route

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/monolith/routes_controller.rb

Overview

Inline ActiveModel-like object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, verb:, path:, controller:, action:, constraints:, defaults:, required_parts:, id:) ⇒ Route

Returns a new instance of Route.



56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/monolith/routes_controller.rb', line 56

def initialize(name:, verb:, path:, controller:, action:, constraints:, defaults:, required_parts:, id:)
  @name = name
  @verb = verb
  @path = path
  @controller = controller
  @action = action
  @constraints = constraints
  @defaults = defaults
  @required_parts = required_parts
  @id = id
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



19
20
21
# File 'app/controllers/monolith/routes_controller.rb', line 19

def action
  @action
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



19
20
21
# File 'app/controllers/monolith/routes_controller.rb', line 19

def constraints
  @constraints
end

#controllerObject (readonly)

Returns the value of attribute controller.



19
20
21
# File 'app/controllers/monolith/routes_controller.rb', line 19

def controller
  @controller
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



19
20
21
# File 'app/controllers/monolith/routes_controller.rb', line 19

def defaults
  @defaults
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'app/controllers/monolith/routes_controller.rb', line 19

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'app/controllers/monolith/routes_controller.rb', line 19

def path
  @path
end

#required_partsObject (readonly)

Returns the value of attribute required_parts.



19
20
21
# File 'app/controllers/monolith/routes_controller.rb', line 19

def required_parts
  @required_parts
end

#verbObject (readonly)

Returns the value of attribute verb.



19
20
21
# File 'app/controllers/monolith/routes_controller.rb', line 19

def verb
  @verb
end

Class Method Details

.allObject



21
22
23
# File 'app/controllers/monolith/routes_controller.rb', line 21

def self.all
  Rails.application.routes.routes.map.with_index { |route, idx| from_route(route, idx) }.compact.sort_by(&:display_name)
end

.find(id) ⇒ Object



25
26
27
# File 'app/controllers/monolith/routes_controller.rb', line 25

def self.find(id)
  all.find { |r| r.to_param == id }
end

.from_route(route, idx) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/monolith/routes_controller.rb', line 29

def self.from_route(route, idx)
  name = route.name.to_s
  verb = route.verb.to_s
  path = route.path.spec.to_s

  # Extract controller and action
  defaults = route.defaults
  controller = defaults[:controller]
  action = defaults[:action]

  # Skip routes without controller/action or internal Rails routes
  return nil if controller.nil? || action.nil?
  return nil if controller.to_s.start_with?("rails/")

  new(
    name: name.empty? ? nil : name,
    verb: verb,
    path: path,
    controller: controller,
    action: action,
    constraints: route.constraints.except(:request_method),
    defaults: defaults.except(:controller, :action),
    required_parts: route.required_parts,
    id: idx
  )
end

Instance Method Details

#display_nameObject



72
73
74
# File 'app/controllers/monolith/routes_controller.rb', line 72

def display_name
  name || "#{controller}##{action}"
end

#full_controller_actionObject



76
77
78
# File 'app/controllers/monolith/routes_controller.rb', line 76

def full_controller_action
  "#{controller}##{action}"
end

#to_paramObject



68
69
70
# File 'app/controllers/monolith/routes_controller.rb', line 68

def to_param
  @id.to_s
end