Skip to content

Commit

Permalink
improvement: support latest ash changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jan 19, 2024
1 parent 97847da commit f20a07f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
erlang 26.0.2
elixir 1.15.4
elixir 1.16.0
15 changes: 11 additions & 4 deletions lib/sort.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ defmodule AshPostgres.Sort do
calcs =
Enum.flat_map(sort, fn
{%Ash.Query.Calculation{} = calculation, _} ->
[calculation]
{:ok, expression} =
calculation.opts
|> calculation.module.expression(calculation.context)
|> Ash.Filter.hydrate_refs(%{
resource: resource,
parent_stack: query.__ash_bindings__[:parent_resources] || [],
public?: false
})

[{calculation, Ash.Filter.move_to_relationship_path(expression, relationship_path)}]

_ ->
[]
Expand All @@ -59,7 +68,7 @@ defmodule AshPostgres.Sort do
query,
%Ash.Filter{
resource: resource,
expression: calcs
expression: Enum.map(calcs, &elem(&1, 1))
},
left_only?: true
)
Expand All @@ -84,9 +93,7 @@ defmodule AshPostgres.Sort do
|> calc.module.expression(calc.context)
|> Ash.Filter.hydrate_refs(%{
resource: resource,
aggregates: query.__ash_bindings__.aggregate_defs,
parent_stack: query.__ash_bindings__[:parent_resources] || [],
calculations: %{},
public?: false
})
|> Ash.Filter.move_to_relationship_path(relationship_path)
Expand Down
4 changes: 4 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ defmodule AshPostgres.Test.Post do
expr(latest_comment_created_at > fragment("now()") || type(false, :boolean))
)

calculate(:price_times_2, :integer, expr(price * 2))

calculate(
:was_created_in_the_last_month,
:boolean,
Expand Down Expand Up @@ -333,6 +335,8 @@ defmodule AshPostgres.Test.Post do
uniq?(true)
end

count(:count_of_ratings, :ratings)

list :comment_titles_with_5_likes, :comments, :title do
sort(title: :asc_nils_last)
filter(expr(likes >= 5))
Expand Down

0 comments on commit f20a07f

Please sign in to comment.