Presenting Presenters on Rails

21
new bamboo Presenting Presenters on Rails

description

 

Transcript of Presenting Presenters on Rails

Page 1: Presenting Presenters on Rails

new bamboo

Presenting Presenterson Rails

Page 2: Presenting Presenters on Rails

new bamboo

The ProblemEither:

Cluttering views with logic.

or

View logic in business logic.

Page 3: Presenting Presenters on Rails

new bamboo

class UsersController def show @user = User.find(params[:id]) endend

<div> <% if CONFIG.date_format == :us %> <%= @user.created_at.strftime('%m/%d/%y') %> <% elsif CONFIG.date_format == :rest_of_the_world %> <%= @user.created_at.strftime('%d/%m/%y') %> <% end %></div>

Page 4: Presenting Presenters on Rails

new bamboo

<div> <%= @user_signup_date %></div>

class UsersController def show @user = User.find(params[:id]) if CONFIG.date_format == :us @user_signup_date = @user.created_at.strftime('%m/%d/%y') elsif CONFIG.date_format == :rest_of_the_world @user_signup_date = @user.created_at.strftime('%d/%m/%y') end endend

Page 5: Presenting Presenters on Rails

new bamboo

Presenters

ViewControllerModel

Page 6: Presenting Presenters on Rails

new bamboo

Presenters

ViewControllerModel

Presenter

Page 7: Presenting Presenters on Rails

new bamboo

class UserPresenter def initialize(user) @user = user end attr_reader :user def signup_date if CONFIG.date_format == :us self.user.created_at.strftime('%m/%d/%y') elsif CONFIG.date_format == :rest_of_the_world self.user.created_at.strftime('%d/%m/%y') end endend

Page 8: Presenting Presenters on Rails

new bamboo

class UsersController def show @user = User.find(params[:id]) @user_presenter = UserPresenter.new(@user) endend

<div> <%= @user_presenter.signup_date %></div>

Page 9: Presenting Presenters on Rails

new bamboo

Disadvantages

• Contrived example

• move the logic into the model

• should the model really handle data representation

• move the logic into a helper

• helpers are ugly

Page 10: Presenting Presenters on Rails

new bamboo

@user_presenter.signup_date

or

format_date(@user.created_at)

Page 11: Presenting Presenters on Rails

new bamboo

Disadvantages• Accessibility for designers

• it’s only a convention. Especially if the variables are suffixed with “presenter” for example.

• Current templating language is Ruby anyway.

• a presenter is just a view without angle brackets

Page 12: Presenting Presenters on Rails

new bamboo

My Views on Presenters

• Overkill in most cases.

• Best used in applications, with a lot of configuration that governs the view.

Page 13: Presenting Presenters on Rails

new bamboo

Conductors : the Anti-Presenter

ViewControllerModel

Presenter

Page 14: Presenting Presenters on Rails

new bamboo

Conductors : the Anti-Presenter

ViewControllerModel

Presenter

Conductor

Page 15: Presenting Presenters on Rails

new bamboo

Multiple Objects, One Form

Page 16: Presenting Presenters on Rails

new bamboo

Card Details AvatarCompany

User Form

Update

Page 17: Presenting Presenters on Rails

new bamboo

One Object, Multiple Forms

Page 18: Presenting Presenters on Rails

new bamboo

Company

Information Form Change Settings Change Password

Update

Page 19: Presenting Presenters on Rails

new bamboo

Limitations

• Doesn’t handle has_many associations

• quite trivial to implement

Page 20: Presenting Presenters on Rails

new bamboo

My Opinions of Conductors

• Extremely useful, in the right situations

• connecting associated objects

• DRY’s the controller code

• Potential for abuse

• probably not worth it if there is only one associated model

Page 21: Presenting Presenters on Rails

new bamboo

Notable Mentions

• Blueprint CSS Framework

• an easy, but pretty demo app

• http://code.google.com/p/blueprintcss/

• Model - Conductor - Controller (MMC)

• heavyweight

• http://mcc.rubyforge.org/svn/trunk/