$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Separando as regras de negócios do Rails
Search
Celso Crivelaro
July 09, 2016
Programming
0
180
Separando as regras de negócios do Rails
Separando as regras de negócios do Rails
Celso Crivelaro
July 09, 2016
Tweet
Share
More Decks by Celso Crivelaro
See All by Celso Crivelaro
Sistemas de Recomendação
celsocrivelaro
2
220
Big Data para Gerentes de Projetos
celsocrivelaro
0
64
FileSystems em Ruby com FUSE
celsocrivelaro
2
68
Padrões e Boas Práticas de Teste de Tela
celsocrivelaro
0
420
Projeto de APIs
celsocrivelaro
0
160
Testing Network Conditions with ToxiProxy
celsocrivelaro
1
390
Testing Network Conditions with ToxiProxy
celsocrivelaro
0
82
Actor Model in Ruby
celsocrivelaro
0
250
InfluxDB + Grafana
celsocrivelaro
0
260
Other Decks in Programming
See All in Programming
CSC509 Lecture 14
javiergs
PRO
0
220
STYLE
koic
0
150
ViewファーストなRailsアプリ開発のたのしさ
sugiwe
0
430
リリース時」テストから「デイリー実行」へ!開発マネージャが取り組んだ、レガシー自動テストのモダン化戦略
goataka
0
120
Microservices Platforms: When Team Topologies Meets Microservices Patterns
cer
PRO
1
1k
ソフトウェア設計の課題・原則・実践技法
masuda220
PRO
26
22k
30分でDoctrineの仕組みと使い方を完全にマスターする / phpconkagawa 2025 Doctrine
ttskch
3
800
sbt 2
xuwei_k
0
260
20 years of Symfony, what's next?
fabpot
2
350
チームをチームにするEM
hitode909
0
290
Rediscover the Console - SymfonyCon Amsterdam 2025
chalasr
2
160
生成AIを利用するだけでなく、投資できる組織へ
pospome
0
240
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
9
1k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
A designer walks into a library…
pauljervisheath
210
24k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.8k
Building an army of robots
kneath
306
46k
How to Ace a Technical Interview
jacobian
280
24k
Statistics for Hackers
jakevdp
799
230k
How to Think Like a Performance Engineer
csswizardry
28
2.4k
Automating Front-end Workflow
addyosmani
1371
200k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Transcript
Separando as regras de negócio do Rails Celso Crivelaro @celsocrivelaro
DEVELOPER @
None
Rails revolucionou frameworks MVC
Convenção sobre Configuração Muitos plugins / gems Documentação Baixo tempo
para subir um projeto
Mas não são todos que concordam...
None
None
Onde está o problema?
Sua App não é o Rails
None
Controller Model View
View não é só HTML
Model não é ActiveRecord
Comunidade Ruby se preocupa pouco com Arquitetura OO
account.rb 439 linhas account_spec.rb 1045 linhas
Dificuldades no upgrade de versão do Rails Má arquitetura torna
os testes difíceis Alterações no software são caras Difícil de dividir a app em várias
Como podemos desacoplar?
Views
Entrada de dados
controllers/users_controller.rb views/users/new.html.erb
controllers/users_controller.rb
Fácil mas problemático
Cadastro em passos Formulário preenche vários modelos Diferentes interfaces: Web
+ API
https://gist.github.com/peternixey/1978249 Mass Assignment => Strong Parameters
Form Objects
forms/user_form.rb
controllers/users_controllers.rb
Exibição de dados
Presenter Objects
controllers/users_controllers.rb presenters/user_show_presenter.rb views/users/show.erb.html
Mock nos testes Favorece Duck-typing
Models
ActiveRecord
ORM Regras de preenchimento Quanto menos regras de negócio melhor
Evite usar callbacks
Regras de Consulta
Policy Objects
Tem sentido levar estes métodos para todo lado? Métodos de
checagem / permissionamento
policies/user_policy.rb
http://ruby-doc.org/stdlib-2.3.0/libdoc/forwardable/rdoc/Forwardable.html
Regras de Ação
Service Objects
Métodos de ação Usa vários modelos Lógica de negócio complexa
Usa várias integrações
services/user_billing_service.rb
Invocando alguns Design Patterns
Pipeline / Chain of Responsibility
None
Strategy
None
Dicas Finais
Estude muito OO Estude arquitetura OO Desacople o seu código
Tente fazer algo sem o Rails
Referências
http://solnic.eu/2016/05/22/my-time-with-rails-is-up.html
http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
None
None
None
None
Obrigado! @celsocrivelaro http://crivelaro.me