tidy vews, decorator and presenter

52
Decorator Decorator Presenter Presenter By: Carlos Sánchez Pérez          madrid-rb By: Carlos Sánchez Pérez          madrid-rb Tidy VIEWS Tidy VIEWS

Upload: carlos-sanchez-perez

Post on 18-Jul-2015

178 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Tidy vews, decorator and presenter

Decorator Decorator & & 

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

Tidy VIEWSTidy VIEWS

Page 2: Tidy vews, decorator and presenter

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....

Page 3: Tidy vews, decorator and presenter

The Problem

Page 4: Tidy vews, decorator and presenter

VIEWS: Responsibility Overload

Page 5: Tidy vews, decorator and presenter

FAT MODEL

Page 6: Tidy vews, decorator and presenter

 BIGGEST HELPERS

Page 7: Tidy vews, decorator and presenter

What's wrong with it?

Page 8: Tidy vews, decorator and presenter
Page 9: Tidy vews, decorator and presenter

app/views/users/show.html.erb

Page 10: Tidy vews, decorator and presenter

DECORATOR PRESENTER

Page 11: Tidy vews, decorator and presenter

PRESENTERS Pattern 

Tidy Views 

Page 12: Tidy vews, decorator and presenter

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

Page 13: Tidy vews, decorator and 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.

Page 14: Tidy vews, decorator and presenter

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.   

Page 15: Tidy vews, decorator and presenter
Page 16: Tidy vews, decorator and presenter
Page 17: Tidy vews, decorator and presenter
Page 18: Tidy vews, decorator and presenter
Page 19: Tidy vews, decorator and presenter

app/views/users/show.html.rb

Page 20: Tidy vews, decorator and presenter

DECORATOR Pattern

Tidy Views 

Page 21: Tidy vews, decorator and presenter

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. 

Page 22: Tidy vews, decorator and presenter

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.

Page 23: Tidy vews, decorator and presenter

CHANGE:  UserDecorator.find(params[:id]

Page 24: Tidy vews, decorator and presenter
Page 25: Tidy vews, decorator and presenter

App/views/users/show.html.erb

Page 26: Tidy vews, decorator and presenter

DECORATOR PRESENTER

PRESENTERDECORATOR

Page 27: Tidy vews, decorator and presenter

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

Page 28: Tidy vews, decorator and presenter
Page 29: Tidy vews, decorator and presenter
Page 30: Tidy vews, decorator and presenter

DRAPPER

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

Page 31: Tidy vews, decorator and presenter
Page 32: Tidy vews, decorator and presenter

Is there anything else?

Page 33: Tidy vews, decorator and presenter

Decorator

OTHER SIDE

Sending Notifications Using Decorators

Page 34: Tidy vews, decorator and presenter

CALLBACK

MODEL

SEND

Page 35: Tidy vews, decorator and presenter
Page 36: Tidy vews, decorator and presenter

“Single Responsibility Principle” 

MODELFIND USER

DECORATOR

CALLING METHOD DECORATOR

Page 37: Tidy vews, decorator and presenter

One more

Notifications

Page 38: Tidy vews, decorator and presenter
Page 39: Tidy vews, decorator and presenter
Page 40: Tidy vews, decorator and presenter
Page 41: Tidy vews, decorator and presenter

The same object diferent Render

Page 42: Tidy vews, decorator and presenter

Exhibit Pattern

Page 43: Tidy vews, decorator and presenter

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“.

Page 44: Tidy vews, decorator and presenter
Page 45: Tidy vews, decorator and presenter
Page 46: Tidy vews, decorator and presenter

Conclusion

Page 47: Tidy vews, decorator and presenter

The power of decorators 

Page 48: Tidy vews, decorator and presenter

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

Page 49: Tidy vews, decorator and presenter

Any questions?

Page 50: Tidy vews, decorator and presenter

Dedication to....

Page 51: Tidy vews, decorator and presenter

Thanks ASPgems and twitter people!!

THANKS A LOT!!

Page 52: Tidy vews, decorator and presenter

That's all folks!!and

Thanks a lot for your attention