Skip to content

Instantly share code, notes, and snippets.

@Fernan2
Fernan2 / application_controller.rb
Created March 30, 2012 14:55
Duda de inherited_resources
...
def decorate_resource_or_collection(item_or_items)
klass = item_or_items.is_a?(Array) ? item_or_items.first.class.to_s : item_or_items.class.to_s
(klass + "Decorator").constantize.decorate(item_or_items)
rescue NameError
item_or_items
end
...
@Fernan2
Fernan2 / Excel.rb
Created May 24, 2012 11:09
Creación de Excels desde Rails
require 'spreadsheet'
include Spreadsheet
format.xls do
# ------ El bloque XLS debe ir sin paginación y con producto ------ #
fichero = "vila_#{@modo}_#{Time.now.to_f.to_s}.xls"
ruta = "#{Rails.root}/tmp/#{fichero}"
Spreadsheet.client_encoding = "UTF-8"
workbook = Spreadsheet::Workbook.new
@Fernan2
Fernan2 / gist:2786697
Created May 25, 2012 08:46
Hash de provincias #hack #performance
# Si tengo que acceder a la provincia en un bucle de 10.000 iteraciones, es poco eficiente hacer
Provincia.nombre(rest.provincia_id)
# En vez de eso, me creo un hash de provincias: {1=>"Álava/Araba", 2=>"Albacete", 3=>"Alicante",...}
provincias = {}
Provincia.select('id, nombre').each{ |p| provincias.merge!({ p.id => p.nombre }) }
# Y ya puedo acceder directamente sin llamar a nadie más:
provincias[rest.provincia_id]
@Fernan2
Fernan2 / gist:3696586
Created September 11, 2012 07:08
Instalación de programas en un Mac recién estrenado

Instalación y configuración

  • Preferencias del sistema (Dock, Seguridad, Economizador, Ratón, Compartir...)

  • Navegadores

  • Firefox (Copy as HTML Link, Firebug, Font Finder, PageSpeed Insights, SearchStatus, Web Developer, YSlow, ¿FireSass, Google Translator?) + preferencias

  • Chrome ya importa extensiones y preferencias si te logueas (Alexa Traffic Rank, Capturador de Páginas Web, Create Link, Firebug Lite, Pendule, Power Twitter, Resolution Test, SEO Status)

  • Editores:

@Fernan2
Fernan2 / gist:4030490
Created November 7, 2012 09:52
multidomain refraction rules
describe "dominios de sudamérica" do
it "rankia.cl se redirige a www.rankia.cl" do
get portada_foros_url(host: 'rankia.cl')
response.should redirect_to(portada_foros_url(host: 'www.rankia.cl'))
end
end
@Fernan2
Fernan2 / Textmate regexp for filtering capybara files
Created September 5, 2013 08:59
This regexp avoids Textmate to look into tmp/capybara files when searching
!.*/(\.[^/]*|coverage|tmp|CVS|_darcs|_MTN|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$
@Fernan2
Fernan2 / haml refactor
Last active December 22, 2015 19:39
haml refactor
Initial ugly version:
- if @post
%div{ class: "blog-titulo#{ ' peque' if @blog.titulo.length > 60 }" }= @blog.titulo
- else
%h1{ class: "blog-titulo#{ ' peque' if @blog.titulo.length > 60 }" }= @blog.titulo
Nice, but doesn't work:

[FIX] something is wrong and you have to fix it if ypu want your branch to be merged

[TIP] doing this in this way might be better

[DISC] brain fapping about something

[GP] good practices, is not wrong but if you dont fix it you will be whipped

=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
@Fernan2
Fernan2 / slow_query.log
Created February 7, 2014 10:03
Analizar el slow_query.log
scp deploy@db.rankia.com:/db/mysql/log/slow_query.log ~/Downloads
ruby ~/SlowQueryLogParser.rb ~/Downloads/slow_query.log number > ~/Downloads/slow_query_formatted.log
mate ~/Downloads/slow_query_formatted.log