2021-06-04 17:48:35 +12:00
|
|
|
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)
|
2022-09-16 08:51:49 +12:00
|
|
|
attribute(:badges, {:array, :atom})
|
2021-06-04 17:48:35 +12:00
|
|
|
end
|
|
|
|
|
2022-04-20 03:08:28 +12:00
|
|
|
actions do
|
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
2022-05-23 10:30:20 +12:00
|
|
|
relationships do
|
|
|
|
has_one(:profile, AshPostgres.Test.Profile)
|
2022-06-30 07:08:49 +12:00
|
|
|
has_many(:posts, AshPostgres.Test.Post)
|
2022-05-23 10:30:20 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
aggregates do
|
|
|
|
first(:profile_description, :profile, :description)
|
|
|
|
end
|
|
|
|
|
2021-06-04 17:48:35 +12:00
|
|
|
calculations do
|
2023-11-17 12:12:00 +13:00
|
|
|
calculate(
|
|
|
|
:description,
|
|
|
|
:string,
|
|
|
|
expr(
|
|
|
|
if is_nil(^actor(:id)) do
|
|
|
|
"no actor"
|
|
|
|
else
|
|
|
|
"actor found"
|
|
|
|
end
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-02-08 10:48:36 +13:00
|
|
|
calculate(:title, :string, expr(bio[:title]))
|
2021-06-04 17:48:35 +12:00
|
|
|
calculate(:full_name, :string, expr(first_name <> " " <> last_name))
|
2023-02-22 04:21:53 +13:00
|
|
|
calculate(:full_name_with_nils, :string, expr(string_join([first_name, last_name], " ")))
|
|
|
|
calculate(:full_name_with_nils_no_joiner, :string, expr(string_join([first_name, last_name])))
|
2023-07-13 07:16:28 +12:00
|
|
|
calculate(:split_full_name, {:array, :string}, expr(string_split(full_name)))
|
|
|
|
|
|
|
|
calculate(
|
|
|
|
:split_full_name_trim,
|
|
|
|
{:array, :string},
|
|
|
|
expr(string_split(full_name, " ", trim?: true))
|
|
|
|
)
|
2021-06-04 17:48:35 +12:00
|
|
|
|
2023-07-13 09:05:33 +12:00
|
|
|
calculate(:first_name_from_split, :string, expr(at(split_full_name_trim, 0)))
|
|
|
|
|
2022-07-21 06:19:06 +12:00
|
|
|
calculate(:first_name_or_bob, :string, expr(first_name || "bob"))
|
|
|
|
calculate(:first_name_and_bob, :string, expr(first_name && "bob"))
|
|
|
|
|
2021-06-04 17:48:35 +12:00
|
|
|
calculate(
|
|
|
|
:conditional_full_name,
|
|
|
|
:string,
|
|
|
|
expr(
|
|
|
|
if(
|
|
|
|
is_nil(first_name) or is_nil(last_name),
|
|
|
|
"(none)",
|
|
|
|
first_name <> " " <> last_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-02-01 08:36:46 +13:00
|
|
|
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
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-06-04 17:48:35 +12:00
|
|
|
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
|