Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Presenters

Rails Presenters

Fb7edc7cc7a53c903af74d07dcecf9dc?s=128

Revath S Kumar

August 07, 2014
Tweet

Transcript

  1. RAILS : PRESENTERS

  2. ABOUT Rubyist / JavaScripter Yeoman Team Member @keralarb / @keralajs

    Works at @whatznear Blog at blog.revathskumar.com Twitter/Github - @revathskumar +RevathSKumar
  3. IS IT GOOD IDEA TO WRITE LOGIC IN VIEW? NO

  4. WHAT ABOUT CONDITIONS? LIKE < % i f @ u

    s e r . w e b s i t e . p r e s e n t ? % > < % = l i n k _ t o @ u s e r . w e b s i t e , @ u s e r . w e b s i t e % > < % e l s e % > < s p a n c l a s s = " n o n e " > N o n e g i v e n < / s p a n > < % e n d % > WE USUALLY DO, RIGHT?
  5. WHEN THE LOGIC GETS COMPLEX, WHAT WE DO? WE MOVE

    THAT TO HELPERS
  6. BUT RAILS HELPERS ARE PROCEDURAL < % = w e

    b p a g e ( @ u s e r . w e b s i t e ) % >
  7. IT WILL BE BETTER IF IT CAN BE USED AS

    < % = @ u s e r . w e b p a g e % > Shorter, Simpler and sleek
  8. BUT IN ORDER TO MAKE IS SIMPLE, WE CAN'T MOVE

    THAT LOGIC INTO MODEL
  9. THATS WHERE "PRESENTERS" COMES IN A BRIDGE BETWEEN "MODEL" &

    "VIEW"
  10. # a p p / p r e s e

    n t e r s / u s e r _ p r e s e n t e r . r b c l a s s U s e r P r e s e n t e r d e f i n i t i a l i z e u s e r , t e m p l a t e @ u s e r = u s e r @ t e m p l a t e = t e m p l a t e e n d d e f w e b s i t e i f @ u s e r . w e b s i t e . p r e s e n t ? @ t e m p l a t e . l i n k _ t o @ u s e r . w e b s i t e , @ u s e r . w e b s i t e e l s e @ t e m p l a t e . c o n t e n t _ t a g : s p a n , ' N o n e G i v e n ' , c l a s s : ' n o n e ' e n d e n d e n d -- # a p p / c o n t r o l l e r s / u s e r _ c o n t r o l l e r . r b c l a s s U s e r C o n t r o l l e r < A p p l i c a t i o n C o n t r o l l e r d e f s h o w u s e r = U s e r . f i n d ( p a r a m s [ : i d ] ) @ u s e r = U s e r P r e s e n t e r . n e w ( u s e r , v i e w _ c o n t e x t ) e n d e n d
  11. IN VIEW AFTER USING 'PRESENTER' < ! - - a

    p p / v i e w s / u s e r s / s h o w . h t m l . e r b - - > < d t > W e b s i t e : < / d t > < d d > < % = @ u s e r . w e b s i t e % > < / d d >
  12. ADVANTAGES Moved the logic out of view Testable

  13. QUEST

  14. None