Efficient rails
-
Upload
fabio-perrella -
Category
Technology
-
view
249 -
download
3
Transcript of Efficient rails
Efficient RailsFabio Perrella
Agenda- Dicas de...- Terminal- Git- Rails Console- Editor- Dicas de Código- Models- Views- Assets- Mailers- Testes
Terminal
Não use o terminal default- Terminator, iTerm2- Zsh > Bash
FASDhttps://github.com/clvv/fasd
Acesso rápido a arquivos e diretórios
HTTPIEhttps://github.com/jkbrzt/httpie
Cliente de http amigável (comparado ao curl)
Git
Prompt coloridohttps://github.com/fabioperrella/dotfiles/blob/master/dotfiles/.gitpromptrc
Git lg (pretty log)https://github.com/fabioperrella/dotfiles/blob/master/dotfiles/.gitconfig#L8
Git prepare-commit hook (add nome da branch)fabioperrella.blogspot.com/2014/03/adicionando-o-nome-do-branch-em-todos.html
Git - Limpeza de brancheshttps://gist.github.com/fabioperrella/1f56229a4c532fc000ed
- Limpa diretórios locais e remotos ja mergeados no master
Dotfileshttps://github.com/fabioperrella/dotfiles
Baseado no https://dotfiles.github.io
Git - Toggle branchesgit checkout -
cd -
Rails Console
Use o .pryrc para comandos frequentes
ActiveRecord lazy loading no console
Atalhos
Bundle show/open
Bundle local pathhttp://ryanbigg.com/2013/08/bundler-local-paths/
Gem jazz_handsRails console + bonito/eficiente
https://github.com/nixme/jazz_hands
- Pry- Pry-debugger- Awesome Print- Hirb
...
Rails console --sandbox- Nenhuma query é comitada
Editor (Sublime)
Atalhos para agilizar a vida- Goto Definition (F12) (só no sublime3)- Jump Back (Alt + -)- Go (and back) to spec (Ctrl + .) (plugin)- Switch project (Ctrl + Alt + p)- Go to anything (Ctrl + p)- Go to symbol (Ctrl + r)- Comandos (Ctrl + shift + p)- Ctrl + Shift + v (paste and indent)
Plugins- RailsGoToSpec- SideBarEnhancements- All Autocomplete- GitGutter
Snippets customizadosByebug (bb)
~.config/sublime-text-3/Packages/User/byebug.sublime-snippet
Dicas de Código - Models
Gem annotatehttps://github.com/ctran/annotate_models
Gera e atualiza schema info nos modelos
Detectando N+1
Gem bullethttps://github.com/flyerhzm/bullet
Detecta N+1 por heurística, loga ou lança exceção quando achar
Gem rails-footnotesMostra informações sobre a página como:
- Queries- Variáves de sessão- Cookies- Variáves de instancia no controller- Parâmetros- etc...
Foreign Key nas migrationsA partir do rails 4.2
Gem immigranthttps://github.com/jenseng/immigrant
Gera foreign keys que foram esquecidas
Immigrant:check_keys
Dicas de Views
Partials reusáveisMuitas variáveis de instância nas actions são “bad smells”
Regras para seguir:
1) Só utilizar 1 variável de instância por action
2) Partial só pode usar variáveis através do locals
3) Documentar cada variável necessária na partial
Só utilizar 1 variável de instância por action
Gem cellshttps://github.com/apotonick/cells
View models
Partial só pode usar variáveis através do locals
Dicas de assets
Gem quiet_assets
Gem lograge
Gem autoprefixer-rails
Dicas sobre Mailers
Organização das views
Gem letter_opener ou mailcatcherLetterOpener:
- Ao disparar email, abre browser com html renderizado
Mailcatcher:
- Sobe SMTP fake para capturar email- Sobre webmail leve para visualizar
Validação de emails/^(?=[A-Z0-9][A-Z0-9@._%+-]{5,253}+$)[A-Z0-9._%+-]{1,64}+@(?:(?=[A-Z0-
9-]{1,63}+\.)[A-Z0-9]++(?:-[A-Z0-9]++)*+\.){1,8}+[A-Z]{2,63}+$/
Solução:
- Só validar se contém “@” ?
Dicas de Testes
Gem zeus ou spring Acelera (fazendo cache) carregamento da aplicação
Bom para:
- Rodar testes- Entrar no console- Subir servidor- Rodar rakes
Muito simples de usar (zeus)
Rspec only failures
Testes rápidos e sem dependência de ordem
Gem parallel_testsRoda testes em paralelo
Necessita múltiplas instâncias de banco
Cuidado se testes dependem de algum outro recurso como redis/memcache
Debugging
Gem pry-byebugComandos:
- Next- Continue- Step
.pryrc
FimDúvidas??
@fabioperrella