Class: AwesomeAnnotate::Route

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

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Route

Returns a new instance of Route.



14
15
16
17
18
# File 'lib/awesome_annotate/route.rb', line 14

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/awesome_annotate/route.rb', line 21

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

  load_rails_environment

  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

#removeObject



40
41
42
43
44
45
46
47
48
# File 'lib/awesome_annotate/route.rb', line 40

def remove
  raise 'Route file not found' unless @route_file_path.exist?

  if remove_annotation(file_path: @route_file_path, marker: 'routes')
    say "remove route annotation in #{@route_file_path}"
  else
    say "no route annotation in #{@route_file_path}"
  end
end