Last active
October 3, 2018 10:17
-
-
Save martink-io/9a5195e8d38a915e51c0c31565a2a4f2 to your computer and use it in GitHub Desktop.
I love refactoring!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Before Refactoring | |
def max_refund_exceeded | |
return unless quantity.present? | |
job_hours = self.job.number_of_hours | |
amendment_hours = self.job.job_items.where(type: ['JobItems::Lateness', 'JobItems::Refund']).map(&:quantity).sum | |
errors[:base] << "You've exceeded maximum refund" unless amendment_hours + quantity <= job_hours | |
end | |
# After Refactoring #1 | |
def max_refund_exceeded | |
return unless quantity.present? | |
job_hours = self.job.number_of_hours | |
amendment_hours = self.job.job_items.where(type: ['JobItems::Lateness', 'JobItems::Refund']).map(&:quantity).sum | |
errors[:base] << "You've exceeded maximum refund" unless amendment_hours + quantity <= max_refundable_amount | |
end | |
def max_refundable_amount | |
cancellation_penalty = self.job.job_items.where(type: 'JobItems::CancelationPenalty').first | |
late_booking_penalty = self.job.job_items.where(type: 'JobItems::LateBookingPenalty').first | |
return self.job.number_of_hours + cancellation_penalty.quantity + late_booking_penalty.quantity if cancellation_penalty.present? && late_booking_penalty.present? | |
return self.job.number_of_hours + cancellation_penalty.quantity if cancellation_penalty.present? && late_booking_penalty.blank? | |
return self.job.number_of_hours + late_booking_penalty.quantity if late_booking_penalty.present? && cancellation_penalty.blank? | |
self.job.number_of_hours | |
end | |
# After Refactoring #2 | |
def max_refund_exceeded | |
return unless quantity.present? | |
amendment_hours = self.job.job_items.where(type: REFUNDABLE_TYPES).map(&:quantity).sum | |
amendment_hours -= quantity if self.type == 'JobItems::CancelationPenalty' | |
errors[:base] << "You've exceeded maximum refund" unless amendment_hours + quantity <= max_refundable_hours | |
end | |
def max_refundable_hours | |
cancellation_penalty = self.job.job_items.where(type: 'JobItems::CancelationPenalty').first | |
late_booking_penalty = self.job.job_items.where(type: 'JobItems::LateBookingPenalty').first | |
max_refundable_hours = self.job.number_of_hours | |
max_refundable_hours += late_booking_penalty.quantity if late_booking_penalty.present? | |
max_refundable_hours -= cancellation_penalty.quantity if cancellation_penalty.present? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment