Rails has a useful method called presence
that allows you to use the value of an object, otherwise it returns nil
.
It is very simple. Below is the implemenation.
def presence
self if present?
end
We can use it to refactor the following code
state = params[:state] if params[:state].present?
country = params[:country] if params[:country].present?
region = state || country || 'US'
to this
region = params[:state].presence || params[:country].presence || 'US'