Slide 1

Slide 1 text

DECORATOR & PRESENTER DESIGN PATTERN @DonSchado | 21.05.2014

Slide 2

Slide 2 text

WHY?

Slide 3

Slide 3 text

Because your controllers become bloated and logic begins to creep into your views (templates)

Slide 4

Slide 4 text

implement procedural helpers or adding view related bulk to your models? Solutions?

Slide 5

Slide 5 text

implement procedural helpers or adding view related bulk to your models? Solutions? helpers are shit making fat models even fatter?
 No class should be fat. Ever.

Slide 6

Slide 6 text

Why are helpers shit? http://nicksda.apotomo.de/2011/10/rails-misapprehensions-helpers-are-shit/ „all helpers are mixed into the view automatically (…) [and are] globally available“ „There is nothing wrong with having those little helpers in your view. What I don’t like is that they are called without an obvious receiver – they look and feel like functions. This is wrong.“

Slide 7

Slide 7 text

Why are helpers shit? http://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters „No seriously, helpers suck“ „Why is it in Ruby that everything is an object, even integers, yet as soon as we need to format a date, we all turn into Dijkstra and bust out structured programming?“

Slide 8

Slide 8 text

DECORATOR

Slide 9

Slide 9 text

Decorator pattern (also known as wrapper) is a design pattern that allows to dynamically add behavior to an individual object. can be used on it’s own or wrapped by decorator each decorator HAS_A (wraps) a component (holds a reference) (composition) (inheritance)

Slide 10

Slide 10 text

The decorator wraps the model, and deals only with presentational concerns. In the controller, you decorate the article before handing it off to the view

Slide 11

Slide 11 text

https://github.com/drapergem/draper Draper: View Models for Rails Draper adds an object-oriented layer of presentation logic to your Rails application

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

PRESENTER

Slide 16

Slide 16 text

A presenter is a special case of the "Decorator design pattern". http://sokolmichael.com/posts/2012-01-14-model-view-controller-presenter-better-separation-of-concern MVCP - Better Separation of Concern

Slide 17

Slide 17 text

http://blog.jayfields.com/2007/03/rails-presenter-pattern.html Presenter was inspired by the various GUI patterns documented by Martin Fowler. An architecture that uses the Presenter pattern provides view specific data as attributes of an instance of the Presenter. The Presenter's state is an aggregation of model and user entered data.

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

7 Patterns to Refactor Fat ActiveRecord Models http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ Service Objects Value Objects Form Objects Query Objects View Objects Policy Objects Decorators further reading:

Slide 21

Slide 21 text

thx!(and have fun refactoring all the fat classes)