I am wondering if there’s a way to write this code in a more elegant fashion:
def default_price
if project.hourly_rate.present?
project.hourly_rate
elsif project.person.hourly_rate.present?
project.person.hourly_rate
elsif project.person.organisation.hourly_rate.present?
project.person.organisation.hourly_rate
else
user.preference.hourly_rate
end
end
There’s a lot of repetition going on here between the conditions and the return values. Is there a better way to code this?
Thanks for any help.
or, to refactor a bit more:
Furthermore, if
user.preference.hourly_rateis alwayspresent?, then you can further refactor: