class FlattenRoutes::Util

Constants

MAGIC_COMMENT_END_CONTENT
MAGIC_COMMENT_QUOTED_PATTERN
MAGIC_COMMENT_START_CONTENT
ROUTES_CONTENT_PATTERN
SEPARATOR

Public Class Methods

add_magic_comment(content, time = Time.now) click to toggle source
# File lib/flatten_routes.rb, line 66
def add_magic_comment(content, time = Time.now)
  "#{MAGIC_COMMENT_START_CONTENT} #{time}\n#{content}\n#{MAGIC_COMMENT_END_CONTENT}\n"
end
commentize(contents) click to toggle source
# File lib/flatten_routes.rb, line 74
def commentize(contents)
  return contents if contents.size < 1

  contents.map { |content| "##{content}" }
end
delete_flatten_routes_definition(content) click to toggle source
# File lib/flatten_routes.rb, line 70
def delete_flatten_routes_definition(content)
  content.gsub(MAGIC_COMMENT_QUOTED_PATTERN, '')
end
format_routes(routes) click to toggle source
# File lib/flatten_routes.rb, line 24
def format_routes(routes)
  routes.map! do |route|
    if route[:reqs] =~ %r{redirect}
      reqs = route[:reqs].split(',').each(&:strip)
      result = reqs[0] << ', ' << reqs[1].gsub(%r{[\w\/:\.]+}, '\'\&\'')
    elsif route[:reqs] =~ %r{(=>\/.*?\/)}
      reqs = route[:reqs].split(' ')
      result = reqs[0].gsub(%r{[\w\/#:]+}, '\'\&\'')
      result << ', ' << reqs[1..-1].join('').gsub(%r{(^{|}$)}, '')
    else
      reqs = route[:reqs].split(' ').each(&:strip)
      result = reqs[0].gsub(%r{[\w\/#:]+}, '\'\&\'')
    end

    # format: false
    unless %r{\(\.:format\)}.match(route[:path])
      result << ', :format => false'
    end

    {
      verb: route[:verb].downcase,
      path: route[:path].gsub(%r{\(\.:format\)},'').gsub(%r{[\w\/:\.\(\)_\-\*']+}, '\'\&\''),
      reqs: result
    }
  end
  verb_length, path_length = %i(verb path).map do |key|
    routes.map { |route| route[key].length }.max || 0
  end
  line = []
  new_routes = []
  routes.each do |routes|
    line.clear
    line << routes[:verb].ljust(verb_length)
    line << routes[:path].ljust(path_length)
    line << SEPARATOR
    line << routes[:reqs]
    new_routes << ('  ' << line.join(' '))
  end

  new_routes
end
routes_from_rails_application() click to toggle source
# File lib/flatten_routes.rb, line 16
def routes_from_rails_application
  all_routes = Rails.application.routes.routes
  inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
  inspector.send(:collect_routes, all_routes)
end