iyjim

iyjim

Ash.Resource — ash v3.5.2 says that the default value can be a zero arity function e.g. &MyMod.my_fun/0 or a value.

What if the default argument value should depend on the actor? Is there a way to get the actor in the zero arity function?

Please help.

Showing Posts 1 to 10

ken-kost

ken-kost

Why make a dynamic value dependent on actor to be default?
I think you could do something like:

      argument :your_argument, :string do
        allow_nil? true
        default "default_value"
      end

      # change relate_actor(:user) <- maybe you want this as well

      change fn %{data: data, arguments: arguments} = changeset, _context ->
        value = case // get actor here // do
                     ... -> ...
                     nil -> arguments.your_argument
                     end
        Ash.Changeset.force_change_attribute(changeset, :your_attribute, value)
      end

You can dbg changeset and context. Make sure to pass the actor in your action call in options.

Not sure if best approach though :thinking: Perhaps Zach will have a better idea. :bulb:

iyjim

iyjim OP

Thanks @ken-kost for your reply.

We are building some internal used system, and would like to provide dynamic default values for different actors (roles actually) on forms.

For some reason, we got to use generic action, so we got to use the AshPhoenix.Form.for_action(). But when we try to set dynamic default value of a form using Ash.ActionInput.set_argument(input, :arg1, value1) in :prepare_source option, we don’t see any different on the form. That’s why I asked the way to set the dynamic default value in the “default” option.

Fortunately, when we use %Ash.ActionInput{input | arguments: %{arg1: value1}} in :prepare_source option to set dynamic default values, it works! And since we can get actor in the :prepare_source option when calling AshPhoenix.Form.for_action(), this issue is resolved.

I’m just wondering:

  1. Why Ash.ActionInput.set_argument(input, :arg1, value1) in :prepare_source option does not work?
  2. Is %Ash.ActionInput{input | arguments: %{arg1: value1}} in :prepare_source option the right way to do so?
zachdaniel

zachdaniel

Creator of Ash

Those should be for all intents and purposes the same thing. I’m not really sure why one would work and one wouldn’t :thinking: It is the trouble with action_input is that they don’t have a concept of preparations/changes the way that queries do. This is something I’d like to change in the future. What you can do to make things a bit simpler is just set the default in the params when creating the form. Would that work? i.e Form.for_action(..., params: %{"default" => "values"})

iyjim

iyjim OP

Thank you for your prompt reply. But the :params option in Form.for_action() get this error msg:

[error] GenServer #PID<0.2885.0> terminating
** (KeyError) key :action not found in: {:error,
 %Ash.Error.Unknown{
   errors: [
     %Ash.Error.Unknown.UnknownError{
       error: "** (Spark.Options.ValidationError) unknown options [:params], valid options are: [:domain, :context, :authorize?, :tenant, :actor, :skip_unknown_inputs, :tracer, :private_arguments]",
       field: nil,
       value: nil,
       splode: Ash.Error,
       bread_crumbs: [],
       vars: [],
       path: [],
       stacktrace: #Splode.Stacktrace<>,
       class: :unknown
     }
   ]
 }}

It looks better if Form.for_action() has this :params option though.

zachdaniel

zachdaniel

Creator of Ash

I think this may be a bug? Can you open an issue with examples of your code and stack traces of the error etc. on ash_phoenix

ken-kost

ken-kost

Could you print your input? Reasons for the skip might be if input does not have action key and that action has to have arguments in which one of them is the sent one i.e. Enum.find is used.

AFAIS for_ calls do not have params option. :thinking:

zachdaniel

zachdaniel

Creator of Ash

Oh you’re right, you’d want to validate once with params after calling the action, sorry :slight_smile: my inner LLM hallucinated that one :joy:

zachdaniel

zachdaniel

Creator of Ash

Hmm…looking at the code, I wasn’t hallucinating. There definitely should be a params option when creating new forms. I tried it myself just recently and it worked :smiley:

iyjim

iyjim OP

Thanks for your kindly help.

I’m faceing a deadline on some work, so the example to reproduce the error will be created later next week.

If a simple example will work without error, I might have to revisit my code to see the differences, like arguments with default value or so, and apply them to this simple example, so it might need some more works to reproduce the error.

Thank you very much!

ken-kost

ken-kost

I does indeed work but it’s not mentioned in the docs or rather in @for_options. But what’s weird is the error (Spark.Options.ValidationError) unknown options [:params] because validate_opts_with_extra_keys is used where params ends up in extra keys that are not spark validated and are merged with validated for options. Weird :thinking:

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews