I also realized that I do crap like this a lot too:
self.owner_object_association ? self.owner_object_association :
default_thing
Where this would give me:
self.owner_object_association.or_if_blank? default_thing
I really don't like "or_if_blank?"
It feels like it should live in a gem,
partly because it doesn't seem necessary,
and partly because there are too many variants I could see people
needing.
or_if_nil
or_if_blank
or_if_present
or_if_a_plate_of_chips
But saying that,
A clear distinctinction needs to be made between;
>> a.blank? ? b : a
and
>> a.or_if_blank?(b)
namely, in the first "b" only gets evaluated ONLY IF "a" is blank
in the second, "b" ALWAYS get evaluated.
It's not necessarily a problem.
But I worry that we'll see code like;
>> a.or_if_blank?
(call_a_function_that_does_something_bad_but_also_returns_something)
Perhaps a different approach would be something like this
>> user.bumblebee
(:full_name, :email, :something_else, :another_fallback)
which is equivalent to;
>> if user.full_name.present?
>> return user.full_name
>> elsif user.email.present?
>> return user.email
>> elsif user.something_else.present?
>> return user.something_else
>> elsif user.another_fallback.present?
>> return user.another_fallback
>> end
The bumblebee hovers around the user,
looking for a flower with some pollen.
It goes from flower to flower,
trying each method in order,
and when it finds one that isn't empty,
it returns it to the hive.