Cells

883 views
827 views

Published on

Slides of the talk I gave at Pastis.rb on 14/11/2012.

Published in: Technology
1 Comment
1 Like
Statistics
Notes
  • C'est super intéressant ! J'aime bien cette approche, ça a l'air d'être du mvc imbriqué.
    En tous les cas, avec ta présentation à Paris.rb, tu chômes pas !
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
883
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Cells

  1. 1. CellsSimon Courtois - @happynoff
  2. 2. Problématique Comment partager un widget entre les pages du site ?github: apotonick/cells
  3. 3. Solution à l’ancienne # app/controllers/application_controller.rb class ApplicationController < ActionController::Base before_filter :set_shopping_cart private def set_shopping_cart @shopping_cart = ShoppingCart.for_user(current_user) end end # app/views/layouts/application.html.erb <%= render shared/shopping_cart, shopping_cart: @shopping_cart %> # app/views/shared/_shopping_cart.html.erb <% if @shopping_cart %> # shopping cart rendering <% end %>github: apotonick/cells
  4. 4. Solution avec Cells # app/views/layouts/application.html.erb <%= render_cell :shopping_cart, :display, user: current_user %> # app/cells/shopping_cart_cell.rb class ShoppingCartCell < Cell::Rails def display(opts) @shopping_cart = ShoppingCart.for_user(opts[:user]) render end end # app/cells/shopping_cart/display.html.erb <% if @shopping_cart %> # shopping cart rendering <% end %>github: apotonick/cells
  5. 5. Avantages Séparation des responsabilités Meilleure organisation du code app/ cells/ shopping_cart/ display.html.erb shopping_cart.rbgithub: apotonick/cells
  6. 6. Aller plus loin - actions # app/cells/shopping_cart_cell.rb class ShoppingCartCell < Cell::Rails def display(opts) @shopping_cart = ShoppingCart.for_user(opts[:user]) render end def count(opts) shopping_cart = ShoppingCart.for_user(opts[:user]) @items_count = shopping_cart.items_count render end end # app/views/layouts/application.html.erb <%= render_cell :shopping_cart, :display, user: current_user %> <%= render_cell :shopping_cart, :count, user: current_user %>github: apotonick/cells
  7. 7. Aller plus loin - cache # app/cells/shopping_cart_cell.rb class ShoppingCartCell < Cell::Rails cache :display, expires_in: 10.minutes def display(opts) # ... end endgithub: apotonick/cells
  8. 8. Aller plus loin - cache # app/cells/shopping_cart_cell.rb class ShoppingCartCell < Cell::Rails cache :display do |cell, opts| opts[:user] end def display(opts) # ... end endgithub: apotonick/cells
  9. 9. Aller plus loin - helpers # app/cells/shopping_cart_cell.rb class ShoppingCartCell < Cell::Rails helper MyAwesomeHelper def display(opts) @shopping_cart = ShoppingCart.for_user(opts[:user]) render end end # app/cells/shopping_cart/display.html.erb <%= do_something_awesome %>github: apotonick/cells
  10. 10. Installation gem ‘cells’ $ rails generate cell shopping_cart display http://github.com/apotonick/cellsgithub: apotonick/cells
  11. 11. Questions ?github: apotonick/cells
  12. 12. Merci !@happynoff

×