[ActiveSupport] [CoreExt] [#presence] Feature Request

From the comments on the file, one of the main short hand usage of ‘presence’ method is :

For example, something like

#   state   = params[:state]   if params[:state].present?
#   country = params[:country] if params[:country].present?
#   region  = state || country || 'US'
#
# becomes
#
#   region = params[:state].presence || params[:country].presence || 'US'

However the ‘||’ use case does not play well if the Object is a boolean type. Example usecase :

# checked = false
# checked.presence || true 
#=> this returns true even even though checked has a value.

Would it be a good idea to have a default value on presence method the way we can provide one with ‘fetch’ method ? . Example :

def presence(default_value = nil)
  return self if present?
  default_value
end

checked = false
checked.presence(true) #instead of 'checked.presence || value'

This also reducers from using ‘||’ and we can just use the argument. Very similar to hash.fetch(key, default_value)

Not a big case/change, but was wondering if someone was having the urge of adding this one line to make life a little easier :slight_smile:

I don’t think Boolean#presence buys you enough over just a #nil? check to warrant the awkwardness of the passed parameter.

Agreed. Was just trying to keep it common across the board as presence is essentially on Object and also thought ‘.presence(default)’ was more idiomatic than '.presence || default` . Thanks.