I decided to move to another section of
Invoice before I tackle the finder used in
Invoice#open. I’m thinking about using a simple state machine for the different invoice states and that will require some redesign. I found it’s best to think about redesigns for time before starting on them.
Today I refactored the inner logic of
Invoice#outstanding to replace how a temp variable is being used.
1 2 3 4 5 6 7
class Invoice 0.0 return total else return 0.0 end end end
1 2 3
class Invoice 0 ? total : 0.0 end end
This was a simple method but it was doing two things wrong:
- There was too much code to do a simple arithmetic operation.
payments.collect(&:amount).sumwas creating a bunch of ActiveRecord objects that would just get thrown away. The new code uses SQL to sum the amount field directly.
I’m thinking about changing this method so it will return negative amounts when there is an overpayment. If that happens, I can remove the ternary operation entirely.