33
Refactoring Rails apps with RubyMine Andrzej Krzywda Arkency

Refactoring Rails applications with RubyMine

Embed Size (px)

Citation preview

Page 1: Refactoring Rails applications with RubyMine

Refactoring Rails apps with RubyMine

Andrzej Krzywda Arkency

Page 2: Refactoring Rails applications with RubyMine

Andrzej Krzywda

• Arkency

• Rails since 2004

• hundreds of Rails projects reviewed

Page 3: Refactoring Rails applications with RubyMine

Agenda

• Typical Rails problems

• Beyond The Rails Way - new patterns

• Refactoring recipes with RubyMine

Page 4: Refactoring Rails applications 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

Page 5: Refactoring Rails applications with RubyMine

2.months.later

Page 6: Refactoring Rails applications with RubyMine

Typical Rails problems

• MVC is not enough

• first regression bugs

• slow tests

• slower progress

Page 7: Refactoring Rails applications with RubyMine

6.months.later

Page 8: Refactoring Rails applications with RubyMine

Typical Rails problems

• hidden dependencies

• implicit assumptions (aka Convention over Configuration)

• security bugs!

• Rails upgrades are now projects on their own with (huge) budgets

Page 9: Refactoring Rails applications with RubyMine

2.years.later

Page 10: Refactoring Rails applications with RubyMine

Typical Rails problems

Page 11: Refactoring Rails applications with RubyMine

Any questions so far?

Page 12: Refactoring Rails applications with RubyMine

New guidelines

• explicit > implicit

• reducing meta

• more building blocks

Page 13: Refactoring Rails applications with RubyMine

New patterns

• Inspired by:

• DDD

• Hexagonal Architecture

• DCI

• CQRS/Event Sourcing

Page 14: Refactoring Rails applications with RubyMine

The building blocks• Service objects

• Repositories

• Adapters

• Form objects

• Domain objects

• RubyMine

Page 15: Refactoring Rails applications with RubyMine
Page 16: Refactoring Rails applications with RubyMine

Extract a service object

• the gateway drug

• a training in refactoring

• turn a controller action into a method object

Page 17: Refactoring Rails applications with RubyMine

Questions?

Page 18: Refactoring Rails applications with RubyMine

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

Page 19: Refactoring Rails applications with RubyMine

newcreate

edit

update

new.haml

edit.haml_form.haml

show.rsb

common/error_messages.rsb

Page 20: Refactoring Rails applications with RubyMine

The filters algebra

• Filters depending on each other

• The order is important

• Hard to decouple

• communication via @ivars

Page 21: Refactoring Rails applications with RubyMine

At the controllers/views level everything is communicating

with each other via @ivars

Page 22: Refactoring Rails applications with RubyMine

Everything is depending on instance variables

Page 23: Refactoring Rails applications with RubyMine

Controller instance variables are the global,

mutable state

Page 24: Refactoring Rails applications with RubyMine

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

Page 25: Refactoring Rails applications with RubyMine
Page 26: Refactoring Rails applications with RubyMine
Page 27: Refactoring Rails applications with RubyMine
Page 28: Refactoring Rails applications with RubyMine

RubyMine coding

• look at the existing concerns

• run the subset of tests

• keep refactoring

Page 29: Refactoring Rails applications with RubyMine
Page 30: Refactoring Rails applications with RubyMine

Summary

• Typical Rails problems

• Refactoring example

• RubyMine support

Page 31: Refactoring Rails applications with RubyMine

• http://rails-refactoring.com/recipes/

Page 32: Refactoring Rails applications with RubyMine

Fearless Refactoring: Rails

ControllersToday 20% off with the

JETBRAINS coupon code!

http://rails-refactoring.com

Page 33: Refactoring Rails applications with RubyMine

Thank you!

Questions?