1. Fluent Refactoring
Sam Livingston-Gray
THERE
WILL BE
CODE!
It may be
this small
puts (1..100).map { |i|
s = ''
fizz = (i % 3).zero?
buzz = (i % 5).zero?
s << 'Fizz' if fizz
s << 'Buzz' if buzz
s = i if s.empty?
s
}
1
Tuesday, October 22, 13
21. Re·fac·tor·ing (noun)
"...a disciplined technique for
restructuring an existing body of code,
altering its internal structure without
changing its external behavior."
-refactoring.com
18
Tuesday, October 22, 13
23. TL;DR
"...a disciplined technique for
restructuring an existing body of
code, altering its internal structure
without changing its external
behavior."
19
Tuesday, October 22, 13
35. Levels of Proficiency
Level 1
Level 2
Level 3
Level 4
28
Tuesday, October 22, 13
http://www.jamesshore.com/Blog/Proficiencies-of-Planning.html
36. Levels of Proficiency
Level 1
Tarzan at
a party
“Beer!”
“Good party.”
Level 2
Level 3
Level 4
29
Tuesday, October 22, 13
http://www.jamesshore.com/Blog/Proficiencies-of-Planning.html
37. Levels of Proficiency
Level 1
Tarzan at
a party
“Beer!”
“Good party.”
Level 2
Going to
the party
"Where is the party?"
"How do I get to the party?"
Level 3
Level 4
30
Tuesday, October 22, 13
http://www.jamesshore.com/Blog/Proficiencies-of-Planning.html
38. Levels of Proficiency
Level 1
Tarzan at
a party
“Beer!”
“Good party.”
Level 2
Going to
the party
"Where is the party?"
"How do I get to the party?"
Level 3
Discussing
the party
"What happened at the party
last night?"
Level 4
31
Tuesday, October 22, 13
http://www.jamesshore.com/Blog/Proficiencies-of-Planning.html
39. Levels of Proficiency
Level 1
Tarzan at
a party
“Beer!”
“Good party.”
Level 2
Going to
the party
"Where is the party?"
"How do I get to the party?"
Level 3
Discussing
the party
"What happened at the party
last night?"
Level 4
Charlie Rose
"Should parties be illegal?"
32
Tuesday, October 22, 13
http://www.jamesshore.com/Blog/Proficiencies-of-Planning.html
40. Levels of Proficiency
Level 1
Tarzan at
a party
“Beer!”
“Good party.”
Level 2
Going to
the party
"Where is the party?"
"How do I get to the party?"
Level 3
Discussing
the party
"What happened at the party
last night?"
Level 4
Charlie Rose
"Should parties be illegal?"
33
Tuesday, October 22, 13
http://www.jamesshore.com/Blog/Proficiencies-of-Planning.html
41. Levels of Proficiency
Level 1
Tarzan at
a party
“Beer!”
“Good party.”
Level 2
Going to
the party
"Where is the party?"
"How do I get to the party?"
34
Tuesday, October 22, 13
http://www.jamesshore.com/Blog/Proficiencies-of-Planning.html
48. class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
# lots more stuff...
end
Tuesday, October 22, 13
38
49. Observations
class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
# lots more stuff...
end
Tuesday, October 22, 13
39
50. Observations
class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
~800 lines in file
# lots more stuff...
end
Tuesday, October 22, 13
39
51. Observations
class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
~800 lines in file
~50 lines in method
# lots more stuff...
end
Tuesday, October 22, 13
39
52. Observations
class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
~800 lines in file
~50 lines in method
Longest line: 177 chars
# lots more stuff...
end
Tuesday, October 22, 13
39
53. Observations
class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
~800 lines in file
~50 lines in method
Longest line: 177 chars
Indentation: 4-16 spaces
# lots more stuff...
end
Tuesday, October 22, 13
39
54. Observations
class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
~800 lines in file
~50 lines in method
Longest line: 177 chars
Indentation: 4-16 spaces
Nested control
structures:
# lots more stuff...
end
Tuesday, October 22, 13
39
55. Observations
class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
~800 lines in file
~50 lines in method
Longest line: 177 chars
Indentation: 4-16 spaces
Nested control
structures:
audit_trail_for
# lots more stuff...
end
Tuesday, October 22, 13
39
56. Observations
class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
~800 lines in file
~50 lines in method
Longest line: 177 chars
Indentation: 4-16 spaces
Nested control
structures:
audit_trail_for
begin/rescue/end
# lots more stuff...
end
Tuesday, October 22, 13
39
57. Observations
class InstallationsController < ActionController::Base
# lots more stuff...
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
~800 lines in file
~50 lines in method
Longest line: 177 chars
Indentation: 4-16 spaces
Nested control
structures:
audit_trail_for
begin/rescue/end
if/else/end
# lots more stuff...
end
Tuesday, October 22, 13
39
86. if request.xhr?
if request.xhr?
begin
if @installation.pend
if @installation.pending_credit_check? #..
render :json =>
render :json => #... return
return
end
end
begin
#...
#...
end
end
else
else
if @installation.pending_credit_check?
if @installation.pend
flash[:error] = #...
flash[:error] = #..
redirect_to installations_path(:city_id =>
redirect_to install
return
return
end
end
begin
begin
#...
#...
end
end
60
Tuesday, October 22, 13
87. if request.xhr?
if @installation.pending_credit_check?
render :json => #...
return
end
begin
#...
end
else
if @installation.pending_credit_check?
flash[:error] = #...
redirect_to installations_path(:city_id =>
return
end
begin
#...
end
61
Tuesday, October 22, 13
88. if request.xhr?
if request.xhr?
if @installation.pending_credit_check?
if @installation.pend
render :json => #...
render :json => #..
return
return
end
end
begin
else
#...
if @installation.pend
end
flash[:error] = #..
else
redirect_to #...
if @installation.pending_credit_check?
return
flash[:error] = #... end
redirect_to installations_path(:city_id =>
end
return
end
if request.xhr?
begin
begin
#...
#...
end
end
61
Tuesday, October 22, 13
96. if ajax
if nope
render :json => #...
return
end
else
if nope
flash[:error] = #...
redirect_to #...
return
end
end
67
Tuesday, October 22, 13
97. if ajax
if nope
render :json => #...
return
end
else
if nope
flash[:error] = #...
redirect_to #...
return
end
end
67
Tuesday, October 22, 13
ajax
nope
TRUE
TRUE
ajax
nope
FALSE
TRUE
98. if ajax
if nope
render :json => #...
return
end
else
if nope
flash[:error] = #...
redirect_to #...
return
end
end
68
Tuesday, October 22, 13
99. if ajax
if ajax
if nope
if nope
render :json => #...
render :json => #..
return
return
end
end
else
end
if nope
if not ajax
flash[:error] = #... if nope
redirect_to #...
flash[:error] = #..
return
redirect_to #...
end
return
end
end
end
68
Tuesday, October 22, 13
100. if ajax
if nope
render :json => #...
return
end
end
if not ajax
if nope
flash[:error] = #...
redirect_to #...
return
end
end
69
Tuesday, October 22, 13
101. if ajax
if ajax && nope
if nope
render :json => #...
render :json => #... return
return
end
end
if (not ajax) && nope
end
flash[:error] = #...
if not ajax
redirect_to #...
if nope
return
flash[:error] = #...end
redirect_to #...
return
end
end
69
Tuesday, October 22, 13
102. if ajax && nope
render :json => #...
return
end
if (not ajax) && nope
flash[:error] = #...
redirect_to #...
return
end
70
Tuesday, October 22, 13
103. if ajax && nope
render :json => #...
return
end
if (not ajax) && nope
flash[:error] = #...
redirect_to #...
return
end
70
Tuesday, October 22, 13
104. if ajax && nope
render :json => #...
return
end
if (not ajax) && nope
flash[:error] = #...
redirect_to #...
return
end
if nope
if ajax
render :json => #..
return
end
if not ajax
flash[:error] = #..
redirect_to #...
return
end
end
70
Tuesday, October 22, 13
105. if nope
if ajax
render :json => #...
return
end
if not ajax
flash[:error] = #...
redirect_to #...
return
end
end
71
Tuesday, October 22, 13
106. if nope
if nope
if ajax
if ajax
render :json => #...
render :json => #..
return
return
end
else
if not ajax
flash[:error] = #..
flash[:error] = #...
redirect_to #...
redirect_to #...
return
return
end
end
end
end
71
Tuesday, October 22, 13
107. if nope
if ajax
render :json => #...
return
else
flash[:error] = #...
redirect_to #...
return
end
end
72
Tuesday, October 22, 13
108. if nope
if nope
if ajax
if ajax
render :json => #...
render :json => #..
return
else
else
flash[:error] = #..
flash[:error] = #...
redirect_to #...
redirect_to #...
end
return
return
end
end
end
72
Tuesday, October 22, 13
110. if request.xhr?
if @installation.pendin
if @installation.pending_credit_check?
if request.xhr?
render :json => #...
render :json => #..
return
else
end
flash[:error] = #..
else
redirect_to #...
if @installation.pending_credit_check?
end
flash[:error] = #... return
redirect_to #...
end
return
end
end
73
Tuesday, October 22, 13
115. class InstallationsController < ActionController::Base
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
end
77
Tuesday, October 22, 13
116. class InstallationsController < ActionController::Base
def schedule
desired_date = params[:desired_date]
if request.xhr?
begin
if @installation.pending_credit_check?
render :json => {:errors => ["Cannot schedule installation while credit check is pending"]}, :status => 400
return
end
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
date = @installation.scheduled_date.in_time_zone(@installation.city.timezone).to_date
render :json => {:errors => nil, :html => schedule_response(@installation, date)}
end
else
render :json => {:errors => [%Q{Could not update installation. #{@installation.errors.full_messages.join(' ')}}] }
end
end
rescue ActiveRecord::RecordInvalid => e
render :json => {:errors => [e.message] }
rescue ArgumentError => e
render :json => {:errors => ["Could not schedule installation. Start by making sure the desired date is on a business day."]}
end
else
if @installation.pending_credit_check?
flash[:error] = "Cannot schedule installation while credit check is pending"
redirect_to installations_path(:city_id => @installation.city_id, :view => "calendar") and return
end
begin
audit_trail_for(current_user) do
if @installation.schedule!(desired_date, :installation_type => params[:installation_type], :city => @city)
if @installation.scheduled_date
if @installation.customer_provided_equipment?
flash[:success] = %Q{Installation scheduled}
else
flash[:success] = %Q{Installation scheduled! Don't forget to order the equipment also.}
end
end
else
flash[:error] = %Q{Could not schedule installation, check the phase of the moon}
end
end
rescue => e
flash[:error] = e.message
end
redirect_to(@installation.customer_provided_equipment? ? customer_provided_installations_path
: installations_path(:city_id => @installation.city_id, :view => "calendar"))
end
end
end
class ScheduleInstallation
def call
if @installation.pending_c
cant_schedule_while_cred
return
end
77
Tuesday, October 22, 13
audit_trail_for(current_us
if schedule!
if @installation.sched
scheduling_succeeded
end
do_post_success_cleanu
else
scheduling_failed
end
end
rescue => e
handle_exception e
end
end
117. class ScheduleInstallation
def call
if @installation.pending_credit_check?
cant_schedule_while_credit_check_pending
return
end
audit_trail_for(current_user) do
if schedule!
if @installation.scheduled_date
scheduling_succeeded
end
do_post_success_cleanup
else
scheduling_failed
end
end
rescue => e
handle_exception e
end
end
78
Tuesday, October 22, 13
118. class ScheduleInstallation
def call
if @installation.pending_credit_check?
cant_schedule_while_credit_check_pending
return
end
audit_trail_for(current_user) do
if schedule!
if @installation.scheduled_date
scheduling_succeeded
end
do_post_success_cleanup
else
scheduling_failed
end
end
rescue => e
handle_exception e
end
end
request.xhr?
79
Tuesday, October 22, 13
119. class ScheduleInstallation
def call
if @installation.pending_credit_check?
cant_schedule_while_credit_check_pending
return
end
audit_trail_for(current_user) do
if schedule!
if @installation.scheduled_date
scheduling_succeeded
end
do_post_success_cleanup
else
scheduling_failed
end
end
rescue => e
handle_exception e
end
end
request.xhr?
79
Tuesday, October 22, 13
121. def cannot_schedule_while_#...
if request.xhr?
# ...1 line...
else
# ...2 lines...
end
end
def scheduling_failed
if request.xhr?
# ...1 line...
else
# ...2 lines...
end
end
def handle_exception(e)
if request.xhr?
# ...7 lines...
else
# ...2 lines...
end
end
def do_post_success_cleanup
if request.xhr?
# DO NOTHING
else
# ...1 line...
end
end
def scheduling_succeeded
if request.xhr?
# ...2 lines...
else
# ...5 lines...
end
end
81
Tuesday, October 22, 13
122. def cannot_schedule_while_#...
if request.xhr?
# ...1 line...
else
# ...2 lines...
end
end
def scheduling_failed
if request.xhr?
# ...1 line...
else
# ...2 lines...
end
end
def handle_exception(e)
if request.xhr?
# ...7 lines...
else
# ...2 lines...
end
end
def do_post_success_cleanup
if request.xhr?
# DO NOTHING
else
# ...1 line...
end
end
def scheduling_succeeded
if request.xhr?
# ...2 lines...
else
# ...5 lines...
end
end
81
Tuesday, October 22, 13
141. class ScheduleInstallation
class ScheduleInstallat
def call
def call
end
private
class Responder
def cannot_schedule_while_credit_check_pendin
def cannot_schedule_w
def handle_exception(e) def handle_exception(
def scheduling_failed
def scheduling_failed
def scheduling_succeededdef scheduling_succee
def do_post_success_cleanup do_post_success_c
def
end
end
class Responder
end
90
Tuesday, October 22, 13
150. class AJAXResponder
def scheduling_failed
if request.xhr?
render :json => #...
else
flash[:error] = #...
redirect_to #...
end
end
end
class HTMLResponder
def scheduling_failed
if request.xhr?
render :json => #...
else
flash[:error] = #...
redirect_to #...
end
end
end
Tuesday, October 22, 13
151. class AJAXResponder
def scheduling_failed
if request.xhr?
render :json => #...
else
flash[:error] = #...
redirect_to #...
end
end
end
class HTMLResponder
def scheduling_failed
if request.xhr?
render :json => #...
else
flash[:error] = #...
redirect_to #...
end
end
end
Tuesday, October 22, 13
class AJAXResponder
def scheduling_failed
render :json => #...
end
end
class HTMLResponder
def scheduling_failed
flash[:error] = #...
redirect_to #...
end
end
162. Speed Up Your Tests!
See also:
Katrina Owen,
“Therapeutic Refactoring”
(srsly)
106
Tuesday, October 22, 13
163. I work at LivingSocial.
We’re hiring.
107
Tuesday, October 22, 13
164. I work at LivingSocial.
We’re hiring.
(Who isn’t?)
107
Tuesday, October 22, 13
165. Thanks to:
Jim Shore and Diana Larsen
for “Agile Fluency”
Kirsten Comandich
Sandi Metz, Katrina Owen,
Sonia Connolly
PDX.rb
108
Tuesday, October 22, 13