Slide 1

Slide 1 text

Poderosas Interfaces Rails Caike Souza RubyConf BR 2012

Slide 2

Slide 2 text

@caike

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Problemas http://www.flickr.com/photos/lianneviau/499003712/sizes/l/in/photostream/

Slide 8

Slide 8 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 9

Slide 9 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end Client

Slide 10

Slide 10 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end Client Interface

Slide 11

Slide 11 text

Interface

Slide 12

Slide 12 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end Client

Slide 13

Slide 13 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 14

Slide 14 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 15

Slide 15 text

Protocolo

Slide 16

Slide 16 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 17

Slide 17 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment,success: 'You are now subscribed!') else render :index end end end

Slide 18

Slide 18 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment,success: 'You are now subscribed!') else render :index end end end Baixa Granularidade

Slide 19

Slide 19 text

Granularidade

Slide 20

Slide 20 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment,success: 'You are now subscribed!') else render :index end end end

Slide 21

Slide 21 text

Callbacks & Observers if pra if ca if ralho e outras bizarrices

Slide 22

Slide 22 text

As Três Leis das Interfaces

Slide 23

Slide 23 text

Os Três Filtros de Interfaces

Slide 24

Slide 24 text

fazer aquilo que seus métodos indicam fazer. #1

Slide 25

Slide 25 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 26

Slide 26 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 27

Slide 27 text

Payment#save vs. Payment#place

Slide 28

Slide 28 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 29

Slide 29 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.place redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 30

Slide 30 text

class Payment < ActiveRecord::Base protected :save ## # Add your comments here. # Really. ## def place end end

Slide 31

Slide 31 text

deve ser eficiente. #2

Slide 32

Slide 32 text

Usuários Registrados

    <% User.recent.paginate(@page).each do |user| %>
  • <%= link_to(user.full_name, user) %>
  • <% end %>

Slide 33

Slide 33 text

Registered Users

    <% User.recent.paginate(@page).each do |user| %>
  • <%= link_to(user.full_name, user) %>
  • <% end %>

Slide 34

Slide 34 text

Registered Users

    <% @users.each do |user| %>
  • <%= link_to(user.full_name, user) %>
  • <% end %>

Slide 35

Slide 35 text

Usuários Registrados

    <% @users.each do |user| %>
  • <%= link_to(user.full_name, user) %>
  • <% end %>

Slide 36

Slide 36 text

avisar ao client caso haja algum erro. #3

Slide 37

Slide 37 text

raise "Invalid HTTP request"

Slide 38

Slide 38 text

raise "Invalid HTTP request"

Slide 39

Slide 39 text

raise "Invalid HTTP request" vs. raise "Erro na autorização do Cartão de Crédito"

Slide 40

Slide 40 text

raise "Erro na autorização do Cartão de Crédito"

Slide 41

Slide 41 text

raise "Erro na autorização do Cartão de Crédito"

Slide 42

Slide 42 text

fazer aquilo que seus métodos indicam fazer. #1

Slide 43

Slide 43 text

deve ser eficiente. #2

Slide 44

Slide 44 text

avisar ao client caso haja algum erro. #3

Slide 45

Slide 45 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 46

Slide 46 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.save redirect_to(payment,success: 'You are now subscribed!') else render :index end end end

Slide 47

Slide 47 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.place redirect_to(payment,success: 'You are now subscribed!') else render :index end end end

Slide 48

Slide 48 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.place redirect_to(payment,success: 'You are now subscribed!') else render :index end end end fazendo muita coisa

Slide 49

Slide 49 text

Factory Method

Slide 50

Slide 50 text

class PaymentsController < ApplicationController def create payment = Payment.new(params[:payment]) if payment.place redirect_to(payment,success: 'You are now subscribed!') else render :index end end end

Slide 51

Slide 51 text

class PaymentsController < ApplicationController def create payment = Payment.build_with(params[:payment]) if payment.place redirect_to(payment,success: 'You are now subscribed!') else render :index end end end

Slide 52

Slide 52 text

class PaymentsController < ApplicationController def create payment = Payment.build_with(params[:payment]) if payment.place redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 53

Slide 53 text

class PaymentsController < ApplicationController def create payment = Payment.build_with(params[:payment]) if payment.place redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 54

Slide 54 text

class PaymentsController < ApplicationController def create payment = Payment.build_with(params[:payment]) if payment.place redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 55

Slide 55 text

class PlanSubscriber def self.build_for(user) end def subscribe(payment_info) end end

Slide 56

Slide 56 text

class PlanSubscriber def self.build_for(user) end def subscribe(payment_info) end end

Slide 57

Slide 57 text

require 'plan_subscriber' describe PlanSubscriber do end

Slide 58

Slide 58 text

class PaymentsController < ApplicationController def create payment = Payment.build_with(params[:payment]) if payment.place redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 59

Slide 59 text

class PaymentsController < ApplicationController def create payment = Payment.build_with(params[:payment]) if payment.place redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 60

Slide 60 text

class PaymentsController < ApplicationController def create plan_subscriber = PlanSubscriber.build_for(current_user) if plan_subscriber.subscribe(params[:payment]) payment = plan_subscriber.payment redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 61

Slide 61 text

Models != AR::Base

Slide 62

Slide 62 text

Rails não é o seu domain http://blog.firsthand.ca/2011/10/rails-is-not-your- application.html

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Design by Contract

Slide 65

Slide 65 text

Pre-Conditions Post-Conditions Invariants

Slide 66

Slide 66 text

Pre-Conditions

Slide 67

Slide 67 text

E-mail deve ser válido. Produto deve estar disponível.

Slide 68

Slide 68 text

Pre-Conditions Post-Conditions

Slide 69

Slide 69 text

Assento deve estar reservado. Novo pagamento deve ser criado.

Slide 70

Slide 70 text

class PaymentsController < ApplicationController def create plan_subscriber = PlanSubscriber.build_for(current_user) if plan_subscriber.subscribe(params[:payment]) payment = plan_subscriber.payment redirect_to(payment, success:'You are now subscribed!') else render :index end end end

Slide 71

Slide 71 text

Pre-Conditions Post-Conditions Invariants

Slide 72

Slide 72 text

Usuário deve estar ativado. Loja deve estar aberta.

Slide 73

Slide 73 text

Pre-Conditions Post-Conditions Invariants

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Pirâmide de complexidade de models

Slide 76

Slide 76 text

0 1 2 Pirâmide de complexidade de models

Slide 77

Slide 77 text

0 Rails out-of-the-box 1 2 Pirâmide de complexidade de models

Slide 78

Slide 78 text

0 Rails out-of-the-box 1 Não utilizar métodos AR::Base nos controllers 2 Pirâmide de complexidade de models

Slide 79

Slide 79 text

0 2 Rails out-of-the-box Não utilizar classes AR::Base nos controllers 1 Não utilizar métodos AR::Base nos controllers Pirâmide de complexidade de models

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

"Bons sistemas orientados a objeto focam em como as suas diferentes partes se comunicam, e não nas propriedades internas e comportamento dessas partes." Alan Kay

Slide 82

Slide 82 text

Mensagens

Slide 83

Slide 83 text

Mensagens

Slide 84

Slide 84 text

Protocolo

Slide 85

Slide 85 text

Os Três Filtros de Interfaces

Slide 86

Slide 86 text

Design by Contract

Slide 87

Slide 87 text

Obrigado :)

Slide 88

Slide 88 text

Poderosas Interfaces Rails Caike Souza RubyConf BR 2012