mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
improvement: add Ash.Sort.expr_sort
. In 3.0 sort should be a macro
This commit is contained in:
parent
154fa1d6d6
commit
32620dfd52
2 changed files with 26 additions and 0 deletions
|
@ -24,6 +24,23 @@ defmodule Ash.Sort do
|
|||
|
||||
alias Ash.Error.Query.{InvalidSortOrder, NoSuchAttribute}
|
||||
|
||||
defmacro expr_sort(expression, type \\ nil) do
|
||||
quote do
|
||||
require Ash.Expr
|
||||
type = unquote(type)
|
||||
|
||||
case Ash.Query.Calculation.new(
|
||||
:expr_sort,
|
||||
Ash.Resource.Calculation.Expression,
|
||||
[expr: Ash.Expr.expr(unquote(expression))],
|
||||
type && Ash.Type.get_type(type)
|
||||
) do
|
||||
{:ok, calc} -> calc
|
||||
{:error, term} -> raise Ash.Error.to_ash_error(term)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
A utility for parsing sorts provided from external input. Only allows sorting
|
||||
on public attributes and aggregates.
|
||||
|
|
|
@ -586,6 +586,15 @@ defmodule Ash.Test.Actions.ReadTest do
|
|||
|> Api.read()
|
||||
|> strip_metadata()
|
||||
end
|
||||
|
||||
test "a sort can use an expression", %{post1: post1, post2: post2} do
|
||||
require Ash.Sort
|
||||
|
||||
Post
|
||||
|> Ash.Query.sort([{Ash.Sort.expr_sort(title <> contents), :asc}])
|
||||
|> Api.read!()
|
||||
|> IO.inspect()
|
||||
end
|
||||
end
|
||||
|
||||
describe "get_by with only a single field" do
|
||||
|
|
Loading…
Reference in a new issue