Skip to content

Commit 0872eec

Browse files
committed
Add "smart" projections for unknown app subdirectories
1 parent 91d4449 commit 0872eec

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

autoload/rails.vim

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4569,6 +4569,35 @@ function! s:app_engines() dict abort
45694569
return self.cache.get('engines')
45704570
endfunction
45714571

4572+
function! s:app_smart_projections() dict abort
4573+
let ts = getftime(self.path('app/'))
4574+
if self.cache.needs('smart_projections', ts)
4575+
let dict = {}
4576+
for dir in self.relglob('app/', '*s', '/')
4577+
let singular = rails#singularize(dir)
4578+
let glob = 'app/' . dir . '/*_' . singular . '.rb'
4579+
if dir !~# '\v^%(assets|models|views)$' &&
4580+
\ !has_key(s:default_projections, glob) &&
4581+
\ !empty(self.relglob('', glob))
4582+
let dict[glob] = {'type': s:gsub(tolower(singular), '\A+', ' ')}
4583+
endif
4584+
endfor
4585+
if has_key(dict, 'app/mailers/*_mailer.rb') || self.has_rails5()
4586+
let dict['app/mailers/*_mailer.rb'] = {
4587+
\ "affinity": "controller",
4588+
\ "template": ["class {camelcase|capitalize|colons}Mailer < ActionMailer::Base", "end"],
4589+
\ "type": "mailer"}
4590+
else
4591+
let dict['app/mailers/*.rb'] = {
4592+
\ "affinity": "controller",
4593+
\ "template": ["class {camelcase|capitalize|colons} < ActionMailer::Base", "end"],
4594+
\ "type": "mailer"}
4595+
endif
4596+
call self.cache.set('smart_projections', dict, ts)
4597+
endif
4598+
return self.cache.get('smart_projections')
4599+
endfunction
4600+
45724601
function! s:extend_projection(dest, src) abort
45734602
let dest = copy(a:dest)
45744603
for key in keys(a:src)
@@ -4642,11 +4671,6 @@ let s:default_projections = {
46424671
\ "template": ["class {camelcase|capitalize|colons}Job < ActiveJob::Base", "end"],
46434672
\ "type": "job"
46444673
\ },
4645-
\ "app/mailers/*.rb": {
4646-
\ "affinity": "controller",
4647-
\ "template": ["class {camelcase|capitalize|colons} < ActionMailer::Base", "end"],
4648-
\ "type": "mailer"
4649-
\ },
46504674
\ "app/models/*.rb": {
46514675
\ "affinity": "model",
46524676
\ "template": ["class {camelcase|capitalize|colons}", "end"],
@@ -4857,6 +4881,7 @@ function! s:app_projections() dict abort
48574881
call s:combine_projections(dict, v)
48584882
endif
48594883
endfor
4884+
call s:combine_projections(dict, self.smart_projections())
48604885
call s:combine_projections(dict, get(g:, 'rails_projections', ''))
48614886
for gem in keys(get(g:, 'rails_gem_projections', {}))
48624887
if self.has_gem(gem)
@@ -4898,7 +4923,7 @@ function! s:app_projections() dict abort
48984923
return dict
48994924
endfunction
49004925

4901-
call s:add_methods('app', ['gems', 'has_gem', 'engines', 'projections'])
4926+
call s:add_methods('app', ['gems', 'has_gem', 'engines', 'smart_projections', 'projections'])
49024927

49034928
let s:transformations = {}
49044929

doc/rails.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ They also allow for jumping to methods or line numbers using the same syntax
155155
as |:rails-R|, and file creation (with a bit of boilerplate) can be forced by
156156
adding a ! after the filename (not after the command itself!).
157157

158-
There are also "classic" versions of these commands that start with :R (e.g.,
159-
:Rmodel, :RSmodel, :RVmodel, :RTmodel, and :RDmodel).
158+
Additionally, for each unrecognized directory of the form app/{type}s/
159+
containing at least one file matching *_{type}.rb, a command of the form
160+
:E{type} is automatically defined.
160161

161162
:Econtroller |rails-:Econtroller|
162163
:Eenvironment |rails-:Eenvironment|

0 commit comments

Comments
 (0)