Skip to content

Instantly share code, notes, and snippets.

@Fernan2
Last active October 18, 2016 12:15
Show Gist options
  • Select an option

  • Save Fernan2/f3c2494808d4de7ce328bb11564d1a20 to your computer and use it in GitHub Desktop.

Select an option

Save Fernan2/f3c2494808d4de7ce328bb11564d1a20 to your computer and use it in GitHub Desktop.
Refactor
Hemos hecho un scaffolding para validar con nuestro cliente una propuesta:
rails g scaffold Client first_name:string last_name:string email:string phone:string city:string address:string website:string
Esto genera, entre otros, el archivo _form.html.haml incluido en este Gist.
Una vez que el scaffolding ha sido validado, lo queremos "pasar a limpio"; y lo primero que nos pide un refactor son todas esas repeticiones de las mismas tres líneas para cada campo:
.field
= f.label xxxx
= f.text_field xxxx
No queremos que nuestro maquetador, cuando tenga que aplicar algún cambio de estilos, tenga que repetir el cambio siete veces, una por cada campo!
Hay varias posibles soluciones, ¿cómo harías tú el refactor?
= form_for @client do |f|
- if @client.errors.any?
#error_explanation
%h2= "#{pluralize(@client.errors.count, "error")} prohibited this client from being saved:"
%ul
- @client.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :first_name
= f.text_field :first_name
.field
= f.label :last_name
= f.text_field :last_name
.field
= f.label :email
= f.text_field :email
.field
= f.label :phone
= f.text_field :phone
.field
= f.label :city
= f.text_field :city
.field
= f.label :address
= f.text_field :address
.field
= f.label :website
= f.text_field :website
.actions
= f.submit 'Save'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment