+ All Categories

Cells

Date post: 21-Dec-2014
Category:
Upload: simon-courtois
View: 665 times
Download: 4 times
Share this document with a friend
Description:
Slides of the talk I gave at Pastis.rb on 14/11/2012.
Popular Tags:
12
C S Cr - @pp
Transcript
Page 1: Cells

CellsSimon Courtois - @happynoff

Page 2: Cells

github: apotonick/cells

Problématique

Comment partager un widget entre les pages du site ?

Page 3: Cells

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

Page 4: Cells

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

Page 5: Cells

github: apotonick/cells

Avantages

Séparation des responsabilités

Meilleure organisation du codeapp/ cells/ shopping_cart/ display.html.erb shopping_cart.rb

Page 6: Cells

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

Page 7: Cells

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

Page 8: Cells

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

Page 9: Cells

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

Page 10: Cells

github: apotonick/cells

Installation

gem ‘cells’

$ rails generate cell shopping_cart display

http://github.com/apotonick/cells

Page 11: Cells

github: apotonick/cells

Questions ?

Page 12: Cells

Merci !@happynoff


Recommended