roberts.gulans
roberts.gulans
AEAsh Elixir
Created by roberts.gulans on 5/5/2023 in #support
About policies
sorry for this thread, its unrelated. its my machines fault 😄
12 replies
AEAsh Elixir
Created by roberts.gulans on 5/5/2023 in #support
About policies
i dont know man, sometimes, elixir compiled parts get stuck and do not recompile correctly. Like once every month. Rarely enough that i dont think of it streight away, but often enough, that im like, ah, once again 😄
12 replies
AEAsh Elixir
Created by roberts.gulans on 5/5/2023 in #support
About policies
it seems so.
12 replies
AEAsh Elixir
Created by roberts.gulans on 5/5/2023 in #support
About policies
i did dependency update/mix compile --force, and now it seems to work correctly
12 replies
AEAsh Elixir
Created by roberts.gulans on 5/5/2023 in #support
About policies
Yes thats is exactly what i expect. after providing authorization authorize :by_default i expect that policies will be executed and only related data will be show or empty list, if actor not present. currently if actor is not present all organizations are listed
12 replies
AEAsh Elixir
Created by roberts.gulans on 5/4/2023 in #support
How to create organization after user creation.
Question about this is, how i possible could find by my self that action for it is :register_with_password? Im a little bit confused about how to approach finding related parts.
16 replies
AEAsh Elixir
Created by roberts.gulans on 5/3/2023 in #support
select relationship
How to mark as sloved? 😄
18 replies
AEAsh Elixir
Created by roberts.gulans on 5/3/2023 in #support
select relationship
I found "solution"
actions do
defaults [:create, :read, :update, :destroy]
end

# to
actions do
defaults [:read, :update, :destroy]

create :create do
argument :debit_account_id, :uuid, allow_nil?: false
argument :credit_account_id, :uuid, allow_nil?: false
end
end
actions do
defaults [:create, :read, :update, :destroy]
end

# to
actions do
defaults [:read, :update, :destroy]

create :create do
argument :debit_account_id, :uuid, allow_nil?: false
argument :credit_account_id, :uuid, allow_nil?: false
end
end
It seems that defaults does not add ability to populate relations. Now IO.inspect(AshPhoenix.Form.errors(form, for_path: :all)) prints
%{
[] => [
paid_at: "is required",
subject: "is required",
credit_account_id: "is required",
debit_account_id: "is required"
]
}
%{
[] => [
paid_at: "is required",
subject: "is required",
credit_account_id: "is required",
debit_account_id: "is required"
]
}
As we can see it now has correct errors for related ids as well. Im not sure if this is intended behaviour of defaults or not. I would argue that defaults [:create, :update], should be able to populate relationship ids as well.
18 replies
AEAsh Elixir
Created by roberts.gulans on 5/3/2023 in #support
select relationship
%{[] => [paid_at: "is required", amount: "is required"]}
18 replies
AEAsh Elixir
Created by roberts.gulans on 5/3/2023 in #support
select relationship
# remaining parts
defp prepare_form(nil, current_user) do
AshPhoenix.Form.for_create(Budget.Payment, :create, api: Budget, actor: current_user)
end

defp prepare_form(payment, current_user) do
AshPhoenix.Form.for_update(payment, :update, api: Budget, actor: current_user)
end
# remaining parts
defp prepare_form(nil, current_user) do
AshPhoenix.Form.for_create(Budget.Payment, :create, api: Budget, actor: current_user)
end

defp prepare_form(payment, current_user) do
AshPhoenix.Form.for_update(payment, :update, api: Budget, actor: current_user)
end
18 replies
AEAsh Elixir
Created by roberts.gulans on 5/3/2023 in #support
select relationship
defmodule Web.Budget.Payments.CreateEditFormComponent do
def render(assigns) do
~H"""
<div>
<.simple_form
:let={f}
for={@form}
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.header><%= @title %></.header>
<.input field={{f, :subject}} type="text" label="Subject" />
<.input field={{f, :amount}} type="number" label="Amount" />
<.input field={{f, :credit_account_id}} type="select" prompt="Select credit account" label="Credit account" options={@accounts} />
<.input field={{f, :debit_account_id}} type="select" prompt="Select debit account" label="Debit account" options={@accounts} />
<:actions>
<.button phx-disable-with="Creating...">Create</.button>
</:actions>
</.simple_form>
</div>
"""
end

def update(%{payment: payment, current_user: current_user} = assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> assign(:form, prepare_form(payment, current_user))
|> assign_new(:accounts, fn _ -> Budget.Account.list!() |> Enum.map(&{&1.subject, &1.id}) end)}
end

def handle_event("validate", %{"form" => params}, socket) do
{:noreply, assign(socket, :form, AshPhoenix.Form.validate(socket.assigns.form, params))}
end

def handle_event("save", %{"form" => params}, socket) do
params = Map.put(params, "paid_at", DateTime.utc_now())

case AshPhoenix.Form.submit(socket.assigns.form, params: params) do
{:ok, payment} ->
message =
case socket.assigns.form.type do
:create -> "Payment created successfully"
:update -> "Payment updated successfully"
end

{:noreply,
socket}

{:error, form} ->
{:noreply, assign(socket, form: form)}
end
end
end
defmodule Web.Budget.Payments.CreateEditFormComponent do
def render(assigns) do
~H"""
<div>
<.simple_form
:let={f}
for={@form}
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.header><%= @title %></.header>
<.input field={{f, :subject}} type="text" label="Subject" />
<.input field={{f, :amount}} type="number" label="Amount" />
<.input field={{f, :credit_account_id}} type="select" prompt="Select credit account" label="Credit account" options={@accounts} />
<.input field={{f, :debit_account_id}} type="select" prompt="Select debit account" label="Debit account" options={@accounts} />
<:actions>
<.button phx-disable-with="Creating...">Create</.button>
</:actions>
</.simple_form>
</div>
"""
end

def update(%{payment: payment, current_user: current_user} = assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> assign(:form, prepare_form(payment, current_user))
|> assign_new(:accounts, fn _ -> Budget.Account.list!() |> Enum.map(&{&1.subject, &1.id}) end)}
end

def handle_event("validate", %{"form" => params}, socket) do
{:noreply, assign(socket, :form, AshPhoenix.Form.validate(socket.assigns.form, params))}
end

def handle_event("save", %{"form" => params}, socket) do
params = Map.put(params, "paid_at", DateTime.utc_now())

case AshPhoenix.Form.submit(socket.assigns.form, params: params) do
{:ok, payment} ->
message =
case socket.assigns.form.type do
:create -> "Payment created successfully"
:update -> "Payment updated successfully"
end

{:noreply,
socket}

{:error, form} ->
{:noreply, assign(socket, form: form)}
end
end
end
18 replies
AEAsh Elixir
Created by roberts.gulans on 5/3/2023 in #support
select relationship
# validate callback
@impl true
def handle_event("validate", %{"form" => params}, socket) do
{:noreply, assign(socket, :form, AshPhoenix.Form.validate(socket.assigns.form, params))}
end
# validate callback
@impl true
def handle_event("validate", %{"form" => params}, socket) do
{:noreply, assign(socket, :form, AshPhoenix.Form.validate(socket.assigns.form, params))}
end
18 replies
AEAsh Elixir
Created by roberts.gulans on 5/2/2023 in #support
Union relationships
Thanks mate. Unreal job you have made.
6 replies