I have a serious nesting of ifs in a helper code and I would like to making cleaner. I would like to avoid case if possible as well.
I know there is probably a more Object-Oriented approach to this but I can't seem to know how.
I'm flooded with stuff like this:
def print_flight_options_status(invitation)
if invitation.group.travel_class == 'none'
not_allowed
elsif invitation.refused_flight_options?
not_needed
elsif invitation.selected_flights?
waiting_reservation
elsif invitation.flight_options.empty?
not_sent_yet
elsif invitation.requested_more_flight_options?
rejected
else
waiting_guest_input
end
end
def print_event_terms_status(invitation)
if invitation.event_terms_status.nil?
"<span class='grey_highlight pj_cat'>Aguardando</span>"
elsif invitation.accepted_event_terms?
"<span class='green_highlight pj_cat'>Aceito</span>"
elsif invitation.rejected_event_terms?
"<span class='red_highlight pj_cat'>Declinado</span>"
elsif invitation.cancelled_event_terms?
"<span class='yellow_highlight pj_cat'>Cancelado</span>"
end.html_safe
end
Is there a better/simpler way?
Thank you!
print_flight_options_statuslooks fantastic as it is, I wouldn't write it as in-lineifs. At most, to reduce the line-count, I'd useif condition then value ..., but it won't look as nice. Inprint_event_terms_statusI'd just use helpers for the HTML tags instead of building them by hand. – tokland Aug 3 '12 at 19:39