Refactoring Your Rails Application

Get Started. It's Free
or sign up with your email address
Refactoring Your Rails Application by Mind Map: Refactoring Your Rails Application

1. Speakers

1.1. Zach Dennis (Atomic Object)

1.2. Drew Colthorp (Atomic Object)

2. What to refactor

2.1. Code that smells

2.1.1. Duplicated code smell Project find en varias acciones

2.1.2. Shotgum surgery smell un cambio implica cambiar muchas cosas

2.1.3. Long method smell

2.1.4. Inappropiate intimacy smell un find demasiado sofisticado en el controlador

2.1.5. Divergent change smell a class is commonly changing in different reasons

2.1.6. Large class smell A class is doing too much too many instance variables too much code too many methods

2.1.7. Feature envy smell A methis is more interested in a cvlass other than the one is actually on

2.1.8. More

2.2. How to do refactor (small changes)

3. How do you refactor

3.1. small steps

3.2. two hats

3.3. solid tests

3.3.1. TDD / BDD Red. Green. Refactor

4. Rails -> MVC

4.1. Skinny Controller / Fat Model

4.2. Refactor to new artifacts

4.2.1. Presenters

4.2.2. Services

4.2.3. Mixins

4.2.4. Renderers

5. Examples

5.1. discharge model with Mixins

5.1.1. project.rb statistics Crea una carpeta project dir en models Cread models/statistics Mueve los métodos desde project.rb include Project::Statistics project_spec.rb has_many :stories do include Project::StoriesAssociationMethods using extend mejor que include has_many :stories, :exclude => Projects::StoriesAssociationMethods

5.2. Move logic from controller to presenters

5.2.1. formulario con selects. las listas para los selectes vienen en variables desde el controlador

5.2.2. Opción 1. Pasar que el objeto expense tenga varios métodos, cada uno para cada con los valores de cada select

5.2.3. What is a presenter? Decorators wrap objects Can work for individual models ExpensePresenter can work for specific UI components PlayerCardPresenter

5.2.4. class ExpensePresenter initialize(expense) delegate :description, :amount, :to => :@expense def possible_expense_types

5.2.5. Bad smells cargar variables solo con find :all tener before_filter para eso tener helper methods para eso

5.2.6. ejemplo app/presenters/story_presenter.rb class StoryPresenter En el controlador @story = @project.find_by... admite que quizá sea mejor user @story_presenter

5.2.7. David Chemlisky propone alternativa En lugar de un presenter que haga de wrapper Un modulo que extienda el modelo Y pasar a la vista un modelo extendido Ventaja: no hay que preocuparse de delegar o del method_missiing @story.extend StoryPresenter

5.3. Move logic from view to presenters

5.3.1. <% groups = user.admin? ? Group.find : user.groups %>

5.3.2. Sol: user.groups (user is a presenter)

5.4. Extract operations into service

5.4.1. Service: operations not state

5.4.2. Basics controller def create ProjectService.create_project(params[:project]) end app/services/project_service.rb def self.create_project #complex creation logic end

5.4.3. Possibilities def create ProjectService.create_project(params[:project]) do |result| result.success do # success logic end result.failure do # failure logic end end end

5.5. Extract RJS into Renderer

5.5.1. ugly, nasty (realistic) rjs file

5.5.2. render with :site do

5.6. More refactorings

5.6.1. Extract complex creation into factory

5.6.2. move before filter and friends into app. service

5.6.3. move view data from controller into presenter

5.6.4. move view logic from controller into presenter

5.6.5. move extrinsic callback into observer

5.6.6. ...