1

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?

fguillen
  • 36,125
  • 23
  • 149
  • 210
  • This is a Ruby question and not specific to Rails. However, there is no built in way to disable the short hash syntax. – DiegoSalazar Feb 06 '14 at 20:46
  • @diego.greyrobot why do you say _"This is a Ruby question and not specific to Rails"_? – fguillen Feb 06 '14 at 23:00
  • Ruby is the language, Rails is a web app framework. The short hash syntax is a feature of the language, not of the framework. You tagged the question with ruby-on-rails and not with ruby. And your question asked that of Rails. Just trying to help... pedantically. – DiegoSalazar Feb 06 '14 at 23:03
  • 1
    @diego.greyrobot I see, thanks for the clarification. But as I'm blaming to one of the Rails code generators as the responsible of this choice I think that _"This is a Rails question and not specific to Ruby"_ ;) – fguillen Feb 06 '14 at 23:36

2 Answers2

3

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.

Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
  • 1
    Even if this is not a solution to my problem I think this is the correct, and sad, answer :/. Thanks for the link to the commit. – fguillen Feb 07 '14 at 09:38
1

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.

kikito
  • 51,734
  • 32
  • 149
  • 189
  • What I'm worry about is that looks like from now on the Rails code generators are gonna use the new syntax for any code generated, I would like to have an option to tell it: please use the old syntax :), the same way I can tell it to use my favourite test framework. So I don't need to be cleaning up all the time. But thanks for the trick anyway. – fguillen Feb 07 '14 at 11:05