Skip to content

Commit

Permalink
Partial :collection
Browse files Browse the repository at this point in the history
  • Loading branch information
tpope committed Jun 6, 2006
1 parent 5ee755e commit 49e4d7c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
4 changes: 3 additions & 1 deletion doc/rails.txt
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,14 @@ Then, a simple >
:'<,'>Partial blog/post
will suffice. (Note the use of a controller name in this example.)

Finally, take note of what happens when the range is nested inside a for or
.each loop.

==============================================================================
TODO *rails-todo*

Better interface to abbreviations (perhaps a separate plugin)
:Generate, and edit the file
:Partial that can handle collections
Some sort of <RailsLeader>

==============================================================================
Expand Down
53 changes: 43 additions & 10 deletions plugin/rails.vim
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,9 @@ function! s:MakePartial(bang,...) range abort
return
endif
let file = a:1
let range = a:firstline.",".a:lastline
let first = a:firstline
let last = a:lastline
let range = first.",".last
let curdir = expand("%:p:h")
let dir = fnamemodify(file,":h")
let fname = fnamemodify(file,":t")
Expand All @@ -798,30 +800,61 @@ function! s:MakePartial(bang,...) range abort
let name = fnamemodify(name,":r")
endif
let var = "@".name
let collection = ""
if dir =~ '^/'
let out = s:escapepath(b:rails_app_path).dir."/_".fname
let out = (b:rails_app_path).dir."/_".fname
elseif dir == ""
let out = s:escapepath(curdir)."/_".fname
let out = (curdir)."/_".fname
elseif isdirectory(curdir."/".dir)
let out = s:escapepath(curdir)."/".dir."/_".fname
let out = (curdir)."/".dir."/_".fname
else
let out = s:escapepath(b:rails_app_path)."/app/views/".dir."/_".fname
let out = (b:rails_app_path)."/app/views/".dir."/_".fname
endif
if filereadable(out)
echoerr "Partial exists"
return
endif
let out = s:escapepath(out)
" No tabs, they'll just complicate things
let spaces = matchstr(getline(a:firstline),"^ *")
if expand("%:e") == "rhtml"
let erub1 = '<%\s*'
let erub2 = '\s*-\=%>'
else
let erub1 = ''
let erub2 = ''
endif
let spaces = matchstr(getline(first),"^ *")
if getline(last+1) =~ '^\s*'.erub1.'end'.erub2.'\s*$'
let fspaces = matchstr(getline(last+1),"^ *")
if getline(a:firstline-1) =~ '^'.fspaces.erub1.'for\s\+\(\k\+\)\s\+in\s\+\([^ %>]\+\)'.erub2.'\s*$'
let collection = s:sub(getline(a:firstline-1),'^'.fspaces.erub1.'for\s\+\(\k\+\)\s\+in\s\+\([^ >]\+\)'.erub2.'\s*$','\1>\2')
elseif getline(a:firstline-1) =~ '^'.fspaces.erub1.'\([^ %>]\+\)\.each\s\+do\s\+|\s*\(\k\+\)\s*|'.erub2.'\s*$'
let collection = s:sub(getline(a:firstline-1),'^'.fspaces.erub1.'\([^ %>]\+\)\.each\s\+do\s\+|\s*\(\k\+\)\s*|'.erub2.'\s*$','\2>\1')
endif
if collection != ''
let var = matchstr(collection,'^\k\+')
let collection = s:sub(collection,'^\k\+>','')
let first = first - 1
let last = last + 1
endif
else
let fspaces = spaces
endif
if spaces != ""
silent! exe range.'sub/'.spaces.'//'
silent! exe range.'sub/^'.spaces.'//'
endif
silent! exe range.'sub?\w\@<!'.var.'\>?'.name.'?g'
silent! exe range.'sub?\%(\w\|[@:]\)\@<!'.var.'\>?'.name.'?g'
silent exe range."write ".out
let renderstr = "render :partial => '".fnamemodify(file,":r")."'"
if "@".name != var
if collection != ""
let renderstr = renderstr.", :collection => ".collection
elseif "@".name != var
let renderstr = renderstr.", :object => ".var
endif
if expand("%:e") == "rhtml"
let renderstr = "<%= ".renderstr." %>"
endif
silent exe "norm :".range."change\<CR>".spaces.renderstr."\<CR>.\<CR>"
silent exe "norm :".first.",".last."change\<CR>".fspaces.renderstr."\<CR>.\<CR>"
if renderstr =~ '<%'
norm ^6w
else
Expand Down

0 comments on commit 49e4d7c

Please sign in to comment.