Date post: | 21-Dec-2014 |
Category: |
Technology |
Upload: | simon-courtois |
View: | 665 times |
Download: | 4 times |
CellsSimon Courtois - @happynoff
github: apotonick/cells
Problématique
Comment partager un widget entre les pages du site ?
github: apotonick/cells
Solution à l’ancienne
# app/controllers/application_controller.rbclass ApplicationController < ActionController::Base before_filter :set_shopping_cart
private
def set_shopping_cart @shopping_cart = ShoppingCart.for_user(current_user) endend
# 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
Solution avec Cells
# app/cells/shopping_cart_cell.rbclass ShoppingCartCell < Cell::Rails def display(opts) @shopping_cart = ShoppingCart.for_user(opts[:user]) render endend
# app/views/layouts/application.html.erb<%= render_cell :shopping_cart, :display, user: current_user %>
# app/cells/shopping_cart/display.html.erb<% if @shopping_cart %> # shopping cart rendering<% end %>
github: apotonick/cells
Avantages
Séparation des responsabilités
Meilleure organisation du codeapp/ cells/ shopping_cart/ display.html.erb shopping_cart.rb
github: apotonick/cells
Aller plus loin - actions
# app/cells/shopping_cart_cell.rbclass 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 endend
# 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
Aller plus loin - cache
# app/cells/shopping_cart_cell.rbclass ShoppingCartCell < Cell::Rails
cache :display, expires_in: 10.minutes
def display(opts) # ... endend
github: apotonick/cells
Aller plus loin - cache
cache :display do |cell, opts| opts[:user] end
# app/cells/shopping_cart_cell.rbclass ShoppingCartCell < Cell::Rails
def display(opts) # ... endend
github: apotonick/cells
Aller plus loin - helpers
# app/cells/shopping_cart_cell.rbclass ShoppingCartCell < Cell::Rails helper MyAwesomeHelper
def display(opts) @shopping_cart = ShoppingCart.for_user(opts[:user]) render endend
# app/cells/shopping_cart/display.html.erb<%= do_something_awesome %>
github: apotonick/cells
Installation
gem ‘cells’
$ rails generate cell shopping_cart display
http://github.com/apotonick/cells
github: apotonick/cells
Questions ?
Merci !@happynoff