Skip to content

Commit b038bcc

Browse files
committed
rails#app().has(feature) to check for a feature
Currently works for 'test', 'spec', 'cucumber', and 'sass'.
1 parent 48b1d50 commit b038bcc

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

autoload/rails.vim

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -598,22 +598,29 @@ function! s:app_environments() dict
598598
return copy(self.cache.get('environments'))
599599
endfunction
600600

601-
" Check for Test::Unit, RSpec, and Cucumber by calling this method with an
602-
" argument of 'test', 'spec', or 'features'. Given no arguments, returns a
603-
" list.
604-
function! s:app_test_suites(...) dict
605-
if self.cache.needs('test_suites')
606-
let suites = filter(['test','spec','features'],'isdirectory(self.path(v:val))')
607-
call self.cache.set('test_suites',suites)
601+
function! s:app_has(feature) dict
602+
let map = {
603+
\'test': 'test/',
604+
\'spec': 'spec/',
605+
\'cucumber': 'features/',
606+
\'sass': 'public/stylesheets/sass/'}
607+
if self.cache.needs('features')
608+
call self.cache.set('features',{})
608609
endif
609-
if a:0
610-
return index(self.cache.get('test_suites'),a:1) + 1
611-
else
612-
return copy(self.cache.get('test_suites'))
610+
let features = self.cache.get('features')
611+
if !has_key(features,a:feature)
612+
let path = get(map,a:feature,a:feature.'/')
613+
let features[a:feature] = isdirectory(rails#app().path(path))
613614
endif
615+
return features[a:feature]
616+
endfunction
617+
618+
" Returns the subset of ['test', 'spec', 'cucumber'] present on the app.
619+
function! s:app_test_suites() dict
620+
return filter(['test','spec','cucumber'],'self.has(v:val)')
614621
endfunction
615622

616-
call s:add_methods('app',['calculate_file_type','environments','test_suites'])
623+
call s:add_methods('app',['calculate_file_type','environments','has','test_suites'])
617624

618625
" }}}1
619626
" Ruby Execution {{{1
@@ -1121,7 +1128,7 @@ function! s:default_rake_task(lnum)
11211128
else
11221129
return 'db:migrate'
11231130
endif
1124-
elseif self.test_suites('spec') && RailsFilePath() =~# '^app/.*\.rb' && self.has_file(s:sub(RailsFilePath(),'^app/(.*)\.rb$','spec/\1_spec.rb'))
1131+
elseif self.has('spec') && RailsFilePath() =~# '^app/.*\.rb' && self.has_file(s:sub(RailsFilePath(),'^app/(.*)\.rb$','spec/\1_spec.rb'))
11251132
return 'spec SPEC="%:p:r:s?[\/]app[\/]?/spec/?_spec.rb" SPEC_OPTS='
11261133
elseif t=~ '^model\>'
11271134
return 'test:units TEST="%:p:r:s?[\/]app[\/]models[\/]?/test/unit/?_test.rb"'
@@ -1408,7 +1415,7 @@ function! s:app_generate_command(bang,...) dict
14081415
endif
14091416
if !self.execute_ruby_command("script/generate ".target.str) && file != ""
14101417
call self.cache.clear('user_classes')
1411-
call self.cache.clear('test_suites')
1418+
call self.cache.clear('features')
14121419
if file =~ '^db/migrate/\d\d\d\d'
14131420
let file = get(self.relglob('',s:sub(file,'\d+','[0-9]*[0-9]')),-1,file)
14141421
endif
@@ -1902,14 +1909,14 @@ function! s:BufFinderCommands()
19021909
call s:addfilecmds("api")
19031910
call s:addfilecmds("layout")
19041911
call s:addfilecmds("fixtures")
1905-
if rails#app().test_suites('test') || rails#app().test_suites('spec')
1912+
if rails#app().has('test') || rails#app().has('spec')
19061913
call s:addfilecmds("unittest")
19071914
call s:addfilecmds("functionaltest")
19081915
endif
1909-
if rails#app().test_suites('test') || rails#app().test_suites('features')
1916+
if rails#app().has('test') || rails#app().has('cucumber')
19101917
call s:addfilecmds("integrationtest")
19111918
endif
1912-
if rails#app().test_suites('spec')
1919+
if rails#app().has('spec')
19131920
call s:addfilecmds("spec")
19141921
endif
19151922
call s:addfilecmds("stylesheet")
@@ -2040,32 +2047,32 @@ endfunction
20402047

20412048
function! s:unittestList(A,L,P)
20422049
let found = []
2043-
if rails#app().test_suites('test')
2050+
if rails#app().has('test')
20442051
let found += rails#app().relglob("test/unit/","**/*","_test.rb")
20452052
endif
2046-
if rails#app().test_suites('spec')
2053+
if rails#app().has('spec')
20472054
let found += rails#app().relglob("spec/models/","**/*","_spec.rb")
20482055
endif
20492056
return s:autocamelize(found,a:A)
20502057
endfunction
20512058

20522059
function! s:functionaltestList(A,L,P)
20532060
let found = []
2054-
if rails#app().test_suites('test')
2061+
if rails#app().has('test')
20552062
let found += rails#app().relglob("test/functional/","**/*","_test.rb")
20562063
endif
2057-
if rails#app().test_suites('spec')
2064+
if rails#app().has('spec')
20582065
let found += rails#app().relglob("spec/controllers/","**/*","_spec.rb")
20592066
endif
20602067
return s:autocamelize(found,a:A)
20612068
endfunction
20622069

20632070
function! s:integrationtestList(A,L,P)
20642071
let found = []
2065-
if rails#app().test_suites('test')
2072+
if rails#app().has('test')
20662073
let found += s:autocamelize(rails#app().relglob("test/integration/","**/*","_test.rb"),a:A)
20672074
endif
2068-
if rails#app().test_suites('features')
2075+
if rails#app().has('cucumber')
20692076
let found += s:completion_filter(rails#app().relglob("features/","**/*",".feature"),a:A)
20702077
endif
20712078
return found
@@ -2454,14 +2461,14 @@ endfunction
24542461

24552462
function! s:integrationtestEdit(bang,cmd,...)
24562463
if !a:0
2457-
if rails#app().test_suites('features')
2464+
if rails#app().has('cucumber')
24582465
return s:EditSimpleRb(a:bang,a:cmd,"integrationtest","support/env","features/",".rb")
24592466
else
24602467
return s:EditSimpleRb(a:bang,a:cmd,"integrationtest","test_helper","test/",".rb")
24612468
endif
24622469
endif
24632470
let cmd = s:findcmdfor(a:cmd.(a:bang?'!':''))
2464-
let mapping = {'test': ['test/integration/','_test.rb'], 'features': ['features/','.feature']}
2471+
let mapping = {'test': ['test/integration/','_test.rb'], 'cucumber': ['features/','.feature']}
24652472
let tests = map(filter(rails#app().test_suites(),'has_key(mapping,v:val)'),'get(mapping,v:val)')
24662473
if empty(tests)
24672474
let tests = [mapping['test']]
@@ -4337,10 +4344,10 @@ function! s:SetBasePath()
43374344
if s:controller() != ''
43384345
let path += ['app/views/'.s:controller(), 'public']
43394346
endif
4340-
if rails#app().test_suites('test')
4347+
if rails#app().has('test')
43414348
let path += ['test', 'test/unit', 'test/functional', 'test/integration']
43424349
endif
4343-
if rails#app().test_suites('spec')
4350+
if rails#app().has('spec')
43444351
let path += ['spec', 'spec/models', 'spec/controllers', 'spec/helpers', 'spec/views', 'spec/lib']
43454352
endif
43464353
let path += ['app/*', 'vendor', 'vendor/plugins/*/lib', 'vendor/plugins/*/test', 'vendor/rails/*/lib', 'vendor/rails/*/test']

0 commit comments

Comments
 (0)