Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Powerful Interfaces

Powerful Interfaces

We explore the best practices in using interfaces as the foundation for designing object oriented applications in Ruby and Rails. We will talk about some of the techniques that make it possible to write loosely coupled components, run faster tests and have a more enjoyable programming experience.

Carlos Souza

April 26, 2012
Tweet

More Decks by Carlos Souza

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. class Payment < ActiveRecord::Base protected :save ## # Add your

    comments here. # Really. Please do it :) ## def place end end
  11. <h2>Registered Users</h2> <div class="users"> <ul> <% User.recent.paginate(@page).each do |user| %>

    <li><%= link_to(user.full_name, user) %></li> <% end %> </ul> </div>
  12. <h2>Registered Users</h2> <div class="users"> <ul> <% User.recent.paginate(@page).each do |user| %>

    <li><%= link_to(user.full_name, user) %></li> <% end %> </ul> </div>
  13. <h2>Registered Users</h2> <div class="users"> <ul> <% @users.each do |user| %>

    <li><%= link_to(user.full_name, user) %></li> <% end %> </ul> </div>
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 0 2 Rails out-of-the-box Hide AR::Base classes from controller 1

    Protect AR::Base methods from controller Rails Models Complexity Pyramid