Skip to content

Instantly share code, notes, and snippets.

@dznz
Created May 5, 2015 22:36
Show Gist options
  • Save dznz/e890a35259271f89a118 to your computer and use it in GitHub Desktop.
Save dznz/e890a35259271f89a118 to your computer and use it in GitHub Desktop.
Using array operations to simplify conditional complexity
def build_date_filter(earliest_date, latest_date)
return '' if !earliest_date && !latest_date
latest_date_query = "publicationDate > #{earliest_date}" if earliest_date
earliest_date_query = "publicationDate < #{latest_date}" if latest_date
if latest_date_query && earliest_date_query
"(#{latest_date_query} AND #{earliest_date_query})"
else
latest_date_query ? latest_date_query : earliest_date_query
end
end
def build_date_filter(earliest_date, latest_date)
return '' if !earliest_date && !latest_date
query_arr = []
query_arr << "publicationDate > #{earliest_date}" if earliest_date
query_arr << "publicationDate < #{latest_date}" if latest_date
query_arr.intersperse('AND')
query_arr
.unshift('(')
.push(')')
.join(' ')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment