Slide 1

Slide 1 text

THE PATTERNS FALACY! Luciano Sousa http://lucianosousa.net twitter: @lucianosousa github: lucianosousa Rails Version <3 Monday, May 27, 13

Slide 2

Slide 2 text

#WHOAMI? Monday, May 27, 13

Slide 3

Slide 3 text

Monday, May 27, 13

Slide 4

Slide 4 text

#War Feelings Monday, May 27, 13

Slide 5

Slide 5 text

Focus on ideas, not examples Thing about what you are you doing with your apps Try solve your problems out of the box Monday, May 27, 13

Slide 6

Slide 6 text

A BRIEF HISTORY OF TIME Monday, May 27, 13

Slide 7

Slide 7 text

Monday, May 27, 13

Slide 8

Slide 8 text

MVC Monday, May 27, 13

Slide 9

Slide 9 text

RAILS Monday, May 27, 13

Slide 10

Slide 10 text

MVC FRAMEWORK BASED RAILS Monday, May 27, 13

Slide 11

Slide 11 text

MVC Definition Model–view–controller (MVC) is a software architecture pattern which separates the representation of information from the user's interaction with it. (...) • A controller can send commands to its associated view to change the view's presentation of the model (e.g., by scrolling through a document). It can also send commands to the model to update the model's state (e.g., editing a document). • A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands (...). • A view requests from the model the information that it needs to generate an output representation to the user. http://en.wikipedia.org/wiki/Model-view-controller Monday, May 27, 13

Slide 12

Slide 12 text

Monday, May 27, 13

Slide 13

Slide 13 text

LIVE EXAMPLE Monday, May 27, 13

Slide 14

Slide 14 text

‘PATTERNS’ Monday, May 27, 13

Slide 15

Slide 15 text

Oh, wait... Patterns? Monday, May 27, 13

Slide 16

Slide 16 text

like... Monday, May 27, 13

Slide 17

Slide 17 text

OBSERVERS??? Monday, May 27, 13

Slide 18

Slide 18 text

https://blog.engineyard.com/2013/rails-4-changes http://ruby5.envylabs.com/episodes/331-episode-327- december-4-2012/stories/2882-observers-and-sweepers-were- removed-from-rails http://samuelmullen.com/2013/05/the-problem-with-rails-callbacks/ Observers Monday, May 27, 13

Slide 19

Slide 19 text

SCAFFOLD Monday, May 27, 13

Slide 20

Slide 20 text

Sloth Monday, May 27, 13

Slide 21

Slide 21 text

and... not least Monday, May 27, 13

Slide 22

Slide 22 text

CoffeeScript Monday, May 27, 13

Slide 23

Slide 23 text

Bad, bad server. No Donut for you Monday, May 27, 13

Slide 24

Slide 24 text

TestUnit x RSpec Monday, May 27, 13

Slide 25

Slide 25 text

Bad, bad server. No Donut for you Monday, May 27, 13

Slide 26

Slide 26 text

Rails ‘Best’ Practices :%s/Rails/Basecamp/g Monday, May 27, 13

Slide 27

Slide 27 text

LET’S PRACTICE Monday, May 27, 13

Slide 28

Slide 28 text

MODELS Monday, May 27, 13

Slide 29

Slide 29 text

class User < ActiveRecord::Base has_many :posts def post_titles posts.map(&:title).join(', ') end end class Post < ActiveRecord::Base belongs_to :user end Monday, May 27, 13

Slide 30

Slide 30 text

config/application.rb class Application < Rails::Application config.autoload_paths += %W(#{config.root}/app/ extensions/) end Monday, May 27, 13

Slide 31

Slide 31 text

module PostsExtensions def post_titles names = posts.map(&:title) names.join(‘, ‘) end end class User < ActiveRecord::Base include PostsExtensions has_many :posts end Monday, May 27, 13

Slide 32

Slide 32 text

Rails 4 - Concerns Monday, May 27, 13

Slide 33

Slide 33 text

‘Concerned’ people prefer a different name!!! Monday, May 27, 13

Slide 34

Slide 34 text

Please Monday, May 27, 13

Slide 35

Slide 35 text

Controllers Monday, May 27, 13

Slide 36

Slide 36 text

class UsersController < ApplicationController def index @users = search_users end private def search_users filters = [] unless params[:name].blank? filters << [ “name = ?”, params[:name] end unless params.[:last_name].blank? filters << [ “last_name = ?”, params[: last_name] end . . . User.where(filters) end end Monday, May 27, 13

Slide 37

Slide 37 text

config/application.rb class Application < Rails::Application config.autoload_paths += %W(#{config.root}/app/extensions/ #{config.root}/lib/ ) end Monday, May 27, 13

Slide 38

Slide 38 text

module UsersSearch def search_users filters = [] unless params[:name].blank? filters << [ “name = ?”, params[:name] end unless params.[:last_name].blank? filters << [ “last_name = ?”, params[: last_name] end . . . User.where(filters) end end Monday, May 27, 13

Slide 39

Slide 39 text

class UsersController < ApplicationController include UsersSearch def index @users = search_users end end Monday, May 27, 13

Slide 40

Slide 40 text

VIEWS Monday, May 27, 13

Slide 41

Slide 41 text

Avoid if’s Monday, May 27, 13

Slide 42

Slide 42 text

DECORATOR Monday, May 27, 13

Slide 43

Slide 43 text

class PostsList def initialize(posts, user) @posts = posts @user = user end def build_posts posts_list = [] @posts do |post| posts_list << OpenStruct.new( title: post.title, description: post.description ? post.description : ‘Sorry. No text here!’, author_name: @user.full_name ) end posts_list end Monday, May 27, 13

Slide 44

Slide 44 text

<% @posts.each do |post| %> <%= post.title %> <%= post. description %> <%= post.author_name %> <% end %> Monday, May 27, 13

Slide 45

Slide 45 text

Monday, May 27, 13

Slide 46

Slide 46 text

OH, WAIT!!!! Monday, May 27, 13

Slide 47

Slide 47 text

class User < ActiveRecord::Base include PostsExtensions include AvatarsExtensions include PaymentsExtensions include include include include include include include . . . end Monday, May 27, 13

Slide 48

Slide 48 text

Editor’s problem Monday, May 27, 13

Slide 49

Slide 49 text

Monday, May 27, 13

Slide 50

Slide 50 text

module UsersExtensions def users users.map(&:name) end end Monday, May 27, 13

Slide 51

Slide 51 text

module UsersExtensions def users users.map(&:name) end end Monday, May 27, 13

Slide 52

Slide 52 text

class Registrations < ActiveRecord::Base include UsersExtensions has_many :users end > r = Registration.first > r.users => [ ‘you’, ‘are dumb’] Monday, May 27, 13

Slide 53

Slide 53 text

Developer’s problem Monday, May 27, 13

Slide 54

Slide 54 text

Monday, May 27, 13

Slide 55

Slide 55 text

module UsersExtensions def users_name users.map(&:name) end end Monday, May 27, 13

Slide 56

Slide 56 text

Monday, May 27, 13

Slide 57

Slide 57 text

sum = any_hash.map { |k, v| k + v * 10 } Monday, May 27, 13

Slide 58

Slide 58 text

sum = any_hash.map { |discount, price| discount + price * TAX } Monday, May 27, 13

Slide 59

Slide 59 text

Monday, May 27, 13

Slide 60

Slide 60 text

Bonus Monday, May 27, 13

Slide 61

Slide 61 text

Just an example (another terrible one)... Monday, May 27, 13

Slide 62

Slide 62 text

require 'spec_helper' describe User do it 'testing create user time' do 100.times { user = FactoryGirl.create(:user) user.should_not be_nil } end end Monday, May 27, 13

Slide 63

Slide 63 text

$ time rspec spec/models/user_spec.rb . Finished in 6.67 seconds 1 example, 0 failures Monday, May 27, 13

Slide 64

Slide 64 text

require 'spec_helper' describe User do it 'testing create user time' do 100.times { user = double(“User”) user.should_not be_nil } end end Monday, May 27, 13

Slide 65

Slide 65 text

$ time rspec spec/models/user_spec.rb . Finished in 0.02927 seconds 1 example, 0 failures Monday, May 27, 13

Slide 66

Slide 66 text

RUBY MODULES Monday, May 27, 13

Slide 67

Slide 67 text

RAILS DEPENDENCY == 0 Monday, May 27, 13

Slide 68

Slide 68 text

#WINS Monday, May 27, 13

Slide 69

Slide 69 text

FAST RAILS TESTS Monday, May 27, 13

Slide 70

Slide 70 text

#WINS Monday, May 27, 13

Slide 71

Slide 71 text

COMPOSITION vs INHERITANCE Monday, May 27, 13

Slide 72

Slide 72 text

#WINS Monday, May 27, 13

Slide 73

Slide 73 text

CODE ORGANIZATION Monday, May 27, 13

Slide 74

Slide 74 text

#WINS Monday, May 27, 13

Slide 75

Slide 75 text

Monday, May 27, 13

Slide 76

Slide 76 text

OBRIGADO! Merci! Thanks! Gracias! Cheers Mate! хࢎ Monday, May 27, 13