Class: Respect::Rails::RouteInfo

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/respect/rails/route_info.rb

Overview

A wrapper around ActionDispatch::Routing::Route. It provides only access to the information we need when generating doc. It is very strongly inspired from ActionDispatch::Routing::RouteWrapper class which is internal and designed to be used with ActionDispoatch::Routing::RouteInspector. These classes are not meant to be used by external user as far as I can read in rails code base. So, we made up our own so that we can easily adjust it to our need.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RouteInfo) initialize(route, mount_point)

A new instance of RouteInfo



13
14
15
16
17
18
19
# File 'lib/respect/rails/route_info.rb', line 13

def initialize(route, mount_point)
  @route = route
  if !mount_point.is_a?(self.class) && !mount_point.nil? && !mount_point.engine?
    raise "'#{mount_point.inspect}' must be a route to an engine if set"
  end
  @mount_point = mount_point
end

Instance Attribute Details

- (Object) mount_point (readonly)

Returns the value of attribute mount_point



21
22
23
# File 'lib/respect/rails/route_info.rb', line 21

def mount_point
  @mount_point
end

- (Object) route (readonly)

Returns the value of attribute route



21
22
23
# File 'lib/respect/rails/route_info.rb', line 21

def route
  @route
end

Instance Method Details

- (Object) <=>(other)



39
40
41
# File 'lib/respect/rails/route_info.rb', line 39

def <=>(other)
  self.path <=> other.path
end

- (Object) action_name



51
52
53
# File 'lib/respect/rails/route_info.rb', line 51

def action_name
  @route.requirements[:action] || ':action'
end

- (Object) anchor



95
96
97
# File 'lib/respect/rails/route_info.rb', line 95

def anchor
  "#{controller_name}_#{action_name}"
end

- (Object) controller_name



47
48
49
# File 'lib/respect/rails/route_info.rb', line 47

def controller_name
  @route.requirements[:controller] || ':controller'
end

- (Object) endpoint



76
77
78
# File 'lib/respect/rails/route_info.rb', line 76

def endpoint
  rack_app ? rack_app : nil
end

- (Boolean) engine?

Returns:

  • (Boolean)


91
92
93
# File 'lib/respect/rails/route_info.rb', line 91

def engine?
  rack_app && rack_app.respond_to?(:routes)
end

- (Boolean) has_schema?

Returns:

  • (Boolean)


59
60
61
# File 'lib/respect/rails/route_info.rb', line 59

def has_schema?
  schema && schema.has_schema?
end

- (Boolean) internal?

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/respect/rails/route_info.rb', line 34

def internal?
  path =~ %r{\A#{::Rails.application.config.assets.prefix}} \
  || controller_name =~ %r{\Arails/(info|welcome)}
end

- (Object) path



23
24
25
26
27
28
29
30
31
32
# File 'lib/respect/rails/route_info.rb', line 23

def path
  path = @route.path.spec.to_s
  if @mount_point
    path = @mount_point.path + path
  end
  if path.length > 1 && path =~ %r{/$}
    path.chop!
  end
  path
end

- (Object) rack_app(app = @route.app)



80
81
82
83
84
85
86
87
88
89
# File 'lib/respect/rails/route_info.rb', line 80

def rack_app(app = @route.app)
  @rack_app ||= begin
    class_name = app.class.name.to_s
    if class_name == "ActionDispatch::Routing::Mapper::Constraints"
      rack_app(app.app)
    elsif ActionDispatch::Routing::Redirect === app || class_name !~ /^ActionDispatch::Routing/
      app
    end
  end
end

- (Object) schema



55
56
57
# File 'lib/respect/rails/route_info.rb', line 55

def schema
  @schema = ActionSchema.from_controller(controller_name, action_name)
end

- (Object) spec



63
64
65
# File 'lib/respect/rails/route_info.rb', line 63

def spec
  "#{verb} #{path}".strip
end

- (Object) url



67
68
69
70
71
72
73
74
# File 'lib/respect/rails/route_info.rb', line 67

def url
  if schema
    options = schema_set.default_url_options
  else
    options = @route.defaults.merge({ format: 'json' })
  end
  ::Rails.application.routes.url_for(options)
end

- (Object) verb



43
44
45
# File 'lib/respect/rails/route_info.rb', line 43

def verb
  @route.verb.source.gsub(/[$^]/, '')
end