diff --git a/dev/resources/accounts/calculations/concat.ex b/dev/resources/accounts/calculations/concat.ex new file mode 100644 index 0000000..f9e6cfd --- /dev/null +++ b/dev/resources/accounts/calculations/concat.ex @@ -0,0 +1,30 @@ +defmodule Demo.Calculations.Concat do + use Ash.Resource.Calculation + + @impl true + def init(opts) do + if opts[:keys] && is_list(opts[:keys]) && Enum.all?(opts[:keys], &is_atom/1) do + {:ok, opts} + else + {:error, "Expected a `keys` option for which keys to concat"} + end + end + + @impl true + def load(_query, opts, _context) do + opts[:keys] + end + + @impl true + def calculate(records, opts, %{arguments: %{separator: separator}}) when is_bitstring(separator) do + Enum.map(records, fn record -> + Enum.map_join(opts[:keys], separator, fn key -> + to_string(Map.get(record, key)) + end) + end) + end + + def calculate(_records, _opts, _context) do + {:error, "Argument separator invalid"} + end +end diff --git a/dev/resources/accounts/resources/user.ex b/dev/resources/accounts/resources/user.ex index b2a5254..4bd1a9d 100644 --- a/dev/resources/accounts/resources/user.ex +++ b/dev/resources/accounts/resources/user.ex @@ -20,6 +20,8 @@ defmodule Demo.Accounts.User do read_actions [:me, :read, :by_id, :by_name] table_columns [:id, :first_name, :last_name, :representative, :admin, :full_name, :api_key, :date_of_birth] + + show_calculations [:multi_arguments, :full_name] end multitenancy do @@ -82,7 +84,28 @@ defmodule Demo.Accounts.User do end calculations do - calculate :full_name, :string, expr(first_name <> " " <> last_name) + calculate :full_name, :string, {Demo.Calculations.Concat, keys: [:first_name, :last_name]} do + argument :separator, :string do + allow_nil? false + constraints [allow_empty?: true, trim?: false] + default " " + end + end + + calculate :is_super_admin?, :boolean, expr(admin && representative) + + calculate :multi_arguments, :string, expr("Arg1: " <> ^arg(:arg1) <> ", Arg2: " <> (if ^arg(:arg2), do: "Yes", else: "No") <> ", Arg3: " <> ^arg(:arg3)) do + argument :arg1, :string do + allow_nil? false + constraints [allow_empty?: false] + end + + argument :arg2, :boolean + + argument :arg3, :float do + allow_nil? true + end + end end attributes do diff --git a/lib/ash_admin/components/resource/form.ex b/lib/ash_admin/components/resource/form.ex index ec32906..d772fcb 100644 --- a/lib/ash_admin/components/resource/form.ex +++ b/lib/ash_admin/components/resource/form.ex @@ -759,7 +759,7 @@ defmodule AshAdmin.Components.Resource.Form do prompt={allow_nil_option(@attribute, @value)} name={@name || @form.name <> "[#{@attribute.name}]"} /> - <% markdown?(@form.source.resource, @attribute) -> %> + <% markdown?(@resource, @attribute) -> %>