Class: FlattenRoutes::Util
- Inherits:
-
Object
- Object
- FlattenRoutes::Util
- Defined in:
- lib/flatten_routes.rb
Constant Summary collapse
- ROUTES_CONTENT_PATTERN =
%r{(routes.draw do\r?\n)(.*?)(\r?\nend)}m
- MAGIC_COMMENT_START_CONTENT =
'# == generated by flatten_routes from here'.freeze
- MAGIC_COMMENT_END_CONTENT =
'# == generated by flatten_routes to here'.freeze
- MAGIC_COMMENT_QUOTED_PATTERN =
%r{(# == generated by flatten_routes from here)(.*?)(# == generated by flatten_routes to here\r?\n)}m
- SEPARATOR =
'=>'.freeze
Class Method Summary collapse
- .add_magic_comment(content, time = Time.now) ⇒ Object
- .commentize(contents) ⇒ Object
- .delete_flatten_routes_definition(content) ⇒ Object
- .format_routes(routes) ⇒ Object
- .routes_from_rails_application ⇒ Object
Class Method Details
.add_magic_comment(content, time = Time.now) ⇒ Object
66 67 68 |
# 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) ⇒ Object
74 75 76 77 78 |
# 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) ⇒ Object
70 71 72 |
# File 'lib/flatten_routes.rb', line 70 def delete_flatten_routes_definition(content) content.gsub(MAGIC_COMMENT_QUOTED_PATTERN, '') end |
.format_routes(routes) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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 ⇒ Object
16 17 18 19 20 |
# 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 |