2023-09-23 14:52:22 +12:00
|
|
|
defmodule AshSqlite.Test.Author do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
2024-04-02 05:18:42 +13:00
|
|
|
domain: AshSqlite.Test.Domain,
|
2023-09-23 14:52:22 +12:00
|
|
|
data_layer: AshSqlite.DataLayer
|
|
|
|
|
|
|
|
sqlite do
|
|
|
|
table("authors")
|
|
|
|
repo(AshSqlite.TestRepo)
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id, writable?: true)
|
2024-04-02 05:18:42 +13:00
|
|
|
attribute(:first_name, :string, public?: true)
|
|
|
|
attribute(:last_name, :string, public?: true)
|
|
|
|
attribute(:bio, AshSqlite.Test.Bio, public?: true)
|
|
|
|
attribute(:badges, {:array, :atom}, public?: true)
|
2023-09-23 14:52:22 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-04-02 05:18:42 +13:00
|
|
|
default_accept(:*)
|
2023-09-23 14:52:22 +12:00
|
|
|
defaults([:create, :read, :update, :destroy])
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-04-02 05:18:42 +13:00
|
|
|
has_one(:profile, AshSqlite.Test.Profile, public?: true)
|
|
|
|
has_many(:posts, AshSqlite.Test.Post, public?: true)
|
2023-09-23 14:52:22 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
calculations do
|
|
|
|
calculate(:title, :string, expr(bio[:title]))
|
|
|
|
calculate(:full_name, :string, expr(first_name <> " " <> last_name))
|
2023-10-06 09:24:39 +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])))
|
|
|
|
# calculate(:split_full_name, {:array, :string}, expr(string_split(full_name)))
|
2023-09-23 14:52:22 +12:00
|
|
|
|
|
|
|
calculate(:first_name_or_bob, :string, expr(first_name || "bob"))
|
|
|
|
calculate(:first_name_and_bob, :string, expr(first_name && "bob"))
|
|
|
|
|
|
|
|
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,
|
|
|
|
{AshSqlite.Test.Concat, keys: [:first_name, :last_name]} do
|
|
|
|
argument(:separator, :string, default: " ", constraints: [allow_empty?: true, trim?: false])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|