Online Mind Mapping and Brainstorming

Create your own awesome maps

Online Mind Mapping and Brainstorming

Even on the go

with our free apps for iPhone, iPad and Android

Get Started

Already have an account? Log In

Refactoring Your Rails Application by Mind Map: Refactoring Your Rails Application
0.0 stars - 0 reviews range from 0 to 5

Refactoring Your Rails Application


Zach Dennis (Atomic Object)

Drew Colthorp (Atomic Object)

What to refactor

Code that smells

Duplicated code smell, Project find en varias acciones

Shotgum surgery smell, un cambio implica cambiar muchas cosas

Long method smell

Inappropiate intimacy smell, un find demasiado sofisticado en el controlador

Divergent change smell, a class is commonly changing in different reasons

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

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


How to do refactor (small changes)

How do you refactor

small steps

two hats

solid tests

TDD / BDD Red. Green. Refactor

Rails -> MVC

Skinny Controller / Fat Model

Refactor to new artifacts






discharge model with Mixins

project.rb, statistics, Crea una carpeta project dir en models, Cread models/statistics, module Project::Statistics, Mueve los métodos desde project.rb, include Project::Statistics, project_spec.rb, Crea carpeta project, crea statistics_spec.rb, corrige el path del helper, mueve las specs, has_many :stories do include Project::StoriesAssociationMethods, using extend mejor que include has_many :stories, :exclude => Projects::StoriesAssociationMethods

Move logic from controller to presenters

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

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

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

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

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

ejemplo, app/presenters/story_presenter.rb, class StoryPresenter, def possible_statuses, Status.find...., initialize(story), delegate :id, :class, :errors, :to_param, :new_record?, :to => @story, Muchos parámetros?, add metod_missing? @story.send name, *args, En el controlador @story = @project.find_by..., admite que quizá sea mejor user @story_presenter

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

Move logic from view to presenters

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

Sol: user.groups (user is a presenter)

Extract operations into service

Service: operations not state

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

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

Extract RJS into Renderer

ugly, nasty (realistic) rjs file

render with :site do

More refactorings

Extract complex creation into factory

move before filter and friends into app. service

move view data from controller into presenter

move view logic from controller into presenter

move extrinsic callback into observer