Slide 1

Slide 1 text

PATTERNS FALACY! Luciano Sousa http://lucianosousa.net twitter: @lucianosousa github: lucianosousa v2

Slide 2

Slide 2 text

#WHOAMI?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

#War Feelings

Slide 5

Slide 5 text

SOCITRIP.COM

Slide 6

Slide 6 text

DISCLAIMER

Slide 7

Slide 7 text

Focus on ideas, not examples Think about what you are you doing with your apps Try solve your problems out of the box

Slide 8

Slide 8 text

A BRIEF HISTORY OF TIME

Slide 9

Slide 9 text

MVC

Slide 10

Slide 10 text

MVC FRAMEWORK BASED RAILS

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

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

LIVE EXAMPLE

Slide 14

Slide 14 text

‘PATTERNS’

Slide 15

Slide 15 text

Oh, wait... Patterns?

Slide 16

Slide 16 text

like...

Slide 17

Slide 17 text

R.I.P. OBSERVERS

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

Slide 19

Slide 19 text

SCAFFOLD

Slide 20

Slide 20 text

Sloth

Slide 21

Slide 21 text

and... not least

Slide 22

Slide 22 text

CoffeeScript

Slide 23

Slide 23 text

Bad, bad server. No Donut for you

Slide 24

Slide 24 text

http://codon.com/the-dhh-problem

Slide 25

Slide 25 text

RAILS & RAILS 4

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

TestUnit x RSpec

Slide 29

Slide 29 text

RSPEC • pending examples <3 • matchers • custom outputs (?) • describe’s • …

Slide 30

Slide 30 text

Rails ‘Best’ Practices :%s/Rails/Basecamp/g

Slide 31

Slide 31 text

LET’S PRACTICE

Slide 32

Slide 32 text

MODELS

Slide 33

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Rails 4 - Concerns

Slide 37

Slide 37 text

‘Concerned’ people suggest a different name!!!

Slide 38

Slide 38 text

Please

Slide 39

Slide 39 text

Controllers

Slide 40

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

Slide 41

Slide 41 text

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

Slide 42

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

Slide 43

Slide 43 text

class UsersController < ApplicationController ! include UsersSearch ! def index @users = search_users end end

Slide 44

Slide 44 text

VIEWS

Slide 45

Slide 45 text

Avoid if’s

Slide 46

Slide 46 text

<% if @user.citizenship %> <%= some_helper @user.citizenship %> <% else %> <%= content_tag :span, t(‘not_related_citizenship’) %> <% end %>

Slide 47

Slide 47 text

DECORATOR

Slide 48

Slide 48 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 || ‘Sorry. No text here!’, author_name: @user.full_name ) end ! posts_list end

Slide 49

Slide 49 text

Class PostsController < ApplicationController include PostsList ! def index @posts = PostList.new(Post.recent, current_user).build_posts end end

Slide 50

Slide 50 text

<% @posts.each do |post| %> <%= post.title %> <%= post. description %> <%= post.author_name %> <% end %>

Slide 51

Slide 51 text

OH, WAIT!!!!

Slide 52

Slide 52 text

class User < ActiveRecord::Base include PostsExtensions include AvatarsExtensions include PaymentsExtensions include include include include include include include . . . end

Slide 53

Slide 53 text

Editor’s problem

Slide 54

Slide 54 text

module UsersExtensions def users users.map(&:name) end end

Slide 55

Slide 55 text

module UsersExtensions def users users.map(&:name) end end

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Developer’s problem

Slide 58

Slide 58 text

module UsersExtensions def users_name users.map(&:name) end end

Slide 59

Slide 59 text

sum = any_hash.map { |k, v| k + v * 10 }

Slide 60

Slide 60 text

sum = any_hash.map { |discount, price| discount + price * TAX }

Slide 61

Slide 61 text

Bonus

Slide 62

Slide 62 text

Just an example (another terrible one)...

Slide 63

Slide 63 text

require 'spec_helper' ! describe User do it 'testing create user time' do 100.times { user = create(:user) user.should_not be_nil } end end

Slide 64

Slide 64 text

$ time rspec spec/models/user_spec.rb . ! Finished in 6.67 seconds 1 example, 0 failures

Slide 65

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

Slide 66

Slide 66 text

$ time rspec spec/models/user_spec.rb . ! Finished in 0.02927 seconds 1 example, 0 failures

Slide 67

Slide 67 text

RUBY MODULES

Slide 68

Slide 68 text

RAILS DEPENDENCY == 0

Slide 69

Slide 69 text

#WINS

Slide 70

Slide 70 text

FAST RAILS TESTS

Slide 71

Slide 71 text

#WINS

Slide 72

Slide 72 text

COMPOSITION over INHERITANCE

Slide 73

Slide 73 text

#WINS

Slide 74

Slide 74 text

CODE ORGANIZATION

Slide 75

Slide 75 text

#WINS

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

OBRIGADO! Merci! Thanks! Gracias! Cheers Mate! хࢎ