Tidy vews, decorator and presenter

Preview:

Citation preview

Decorator Decorator & & 

PresenterPresenterBy: Carlos Sánchez Pérez          madrid­rbBy: Carlos Sánchez Pérez          madrid­rb

Tidy VIEWSTidy VIEWS

WHO AM I?

Person.new(NameName : 'Carlos Sánchez Pérez',LoveLove : 'Web Dev ­ Ruby & Rails',TwitterTwitter : '@carlossanchezp',JobJob : 'ASPgems',GithubGithub : 'carlossanchezp',Blog Blog ::'carlossanchezperez.wordpress.com')

Let me show you about my ideas, in my blog. I'll be there for you tomorrow....

The Problem

VIEWS: Responsibility Overload

FAT MODEL

 BIGGEST HELPERS

What's wrong with it?

app/views/users/show.html.erb

DECORATOR PRESENTER

PRESENTERS Pattern 

Tidy Views 

The idea of a Presenter was first introduced to the Rails community by Jay Fields in a series of blog articles in 2006 and 2007.

Rails Presenter Pattern Jay Fields

Wikipedia:Model–view–presenter

PRESENTER● Presenters are also decorators. ● The presenters is their proximity to the view and live very close to the view layer. 

● In fact, they are meant to be a representation of the delegate object within the view.

● The main goal of presenters is to keep logic out of the view.

● Presenters maintain an object­oriented approach to logic in the view.

MODEL

VIEW

My name is Presenter,Can I help you?

This way, presenters act like decorators. 

Wrap a Rails model and aid in the presentation.   

app/views/users/show.html.rb

DECORATOR Pattern

Tidy Views 

DECORATOR

● Start needing logic in the view or start thinking about a helper method, you can implement a method on the decorator instead.

● Attach additional responsibilities to an object dynamically.

● A decorator changes an object's skin. 

Jeff Casimir's draper is a gem that embodies the pattern and also allows you to invoke Rails' view helpers by exposing the view 

context in your decorators.

CHANGE:  UserDecorator.find(params[:id]

App/views/users/show.html.erb

DECORATOR PRESENTER

PRESENTERDECORATOR

How to share?In a view with decorator and without decorator

DRAPPER

def helpers Draper::ViewContext.current end alias_method :h, :helpers

Is there anything else?

Decorator

OTHER SIDE

Sending Notifications Using Decorators

CALLBACK

MODEL

SEND

“Single Responsibility Principle” 

MODELFIND USER

DECORATOR

CALLING METHOD DECORATOR

One more

Notifications

The same object diferent Render

Exhibit Pattern

EXHIBIT

● Exhibits are just decorators.

● Add some additional functionality.

● The additional functionality added will extend, but not disrupt.

● Introducing the Exhibit Pattern by Avdi Grimm in his book “Objects on Rails“.

Conclusion

The power of decorators 

LINKS

Decorator gem: Decorator gem: https://github.com/drapergem/draperhttps://github.com/drapergem/draper  

Objects In Rails Objects In Rails http://objectsonrails.com/http://objectsonrails.com/

http://blog.jayfields.com/2007/03/rails­presenter­pattern.htmlhttp://blog.jayfields.com/2007/03/rails­presenter­pattern.html

http://railscasts.com/episodes/286­draperhttp://railscasts.com/episodes/286­draper

Any questions?

Dedication to....

Thanks ASPgems and twitter people!!

THANKS A LOT!!

That's all folks!!and

Thanks a lot for your attention 

Recommended