Class: HelpfulComments::ControllerRoutes

Inherits:
Base
  • Object
show all
Defined in:
lib/helpful_comments/controller_routes.rb

Instance Method Summary collapse

Methods inherited from Base

load

Constructor Details

#initialize(klass) ⇒ ControllerRoutes

takes a controller



5
6
7
# File 'lib/helpful_comments/controller_routes.rb', line 5

def initialize(klass)
  @klass = klass
end

Instance Method Details

#buildObject

builds the lines to be put into the file



10
11
12
13
14
15
16
17
18
19
# File 'lib/helpful_comments/controller_routes.rb', line 10

def build
  controller_name = @klass.name.gsub(/Controller$/, '').underscore
  Rails.application.routes.routes.each_with_object({}) do |route, comments|
    if route.defaults[:controller] == controller_name
      verb_match = route.verb.to_s.match(/\^(.*)\$/)
      verbs = verb_match.nil? ? '*' : verb_match[1]
      (comments[route.defaults[:action]] ||= []) << "#{verbs} #{route.ast}"
    end
  end
end

#loadObject

puts the comments into the file



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/helpful_comments/controller_routes.rb', line 22

def load
  @comments = self.build
  max_size = @comments.values.max_by(&:size).try(:size)
  
  load_comments(Rails.root.join('app', 'controllers').to_s, max_size) do |file, line|
    if get_finder === line
      found_method = $1
      
      comments = build_comments(found_method, line.chomp.match(/^(\s*)[^\s]*/)[1])
      file.write(comments.join) unless file.trailing?(comments)
      
      @comments.delete(found_method)
      get_finder(true)
    end
    
    file.write(line)
  end
end