Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Last active August 2, 2019 01:01
Show Gist options
  • Save paulcsmith/5727ac9a2db44e3c738948d506b99f90 to your computer and use it in GitHub Desktop.
Save paulcsmith/5727ac9a2db44e3c738948d506b99f90 to your computer and use it in GitHub Desktop.
class UserQuery < User::BaseQuery
macro safe_sort(default, **scopes)
def safe_sort(sort_by : String = {{ default }})
case sort_by
{% for key, value in scopes %}
when {{ key.stringify }}
{{ value.id }}
{% end %}
else
raise "Unknown sorty option #{sort_by}"
end
end
safe_sort default: recently_updated,
recently_updated: updated_at.desc_order,
recently_created: created_at.desc_order
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment