Skip to content

Commit 04b1101

Browse files
committed
Support unnamed routes
1 parent ccd7daf commit 04b1101

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

autoload/rails.vim

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,8 +1534,7 @@ function! s:readable_preview_urls(lnum) dict abort
15341534
let handler = self.controller_name().'#'.fnamemodify(self.name(),':t:r:r')
15351535
endif
15361536
if exists('handler')
1537-
call self.app().route_names()
1538-
for route in values(self.app().cache.get('named_routes'))
1537+
for route in self.app().routes()
15391538
if route.method =~# 'GET' && route.handler ==# handler
15401539
let urls += [s:gsub(s:gsub(route.path, '\([^()]*\)', ''), ':\w+', '1')]
15411540

@@ -2344,39 +2343,48 @@ function! rails#cfile(...) abort
23442343
return empty(cfile) && a:0 && a:1 is# 'delegate' ? "\<C-R>\<C-F>" : cfile
23452344
endfunction
23462345

2347-
function! s:app_named_route_file(route) dict abort
2348-
call self.route_names()
2349-
if self.cache.has("named_routes") && has_key(self.cache.get("named_routes"),a:route)
2350-
return s:sub(self.cache.get("named_routes")[a:route].handler, '#', '_controller.rb#')
2351-
endif
2346+
function! s:app_named_route_file(route_name) dict abort
2347+
for route in self.routes()
2348+
if get(route, 'name', '') ==# a:route_name
2349+
return s:sub(route.handler, '#', '_controller.rb#')
2350+
endif
2351+
endfor
23522352
return ""
23532353
endfunction
23542354

2355-
function! s:app_route_names() dict abort
2356-
if self.cache.needs("named_routes")
2355+
function! s:app_routes() dict abort
2356+
if self.cache.needs('routes')
23572357
let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
23582358
let cwd = getcwd()
2359-
let routes = {}
2359+
let routes = []
2360+
let paths = {}
23602361
try
23612362
execute cd fnameescape(rails#app().path())
23622363
let output = system(self.rake_command().' routes')
23632364
finally
23642365
execute cd fnameescape(cwd)
23652366
endtry
23662367
for line in split(output, "\n")
2367-
let matches = matchlist(line, '^ *\(\l\w*\) \{-\}\([A-Z|]*\) \+\(\S\+\) \+\([[:alnum:]_/]\+#\w\+\)')
2368+
let matches = matchlist(line, '^ *\(\l\w*\|\) \{-\}\([A-Z|]*\) \+\(\S\+\) \+\([[:alnum:]_/]\+#\w\+\)\%( {.*\)\=$')
23682369
if !empty(matches)
23692370
let [_, name, method, path, handler; __] = matches
2370-
let routes[name] = {'method': method, 'path': path, 'handler': handler}
2371+
if !empty(name)
2372+
let paths[path] = name
2373+
else
2374+
let name = get(paths, path, '')
2375+
endif
2376+
call insert(routes, {'method': method, 'path': path, 'handler': handler, 'name': name})
2377+
else
2378+
PP line
23712379
endif
23722380
endfor
2373-
call self.cache.set("named_routes",routes)
2381+
call self.cache.set('routes', routes)
23742382
endif
23752383

2376-
return keys(self.cache.get("named_routes"))
2384+
return self.cache.get('routes')
23772385
endfunction
23782386

2379-
call s:add_methods('app', ['route_names','named_route_file'])
2387+
call s:add_methods('app', ['routes', 'named_route_file'])
23802388

23812389
function! s:RailsIncludefind(str,...) abort
23822390
if a:str ==# "ApplicationController" && rails#app().has_path('app/controllers/application.rb')
@@ -5120,7 +5128,7 @@ augroup railsPluginAuto
51205128
autocmd BufWritePost */config/database.yml call rails#cache_clear("db_config")
51215129
autocmd BufWritePost */config/projections.json call rails#cache_clear("projections")
51225130
autocmd BufWritePost */test/test_helper.rb call rails#cache_clear("user_assertions")
5123-
autocmd BufWritePost */config/routes.rb call rails#cache_clear("named_routes")
5131+
autocmd BufWritePost */config/routes.rb call rails#cache_clear("routes")
51245132
autocmd BufWritePost */config/application.rb call rails#cache_clear("default_locale")
51255133
autocmd BufWritePost */config/application.rb call rails#cache_clear("stylesheet_suffix")
51265134
autocmd BufWritePost */config/environments/*.rb call rails#cache_clear("environments")

0 commit comments

Comments
 (0)