Refactoring Rails apps with RubyMine
Andrzej Krzywda Arkency
Andrzej Krzywda
• Arkency
• Rails since 2004
• hundreds of Rails projects reviewed
Agenda
• Typical Rails problems
• Beyond The Rails Way - new patterns
• Refactoring recipes with RubyMine
Rails is great• In the first month of development
• Quick results in almost no code
• Quick feedback loop
• Predictable speed
• Your customer loves you
• You love Rails
2.months.later
Typical Rails problems
• MVC is not enough
• first regression bugs
• slow tests
• slower progress
6.months.later
Typical Rails problems
• hidden dependencies
• implicit assumptions (aka Convention over Configuration)
• security bugs!
• Rails upgrades are now projects on their own with (huge) budgets
2.years.later
Typical Rails problems
Any questions so far?
New guidelines
• explicit > implicit
• reducing meta
• more building blocks
New patterns
• Inspired by:
• DDD
• Hexagonal Architecture
• DCI
• CQRS/Event Sourcing
The building blocks• Service objects
• Repositories
• Adapters
• Form objects
• Domain objects
• RubyMine
Extract a service object
• the gateway drug
• a training in refactoring
• turn a controller action into a method object
Questions?
Typical controller coupling• new-action uses the ‘new’ view
• create-action uses the ‘new’ view
• edit-action uses the ‘new’ view
• ‘new’ view uses _form partial
• ‘edit’ view uses _form partial
• ‘edit’ action uses ‘edit’ view
• ‘update’ action uses ‘edit’ view
newcreate
edit
update
new.haml
edit.haml_form.haml
show.rsb
common/error_messages.rsb
The filters algebra
• Filters depending on each other
• The order is important
• Hard to decouple
• communication via @ivars
At the controllers/views level everything is communicating
with each other via @ivars
Everything is depending on instance variables
Controller instance variables are the global,
mutable state
the Redmine project• a project management tool
• contains a time-tracking module
• http://www.redmine.org/projects/redmine/wiki/RedmineTimeTracking
• since 2007
• https://github.com/redmine/redmine
RubyMine coding
• look at the existing concerns
• run the subset of tests
• keep refactoring
Summary
• Typical Rails problems
• Refactoring example
• RubyMine support
• http://rails-refactoring.com/recipes/
Fearless Refactoring: Rails
ControllersToday 20% off with the
JETBRAINS coupon code!
http://rails-refactoring.com
Thank you!
Questions?