Skip to content

Commit a99a51c

Browse files
committed
Implement rails#buffer().model_name()/controller_name()
1 parent 41bf833 commit a99a51c

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

autoload/rails.vim

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,13 @@ function! s:viewspattern()
308308
endfunction
309309

310310
function! s:controller(...)
311-
let t = RailsFileType()
312-
let f = RailsFilePath()
313-
let o = s:getopt("controller","lb")
314-
if o != ""
315-
return o
311+
return rails#buffer().controller_name(a:0 ? a:1 : 0)
312+
endfunction
313+
314+
function! s:readable_controller_name(...) dict abort
315+
let f = self.name()
316+
if has_key(self,'getvar') && self.getvar('rails_controller') != ''
317+
return self.getvar('rails_controller')
316318
elseif f =~ '\<app/views/layouts/'
317319
return s:sub(f,'.*<app/views/layouts/(.{-})\..*','\1')
318320
elseif f =~ '\<app/views/'
@@ -335,21 +337,24 @@ function! s:controller(...)
335337
return s:sub(f,'.*<components/(.{-})_controller\.rb$','\1')
336338
elseif f =~ '\<components/.*\.'.s:viewspattern().'$'
337339
return s:sub(f,'.*<components/(.{-})/\k+\.\k+$','\1')
338-
elseif f =~ '\<app/models/.*\.rb$' && t =~ '^model-mailer\>'
340+
elseif f =~ '\<app/models/.*\.rb$' && self.type_name('model-mailer')
339341
return s:sub(f,'.*<app/models/(.{-})\.rb$','\1')
340342
elseif f =~ '\<public/stylesheets/.*\.css$'
341343
return s:sub(f,'.*<public/stylesheets/(.{-})\.css$','\1')
342344
elseif a:0 && a:1
343-
return rails#pluralize(s:model())
345+
return rails#pluralize(self.model_name())
344346
endif
345347
return ""
346348
endfunction
347349

348350
function! s:model(...)
349-
let f = RailsFilePath()
350-
let o = s:getopt("model","lb")
351-
if o != ""
352-
return o
351+
return rails#buffer().model_name(a:0 ? a:1 : 0)
352+
endfunction
353+
354+
function! s:readable_model_name(...) dict abort
355+
let f = self.name()
356+
if has_key(self,'getvar') && self.getvar('rails_model') != ''
357+
return self.getvar('rails_model')
353358
elseif f =~ '\<app/models/.*_observer.rb$'
354359
return s:sub(f,'.*<app/models/(.*)_observer\.rb$','\1')
355360
elseif f =~ '\<app/models/.*\.rb$'
@@ -369,11 +374,13 @@ function! s:model(...)
369374
elseif f =~ '\<\%(test/\|spec/\)\=factories/.*\.rb$'
370375
return s:sub(f,'.*<%(test/|spec/)=factories/(.{-})%(_factory)=\.rb$','\1')
371376
elseif a:0 && a:1
372-
return rails#singularize(s:controller())
377+
return rails#singularize(self.controller_name())
373378
endif
374379
return ""
375380
endfunction
376381

382+
call s:add_methods('readable',['controller_name','model_name'])
383+
377384
function! s:readfile(path,...)
378385
let nr = bufnr('^'.a:path.'$')
379386
if nr < 0 && exists('+shellslash') && ! &shellslash
@@ -4477,8 +4484,8 @@ function! s:SetBasePath()
44774484
call filter(old_path,'!s:startswith(v:val,transformed_path)')
44784485

44794486
let path = ['app', 'app/models', 'app/controllers', 'app/helpers', 'config', 'lib', 'app/views']
4480-
if s:controller() != ''
4481-
let path += ['app/views/'.s:controller(), 'public']
4487+
if self.controller_name() != ''
4488+
let path += ['app/views/'.self.controller_name(), 'public']
44824489
endif
44834490
if self.app().has('test')
44844491
let path += ['test', 'test/unit', 'test/functional', 'test/integration']

0 commit comments

Comments
 (0)