The Refactoring
Today I used move method on the InvoiceController
to move the #last_invoice_number
to the Invoice
model
Before
1
2
3
4
5
6
7
8
|
class InvoiceController 'id DESC')
unless last_invoice.nil?
last_invoice.invoice_number
else
'-'
end
end
end |
class InvoiceController 'id DESC')
unless last_invoice.nil?
last_invoice.invoice_number
else
'-'
end
end
end
1
2
3
|
class Invoice < ActiveRecord::Base
# ...
end |
class Invoice < ActiveRecord::Base
# ...
end
After
1
2
3
|
class InvoiceController < ApplicationController
# ...
end |
class InvoiceController < ApplicationController
# ...
end
1
2
3
4
5
6
7
8
9
|
class Invoice 'id DESC')
if last_invoice.present?
last_invoice.invoice_number
else
'-'
end
end
end |
class Invoice 'id DESC')
if last_invoice.present?
last_invoice.invoice_number
else
'-'
end
end
end
Review
I’m really starting to believe that the only methods in a controller should be actions or filters. Everything else should me moved up to ApplicationController
, down to the Model, or sideways into Helpers. Controllers still seem to be the default place code is added in Rails and the place that always has all the bad code.
Reference commit