ash_postgres/test/support/resources/author.ex

61 lines
1.3 KiB
Elixir
Raw Normal View History

defmodule AshPostgres.Test.Author do
@moduledoc false
use Ash.Resource,
data_layer: AshPostgres.DataLayer
postgres do
table("authors")
repo(AshPostgres.TestRepo)
end
attributes do
uuid_primary_key(:id, writable?: true)
attribute(:first_name, :string)
attribute(:last_name, :string)
2022-02-08 10:48:36 +13:00
attribute(:bio, AshPostgres.Test.Bio)
end
2022-04-20 03:08:28 +12:00
actions do
defaults([:create, :read, :update, :destroy])
end
calculations do
2022-02-08 10:48:36 +13:00
calculate(:title, :string, expr(bio[:title]))
calculate(:full_name, :string, expr(first_name <> " " <> last_name))
calculate(
:conditional_full_name,
:string,
expr(
if(
is_nil(first_name) or is_nil(last_name),
"(none)",
first_name <> " " <> last_name
)
)
)
calculate(
:nested_conditional,
:string,
expr(
if(
is_nil(first_name),
"No First Name",
if(
is_nil(last_name),
"No Last Name",
first_name <> " " <> last_name
)
)
)
)
calculate :param_full_name,
:string,
{AshPostgres.Test.Concat, keys: [:first_name, :last_name]} do
argument(:separator, :string, default: " ", constraints: [allow_empty?: true, trim?: false])
end
end
end