Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Separando as regras de negócios do Rails
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Celso Crivelaro
July 09, 2016
Programming
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Separando as regras de negócios do Rails
Separando as regras de negócios do Rails
Celso Crivelaro
July 09, 2016
More Decks by Celso Crivelaro
See All by Celso Crivelaro
Sistemas de Recomendação
celsocrivelaro
2
240
Big Data para Gerentes de Projetos
celsocrivelaro
0
72
FileSystems em Ruby com FUSE
celsocrivelaro
2
75
Padrões e Boas Práticas de Teste de Tela
celsocrivelaro
0
450
Projeto de APIs
celsocrivelaro
0
180
Testing Network Conditions with ToxiProxy
celsocrivelaro
1
400
Testing Network Conditions with ToxiProxy
celsocrivelaro
0
92
Actor Model in Ruby
celsocrivelaro
0
270
InfluxDB + Grafana
celsocrivelaro
0
270
Other Decks in Programming
See All in Programming
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
160
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
1.2k
今さら聞けない .NET CLI
htkym
0
210
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
5
510
プロポーザルを書いてもらう
pvcresin
0
570
「つくるAI」だけではバグは見つからない ~テストに必要な「見つけるAI」を分離させる戦略~
mfunaki
0
160
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
240
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
710
使いながら育てる Claude Code — 開発フローの1コマンド化 × 繰り返し指摘の自動仕組み化
shiki_kakaku
0
1.9k
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.7k
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
350
ソフトウェアラスタライザ
fadis
1
550
Featured
See All Featured
Marketing to machines
jonoalderson
1
5.7k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
260
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
310
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
780
Done Done
chrislema
186
16k
Facilitating Awesome Meetings
lara
57
7.1k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
GraphQLとの向き合い方2022年版
quramy
50
15k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
280
Product Roadmaps are Hard
iamctodd
55
12k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
380
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