Slide 1

Slide 1 text

Powerful Interfaces @caike RailsConf 2012

Slide 2

Slide 2 text

why developing Rails apps with fine-grained components is freaking awesome!

Slide 3

Slide 3 text

Granularity

Slide 4

Slide 4 text

Problems

Slide 5

Slide 5 text

Not Fun! http://www.flickr.com/photos/lianneviau/499003712/sizes/l/in/photostream/

Slide 6

Slide 6 text

Interface

Slide 7

Slide 7 text

class Payment < ActiveRecord::Base def complete end def cancel end end

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

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

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 Coarse-Grained

Slide 14

Slide 14 text

Callbacks Observers

Slide 15

Slide 15 text

Three Laws of Interfaces

Slide 16

Slide 16 text

do what its methods say it does. #1

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

Payment#save vs. Payment#place

Slide 19

Slide 19 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 20

Slide 20 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 21

Slide 21 text

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

Slide 22

Slide 22 text

do no harm. #2

Slide 23

Slide 23 text

Registered Users

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

Slide 24

Slide 24 text

Registered Users

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

Slide 25

Slide 25 text

Registered Users

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

Slide 26

Slide 26 text

notify client if something goes wrong. #3

Slide 27

Slide 27 text

raise "Invalid HTTP request"

Slide 28

Slide 28 text

raise "Invalid HTTP request"

Slide 29

Slide 29 text

raise "Invalid HTTP request" vs. raise "PayX Authentication Error"

Slide 30

Slide 30 text

raise "PayX Authentication Error"

Slide 31

Slide 31 text

raise "PayX Authentication Error"

Slide 32

Slide 32 text

do what its methods say it does. #1

Slide 33

Slide 33 text

do no harm. #2

Slide 34

Slide 34 text

notify client if something goes wrong. #3

Slide 35

Slide 35 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 36

Slide 36 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 37

Slide 37 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 38

Slide 38 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 doing too much

Slide 39

Slide 39 text

Factory Method Pattern

Slide 40

Slide 40 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 41

Slide 41 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 42

Slide 42 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 43

Slide 43 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 44

Slide 44 text

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

Slide 45

Slide 45 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 46

Slide 46 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 47

Slide 47 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 48

Slide 48 text

Design by Contract

Slide 49

Slide 49 text

Pre-Conditions Post-Conditions Invariants

Slide 50

Slide 50 text

Pre-Conditions

Slide 51

Slide 51 text

User must have a valid email address. Product must be available.

Slide 52

Slide 52 text

Pre-Conditions Post-Conditions

Slide 53

Slide 53 text

User must have a new payment. Seat must be reserved.

Slide 54

Slide 54 text

Pre-Conditions Post-Conditions Invariants

Slide 55

Slide 55 text

User must be registered. Store location must be open.

Slide 56

Slide 56 text

Pre-Conditions Post-Conditions Invariants

Slide 57

Slide 57 text

Fat models Thin controllers

Slide 58

Slide 58 text

Models != AR::Base

Slide 59

Slide 59 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 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

Rails is not your application http://blog.firsthand.ca/2011/10/rails-is-not-your- application.html

Slide 62

Slide 62 text

Extract your application into its own ruby gem.

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

http://objectsonrails.com

Slide 65

Slide 65 text

Rails Models Complexity Pyramid

Slide 66

Slide 66 text

0 1 2 Rails Models Complexity Pyramid

Slide 67

Slide 67 text

0 Rails out-of-the-box 1 2 Rails Models Complexity Pyramid

Slide 68

Slide 68 text

0 Rails out-of-the-box 1 Protect AR::Base methods from controller 2 Rails Models Complexity Pyramid

Slide 69

Slide 69 text

0 2 Rails out-of-the-box Hide AR::Base classes from controller 1 Protect AR::Base methods from controller Rails Models Complexity Pyramid

Slide 70

Slide 70 text

Thank you. @caike