Pregunta

I've just upgraded an old project to Rails 4 and I've just realized that it has upgrade the schema.rb using the new-style hash syntax. I suppose Rails is gonna use this syntax for all its generators.

How can I, friendly, say to Rails that I prefer the old-style syntax for hashes?

¿Fue útil?

Solución

schema.rb is created by rake db:migrate command. As per my knowledge, it will be hard to suggest the old-style syntax for hashes to Rails. BUT nothing is impossible, you can play around with rails/activerecord/lib/active_record/schema_dumper.rb file. The only problem is when you upgrade the rails gem next time it will override.

This old-style syntax to new-style syntax for hashes was done in Dump schema using new style hash commit.

Otros consejos

I know this is not exactly an answer to your question, but it might help nevertheless.

If you use vim, this will allow you to toggle between the old & new syntax (source):

function! s:RubyHashSyntaxToggle() range
  if join(getline(a:firstline, a:lastline)) =~# '=>'
    silent! execute a:firstline . ',' . a:lastline . 's/[^{,]*[{,]\?\zs:\([^: ]\+\)\s*=>/\1:/g'
  else
    silent! execute a:firstline . ',' . a:lastline . 's/[^{,]*[{,]\?\zs\([^: ]\+\):/:\1 =>/g'
  endif
endfunction
command! -bar -range RubyHashSyntaxToggle <line1>,<line2>call s:RubyHashSyntaxToggle()
noremap <Leader>rh :RubyHashSyntaxToggle<CR>

At max it will take you 3 keystrokes to get the schema the way you want. It is not automatic, but as a counterpart it will work on any file, not just on the schema.

You could invoke the substitution every time you save a file (I do that to remove extra spaces at the ends of lines).

And if you don't use vim, these regexes probably be adapted to other editors.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top