of changes to its internal structure to make it easier to understand and cheaper to modify without changing its observable behavior. Friday, August 24, 12
3 cities.each do |city| 4 if city.name == "New York" 5 amazing_cities << city 6 elsif city.rating > 5 7 amazing_cities << city 8 end 9 end 10 end 1 def self.amazing_cities(cities) 2 cities.select do |city| 3 city.name == "New York" || city.rating > 5 4 end 3 end 1 describe "amazing_cities" do 2 it "includes only cities named New York or with a rating >5" do 3 portland = City.new(name: "Portland", rating: 5) 4 nyc = City.new(name: "New York") 5 boston = City.new(name: "Boston", rating: 6) 6 amazing_cities(portland, nyc, boston).should =~ [nyc, boston] 7 end 8 end Friday, August 24, 12
• After a working commit • When you need to understand something • The Rule of Three • When you feel pain • slow feature development • frequent bugs • When you encounter a code smell Friday, August 24, 12
Service::Mailchimp.subscribe(email, USER_LIST_ID) 4 end 5 end 1 class Host < ActiveRecord::Base 2 def subscribe! 3 Service::Mailchimp.subscribe(primary_email, HOST_LIST_ID) 4 end 5 end 1 class MailingList < ActiveRecord::Base 2 def subscribe!(user) 3 Service::Mailchimp.subscribe(user.email, USER_LIST_ID) 4 end 5 end Friday, August 24, 12
include Alphabetizable 3 end 4 5 class City < ActiveRecord::Base 6 include Alphabetizable 7 end 8 9 class Movie < ActiveRecord::Base 10 scope :alphabetical, order(:title) 11 end 12 13 module Alphabetizable 14 def self.included(base) 15 base.class_eval do 16 scope :alphabetical, order(:name) 17 end 18 end 19 end 1 class User < ActiveRecord::Base 2 scope :alphabetical, order(:name) 3 end 4 5 class City < ActiveRecord::Base 6 scope :alphabetical, order(:name) 7 end 8 9 class Movie < ActiveRecord::Base 10 scope :alphabetical, order(:title) 11 end Friday, August 24, 12
:events, :tickets].each do |association| 3 class_eval do 4 has_many association 5 end 6 end 7 end 1 class User 2 has_many :accounts 3 has_many :orders 4 has_many :friends 5 has_many :credits 6 has_many :events 7 has_many :tickets 8 end Friday, August 24, 12