Class: AwesomeAnnotate::Route

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/awesome_annotate/route.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Route

Returns a new instance of Route.



8
9
10
11
12
# File 'lib/awesome_annotate/route.rb', line 8

def initialize(params = {})
  super()
  @env_file_path = Pathname.new(params[:env_file_path] || 'config/environment.rb')
  @route_file_path = Pathname.new(params[:route_file_path] || 'config/routes.rb')
end

Instance Method Details

#annotateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/awesome_annotate/route.rb', line 15

def annotate
  raise "Rails application path is required" unless @env_file_path.exist?

  apply @env_file_path.to_s

  inspector = ActionDispatch::Routing::RoutesInspector.new(Rails.application.routes.routes)
  formatter = ActionDispatch::Routing::ConsoleFormatter::Sheet.new

  routes = inspector.format(formatter, {})
  route_message = parse_routes(routes)

  raise "Route file not found" unless @route_file_path.exist?

  insert_file_before_class(@route_file_path, route_message)

  say "annotate routes in #{@route_file_path}"
end