1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Chat < ActiveRecord::Base KINDS = [[:office.l('Oficina'),0],['Personal',1]] SERVERS = [['Skype',0],['Msn',1],['Aim',2],['.Mac',3],['Jabber',4]] def kind; KINDS.fetch(kind_id).first ; end def server; SERVERS.fetch(server_id).first ; end end # _form.html.haml - form_for @chat do |f| = f.select(:kind_id, Chat::KINDS, {}) = f.select(:server_id, Chat::KINDS, {})
Refactorings
No refactoring yet !
elliottcable
June 9, 2008, June 09, 2008 16:16, permalink
I'm no Rails master. I avoid it like the plague to tell the truth. I'm also not sure of the purpose of the top multidimensional arrays, so lemme know if my refactor doesn't match your needs. I'm also entirely confused as to what Symbol#l does - it's certainly not Ruby core (NoMethodError: undefined method `l' for :office:Symbol). Finally, where's kind_id/server_id defined? Give as much context as possible with your code d-:
pimp
June 9, 2008, June 09, 2008 21:32, permalink
Sorry, I should have been much clearer about the above code.
the .l('String') method is from a localization plugin which uses simple strings and yaml files to build a multi-lingual UI. The above arrays are simple meant to populate select tags on a contact form, and have the options appear in the selected language.
Out of curiosity, why do you avoid Rails? Are you a merb fan? Roll your own framework?
So far this works a treat, I just wish the model code didnt make me want to gouge my eyes out :p
I'm sure I will catch hellfire for using global constants as well :ducks: